0

enter image description here

In a 30-day month planner, the day reading goes this way: 1,2,3,...28,29,30,1,2,3.. Implement a class, ModMonth, to perform the following:

Q1.succ(dy), returning the value of the next day.

succ(29) => 30,
succ(30)=> 1

Q2. pred(dy), returning the value of the previous day

pred(2) => 1,
pred(1) ==> 30.

Question is, how to make a short solution to it that also works for a trillion-day month by simply modifying some constants instead of enumerating a trillion values? As for instance:

public int succ(int dy) {
    int[] nextDay = {
        2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 
        19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 1
    };
    return nextDay [dy - 1];
}

Both of these methods are not acceptable.. which i can only think this way. Same goes to the public int succ int dy.. dy ==1

Is there any other way to make a short method? I'm using Drjava apps btw.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
Bwn
  • 29
  • 2
  • The [Mod Operator](http://stackoverflow.com/questions/90238/whats-the-syntax-for-mod-in-java) is what you're looking for. To get the next day try int succ(int n) { return (n+1) % 30; } – Exfridos Sep 13 '16 at 12:36
  • 1
    Do you assume all the months having the same number of days? Anyway I don't see any problem is this question. Even if you don't use mod, a simple if-condition will solve the problem. – user3437460 Sep 13 '16 at 12:39
  • Yes, assuming all the months having the same no.of days. Yes using if-condition can be done here but it is unacceptable. Same goes with the code that i wrote which is next day = 2 - 30 return and so on. If someone can help with a written code.. :l thanks for the comment – Bwn Sep 13 '16 at 12:45
  • For finding the next and the previous day, I too would opt for a simple if-else statement. If this is the last day of the month, the next day is 1, otherwise add 1 to today’s date. Similarly for previous, if this is the first, previous is 30. – Ole V.V. Sep 13 '16 at 12:49
  • Hey, it didn’t say a `switch` statement was unacceptable. Just joking, of course it is. And in case it hasn’t already been said, you need to declare *and use* some `static final int NO_OF_DAYS_IN_MONTH = 30`, or what you wish to call it (or `long` if trillion is to be taken literally). – Ole V.V. Sep 13 '16 at 12:51
  • 1
    @Bwn You only need a **single** if-condition. If it is not allowed for any reasons, just use a mod. This is a really really basic question. – user3437460 Sep 13 '16 at 12:52
  • 1
    Agree with @user3437460, as I read the assignment, I see nothing about a single `if` statement being unacceptable, as long as it is short and also works for a trillion day month. Long story short, `if` and `%` should both work, make your pick. – Ole V.V. Sep 13 '16 at 12:56

2 Answers2

0

Try this:

public class NewClass {
   static int monthLength;
   public static int succ(int n) {
       if(n+1 > monthLength)
       return n+1-monthLength;
       else
       return n+1;    
   }
   public static int pred(int n) {
       if(n-1 <= 0)
       return n-1+monthLength;
       else
       return n-1;  
   }
   public static void main(String[] args) {
       monthLength = 30;
       System.out.println(succ(29));
       System.out.println(succ(30));
       System.out.println(pred(2));
       System.out.println(pred(1));
   }
}
Eritrean
  • 15,851
  • 3
  • 22
  • 28
0

Please don't put homework questions here.

Answer:

succ(x): return x % 30 + 1
pred(x):  return x - 1 + (30 ? x == 1 : 0)
Majeed Siddiqui
  • 548
  • 2
  • 9