Add 1 minute to the hour

Asked

Viewed 250 times

4

I have a problem solving an exercise. It may be easy but I’m not finding the right solution.

I have the following class Time

public final class Time {
      private final int hours;
      private final int minutes;

      public Time(int h, int m) {hours = h; minutes = m;}

      public static final Time MIDNIGHT = new Time(0,0);

      public int getHours() {return hours;}

      public int getMinutes() {return minutes;}

      public Time tick() {
           return null;
      }
}

I have to define the method tick() which shall return a new type object Time corresponding to the addition of one minute over the represented time.

1 answer

5


As in your code you have the constant MIDNIGHT, I’m assuming the class Time represents hours of the day.

So simply adding 1 to minutes can bring invalid values when you have one Time which represents 15:59, for example. By simply adding 1 to the minutes, you will end up with the value corresponding to 15:60 (and then 15:61, 15:62, and so on), when the correct would be to have the value 16:00 (and then 16:01, 16:02, etc).

Therefore you should add 1 to the minutes and adjust the value if it is 60. For this we use the operator %, that returns the rest of the division. To ensure that the next minute is between 0 and 59, you can do (this.minutes + 1) % 60.

Then, if the minutes value is zero, the hours value should be adjusted to the next hour. I’m assuming that the hours also cannot exceed the valid values, and that after 23:59, the next value is 00:00. Therefore, I also use the operator %, but now with the value 24 (ensuring that the hours have values between 0 and 23).

Thus, the method tick() gets like this:

public Time tick() {
    int proximaHora = this.hours;
    int proximoMinuto = (this.minutes + 1) % 60;

    // se o próximo minuto é zero, deve ir para a próxima hora
    if (proximoMinuto == 0) {
        proximaHora = (proximaHora + 1) % 24;
    }
    return new Time(proximaHora, proximoMinuto);
}

Also, it would be interesting for the constructor to validate the received values, to prevent you from creating invalid hours like 99:99, for example. If the values are invalid, you can launch a IllegalArgumentException (which is the native exception used to indicate that an invalid parameter has been passed):

public Time(int h, int m) {
    if (h < 0 || h > 24) {
        throw new IllegalArgumentException("Valor inválido para as horas: " + h);
    }
    if (m < 0 || m > 59) {
        throw new IllegalArgumentException("Valor inválido para os minutos: " + m);
    }
    hours = h;
    minutes = m;
}
  • 1

    you’re absolutely right, thank you!

  • @Override&#xA;public int compareTo(Object o) {&#xA; return this.hours.compareTo(o.hours)&&this.minutes.compareTo(o.minutes);&#xA;} Can you tell me why the implementation of the Comparable interface gives me an error in (o. hours) and (o. minutes) ?

  • Can you help me with the doubt above ?

  • @Stinrose In this case, I suggest you ask another question, so as not to mix up the issues. But this might help you: https://stackoverflow.com/questions/9150446/compareto-with-primitives-integer-int

  • @Stinrose The idea of a.compareTo(b) is to return -1 if a is less than b, 0 if they are equal and 1 if a is greater than b. The method returns a number and vc is using it as a boolean, so it’s not working

  • That would be right: public int compareTo(Time o) {&#xA; if(this.hours < o.hours && this.minutes < o.minutes) {return -1;}&#xA; if(this.hours > o.hours && this.minutes > o.minutes) {return 1;}&#xA; return 0;&#xA; }

  • Just think how you compare two times. You only need to compare the minutes if the time is equal. If the hours are different, you don’t even have to compare the minutes

  • In fact, you don’t need to return exactly -1 and 1, it could be any negative or positive number. So it looks like if (this.hours == o.hours) { return this.minutes - o.minutes; } return this.hours - o.hours; (if the hours are equal, compare the minutes; otherwise compare only the hours)

  • Ah, I think I got it, so just in case the hours are equal if(this.hours == o.hours), I compare the minutes if(this.minutes < o.minutes) {return -1;}&#xA;if(this.minutes > o.minutes) {return 1;}and at the end if they are all the same returns 0

  • ah right, I got it! I thought I had to return the -1 and 1

  • -1 and 1 is a commonly used "standard" but the documentation says that any negative or positive number serves: https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html#compareTo(T)

  • I get it, thank you!

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.