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;
}
you’re absolutely right, thank you!
– CSAnimor
@Override
public int compareTo(Object o) {
 return this.hours.compareTo(o.hours)&&this.minutes.compareTo(o.minutes);
}
Can you tell me why the implementation of the Comparable interface gives me an error in (o. hours) and (o. minutes) ?– CSAnimor
Can you help me with the doubt above ?
– CSAnimor
@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
– hkotsubo
@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 aboolean
, so it’s not working– hkotsubo
That would be right:
public int compareTo(Time o) {
 if(this.hours < o.hours && this.minutes < o.minutes) {return -1;}
 if(this.hours > o.hours && this.minutes > o.minutes) {return 1;}
 return 0;
 }
– CSAnimor
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
– hkotsubo
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)– hkotsubo
Ah, I think I got it, so just in case the hours are equal
if(this.hours == o.hours)
, I compare the minutesif(this.minutes < o.minutes) {return -1;}
if(this.minutes > o.minutes) {return 1;}
and at the end if they are all the same returns 0– CSAnimor
ah right, I got it! I thought I had to return the -1 and 1
– CSAnimor
-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)
– hkotsubo
I get it, thank you!
– CSAnimor