-
Minutes to return
Exercise 4.16* Write a Time method public int timeInMinutes(): The executor tells the total number of minutes that have passed since midnight.
Code:
public class Time extends Object {
private int itsHour;
private int itsMin;
private int timeMin;
/** Create an object for the given hour and minute. If min
* is negative, adjust the values to make 0 <= min < 60. */
public Time (int hour, int min) {
super();
itsHour = hour;
for (itsMin = min; itsMin < 0; itsMin = itsMin + 60){
itsHour--;
}
} //=======================
/** Return the time expressed in military time. */
public String toString(){
if (itsHour < 10) {
return ("0" + itsHour) + itsMin;
}else{
return ("" + itsHour) + itsMin;
}
} //=======================
/** Return the result of adding this Time to that Time. */
public Time add (Time that){
return that;
} // left as an exercise
public int timeInMinutes() {
}
}
I don't know what to even start with.
-
Re: Minutes to return
Suppose it's 15:20 (3:20pm); 15*60 + 20 minutes have passed since midnight ...
kind regards,
Jos
-
Re: Minutes to return
I finished that but now I've run into this one and I can't finish it.
Exercise 4.17* Write a Time method public Time subtract (Time that): The executor returns a new Time object that is itself minus the parameter, e.g., 0720 subtract 1430 is 1650. If the difference is negative, add an extra 24 hours.
Code:
public Time subtract (Time that) {
Time sub = sub - that;
if (sub < 0){
}
}
-
Re: Minutes to return
"sub" ist not "yourself" (that is your "this" object in that method)... first you need to create a NEW object.
Then copy your own object information to the new object.
Then you subtract the time and check for its validity. All right so far?
-
Re: Minutes to return
Go for the hh:mm notation again: 7:20 - 14:30 == -7:-10; the minutes are negative, so correct it: -8:50; the hours are negative, so correct it: 16:50. You can't just so sub-that where one (or both) of them is/are of type Time.
kind regards,
Jos
-
Re: Minutes to return
I still don't understand....