/* TimeOfDay.java ** An instance of this class represents a time of day, precise to a minute. ** ** Authors: R. McCloskey ** Known Defects: ... */ public class TimeOfDay { // 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. private int hour; // class invariant: 0 <= hour < 24 private int minute; // class invariant: 0 <= minute < 60 // constructors // ------------ /* Initializes this time of day to that described by the three ** parameters, which are assumed to express the time in the traditional ** 12-hour mode. An exception is thrown if 'h' is outside the range ** [1..12] or if 'm' is outside the range [0..60). */ public TimeOfDay(int h, int m, boolean isPM) { // Verify that the parameters have values within the respective // valid ranges; if not, throw an exception. if (h < 1 || h > 12) { throw new IllegalArgumentException(h + " is not a legal hour value"); } if (m < 0 || h >= 60) { throw new IllegalArgumentException(m + " is not a legal minute value"); } this.minute = m; this.hour = from12To24(h, isPM); } // observers // --------- /* Returns a string describing this time of day, in the traditional ** 12-hour (AM/PM) mode/format. Examples: "5:37am", "12:04pm". */ public String toString() { String hourStr, minuteStr, amPmStr; if (minute < 10) { minuteStr = "0" + minute; } else { minuteStr = "" + minute; } if (hour < 12) { amPmStr = "am"; } else { amPmStr = "pm"; } hourStr = "" + from24To12(this.hour); return hourStr + ':' + minuteStr + amPmStr; } /* Returns a string describing this time of day, in the 24-hour mode, ** which has this form: hh:mm. ** Examples: "05:37", "12:04". */ public String toString24() { return "hh:mm"; // STUB } /* 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 { 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 true; // STUB } // mutators // -------- /* Advances this time of day by a single minute, possibly resulting ** in it becoming the first minute of the following day. */ public void advanceOneMinute() { minute = (minute + 1) % 60; if (minute == 0) { hour = (hour + 1) % 24; } } /* 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 } /* Sets the "display mode" of this time of day to 24-hour mode, so ** that the toString() method will yield a string of the form hh:mm. */ public void setTo24Mode() { // STUB } /* Sets the "display mode" of this time of day to 12-hour mode, so ** that the toString() method will yield a string of the traditional ** 12-hour mode form (e.g., "8:05pm", "11:37am"). */ public void setTo12Mode() { // STUB } // utilities // --------- /* Translates the given 24-hour mode hour value into the corresponding ** 12-hour mode hour value. E.g., 15 translates to 3 because 15 hours ** past midnight corresponds to the 3pm hour. ** ** pre: 0 <= h < 24 */ private int from24To12(int h) { int result; if (h == 0) { result = 12; } else if (h <= 12) { result = h; } else { result = h - 12; } return result; } /* Translates the given 12-mode hour value into the corresponding ** 24-mode hour value. E.g., 3pm translates to 15 because the 3pm ** hour is 15 complete hours past midnight. ** ** pre: 1 <= h <= 12 */ private int from12To24(int h, boolean isPM) { int result; if (isPM) { if (h == 12) { result = 12; } else { result = h + 12; } } else { // !isPM if (h == 12) { result = 0; } else { result = h; } } return result; } }