You want to write a method named compareTimes
that compares two times of day based on their hour (0 – 23) and
minute (0 – 59). The method takes four int
parameters: the hour and minute of the first time, and the hour and
minute of the second time. The method should return -1 if the first
time is before the second time, 0 if the two times are equal, or 1
if the first time is after the second time. For example,
compareTimes(4, 0, 8, 30) should return -1 since
4:00 am is before 8: 30 am.
Here’s your first attempt at writing
the compareTimes method:
public static int compareTimes method:
int result = 0;
if (m1 < m2) {
result = -1;
}
else if (h1 < h2)
{
result = -1;
}
else if (m1 >
m2) {
result = 1;
}
else if (h1 >
h2) {
result = 1;
}
return
result;
}
Give an example set of arguments (h1, m1, h2, m2) for which this
method does NOT return the correct value. The hour arguments must
be between 0 – 23; the minute arguments must be between 0 – 59.
I am having a hard time figuring out this problem. If you could
please explain your answer clearly for a beginner to understand. I
would greatly appreciate it.
Thank You