Reputation: 117
This is for a homework assignment. I'm posting here with the permission of my instructor. The assignment is to add methods to a class created earlier in the year to find the difference (in hours and minutes) between two times, among other things. The problem I'm having is that the code runner on the course's website says that there is an error when finding the difference between two times that are significantly far apart, but using the runner that was provided to me, I cannot create a combination of times that breaks the code I wrote. Below is the code:
public class Time implements Comparable
{
private int hour;
private int minute;
public Time()
{
hour = 0;
minute = 0;
}
public Time(int h, int m)
{
if (h>=1&&h<=23)
hour = h;
else hour = 0;
if (m>=0&&m<=59)
minute = m;
else minute = 0;
}
public String toString()
{
String hourStr = new String();
String minStr = new String();
if (hour<10)
hourStr = "0" + Integer.toString(hour);
else hourStr = Integer.toString(hour);
if (minute<10)
minStr = "0" + Integer.toString(minute);
else minStr = Integer.toString(minute);
String milTime = hourStr + minStr;
return (milTime);
}
public String difference(Time t)
{
int base = Integer.parseInt(toString());
int compy = Integer.parseInt(t.toString());
int comp = Math.abs(compy-base);
if (comp%100>=60) {
comp-=(comp%100-(60-Math.abs((compy%100)-(base%100))));
}
String reee = "000" + Integer.toString(comp);
reee = reee.substring(reee.length()-4, reee.length());
reee = reee.substring(0, 2) + ":" + reee.substring(2,4);
reee = "Time difference: " + reee;
return reee;
}
}
I've deleted some of the irrelevant methods. Here is the particular block of code from the runner that was provided to me:
Time t4 = new Time(12, 40);
Time t5 = new Time(5, 45);
System.out.println(t4.compareTo(t5));
System.out.println(t5.compareTo(t4));
System.out.println("Difference");
System.out.println(t4.difference(t5));
System.out.println(t5.difference(t4));
System.out.println(t4.difference(t4));
The compareTo parts work fine, but the difference method is what the website says I have wrong. However, replacing Times t4 and t5 with any combination doesn't seem to give me any problems. Do you notice anything wrong with my code that could conceivably trip up the website?
Upvotes: 1
Views: 70
Reputation: 86286
The difference between 9:55 and 10:05 — do we agree it’s 10 minutes? From 5 minutes to 10 to 5 minutes past 10.
I changed t4
and t5
to:
Time t4 = new Time(10, 5);
Time t5 = new Time(9, 55);
Now your program prints:
Difference
Time difference: 00:50
Time difference: 00:50
Time difference: 00:00
My guess is that this is what the website is objecting to.
You are subtracting the results of Integer.parseInt(toString())
. I don’t think this is sound. With a difference of 50 you cannot tell whether is was from 9:05 to 9:55 (indeed 50 minutes) or from 9:55 to 10:05 (only 10 minutes). You will need to subtract hours separately and minutes separately and then do the adjustments for sign, specifically if the two differences have opposite signs.
Upvotes: 1