/* TimeOfDay.java ** An instance of this class represents a time of day, precise to a minute. ** ** Authors: R. McCloskey and < STUDENT's NAME > ** Collaborated with: ... ** Known Defects: ... */ public class TimeOfDay { // class constants // --------------- public static final int MINUTES_PER_HOUR = 60; public static final int HOURS_PER_DAY = 24; // instance variables // ------------------ // A time of day is represented by the pair (hour, minute) of integers, // where the value of 'hour' is the number of complete hours since midnight // and the value of 'minute' is the number of complete minutes since the // beginning of the current hour. protected int hour; // class invariant: 0 <= hour < HOURS_PER_DAY protected int minute; // class invariant: 0 <= minute < MINUTES_PER_HOUR // constructors // ------------ /* Initializes this time of day to midnight. */ public TimeOfDay() { setTo(0,0); } /* Initializes this time of day to that described by the two parameters, ** which indicate, respectively, the number of complete hours since ** midnight and the number of minutes since the beginning of the current ** hour. ** An exception is thrown if 'h' is outside the range [0..HOURS_PER_DAY) ** or if 'm' is outside the range [0..MINUTES_PER_HOUR). */ public TimeOfDay(int h, int m) { setTo(h, m); } // observers // --------- /* Returns a string describing this time of day (e.g., "15:07", "09:45"). */ @Override public String toString() { return toTwoDigits(hour) + ":" + toTwoDigits(minute); } /* Reports whether or not this time of day and the given one represent ** the same minute of a day. */ public boolean equals(TimeOfDay t) { return this.hour == t.hour && this.minute == t.minute; } /* Reports whether or not this time of day occurs earlier in the day ** than the given time of day (t). */ public boolean isEarlierThan(TimeOfDay t) { boolean result; if (this.hour != t.hour) { result = this.hour < t.hour; } else { // this and t are in same hour of day result = this.minute < t.minute; } return result; } //Note: The statement 'result = this.hour < t.hour' // is equivalent to this inferior way of saying the same thing: // // if (this.hour < t.hour) { result = true; } // else { result = false; } // // A similar remark applies to 'result = this.minute < t.minute'. /* Reports whether or not this time of day occurs between the given ** times 'start' and 'stop'. To illustrate, consider these examples ** and take particular note of those in which 'start' is later in the ** day than is 'stop'. ** ** this start stop result ** ------- ------- ------- ------ ** 10:37am 9:15am 2:40pm true ** 6:37pm 9:15am 2:40pm false ** 8:23am 9:15pm 10:35am true ** 12:02pm 9:15pm 10:35am false */ public boolean isBetween(TimeOfDay start, TimeOfDay stop) { return false; // STUB! } // mutators // -------- /* Sets this time of day to that described by the two parameters, which ** indicate, respectively, the number of complete hours since midnight ** and the number of minutes since the beginning of the current hour. ** An exception is thrown if 'h' is outside the range [0..HOURS_PER_DAY) ** or if 'm' is outside the range [0..MINUTES_PER_HOUR). */ public void setTo(int h, int m) { // Verify that the parameters have values within the respective // valid ranges; if not, throw an exception. if (h < 0 || h >= HOURS_PER_DAY) { throw new IllegalArgumentException(h + " is not a legal hour value"); } if (m < 0 || h >= MINUTES_PER_HOUR) { throw new IllegalArgumentException(m + " is not a legal minute value"); } // If the parameters are within the valid ranges, set them to the // specified values. this.hour = h; this.minute = m; } /* Changes this time of day by the specified number of minutes. ** A positive value means that the time is moved forward by that ** many minutes, which could advance it one or more days into the ** future. A negative value means that the time is moved backward, ** which could place it one or more days in the past. */ public void changeByMinutes(int mDelta) { final int MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY; // STUB! } /* Changes this time of day by the specified number of hours. ** A positive value means that the time is moved forward by that ** many hours, which could advance it one or more days into the ** future. A negative value means that the time is moved backward, ** which could place it one or more days in the past. */ public void changeByHours(int hDelta) { // STUB! } /* Advances this time of day by a single minute, possibly resulting ** in it becoming the first minute of the following day. */ public void advanceOneMinute() { // STUB! } /* Retreats this time of day by a single minute, possibly resulting ** in it becoming the last minute of the preceding day. */ public void retreatOneMinute() { // STUB! } // utilities // --------- /* Returns the String image of the given int value, with a ** leading zero if its image is of length one. ** E.g., 5 --> "05", 21 --> "21". */ protected String toTwoDigits(int k) { if (k < 10) { return "0" + k; } else { return "" + k; } } }