/**
* Time represented as attributes hours and minutes.
* @author Dr. Jody Paul
* @version 1 March 2006
*/
public class Time {
private int hours;
private int minutes;
/**
* Constructor for objects of class Time that
* sets time to zero (0:00).
*/
public Time() {
hours = 0;
minutes = 0;
}
/**
* Constructor for objects of class Time that
* sets time to the number of minutes specified.
* The time is normalized to the equivalent number
* of hours and minutes such that minutes is in the
* range 0..59 inclusive.
* @param minutes the number of minutes
*/
public Time(int minutes) {
this.minutes = minutes;
normalize();
}
/**
* Constructor for objects of class Time that
* sets time to the number of hours and minutes specified.
* The time is normalized to the equivalent number
* of hours and minutes such that minutes is in the
* range 0..59 inclusive.
* @param hours the number of hours
* @param minutes the number of minutes
*/
public Time(int hours, int minutes) {
this.hours = hours;
this.minutes = minutes;
normalize();
}
/**
* Normalizes the time so that minutes is < 60.
*/
private void normalize() {
if (minutes >= 60) {
hours += minutes / 60;
minutes %= 60;
}
}
/**
* Retrieve the hours.
* @return the hours
*/
public int getHours() { return hours; }
/**
* Retrieve the minutes.
* @return the minutes
*/
public int getMinutes() { return minutes; }
/**
* Create a new Time object whose value is the
* current object's time incremented by the parameter.
* A parameter of null is equivalent to new Time().
* @param timeIncrement the amount to increment by
* @return new time object
*/
public Time increment(Time timeIncrement) {
if (timeIncrement == null) {
return new Time(hours, minutes);
}
return new Time(hours + timeIncrement.getHours(),
minutes + timeIncrement.getMinutes());
}
/**
* Determine if two Time objects are equal.
* Two Time objects are equal if and only if they have the same
* number of hours and same number of minutes.
* (Returns false if the specified Time is null.)
* @param t the Time object to compare with this Time
* @return true if the hours and minutes are the same; false otherwise
*/
public boolean equals(Time t) {
if (t == null) return false;
return ((hours == t.getHours()) && (minutes == t.getMinutes()));
}
}