/* TimeOfDay1224.java ** An instance of this class represents a time of day, precise to a minute. ** Its behavior augments that of its parent by supporting not only the ** 24-hour clock (as does its parent class) but also the 12-hour clock. ** ** Authors: R. McCloskey and < STUDENT's NAME > ** Collaborated with: ... ** Known Defects: ... */ public class TimeOfDay1224 extends TimeOfDay { // class constant // -------------- protected static final int MID_HOUR = HOURS_PER_DAY / 2; // instance variable // ----------------- protected boolean in24Mode; // governs whether toString() produces a string // describing the time of day in 24-hour notation // (e.g., "15:36") as opposed to 12-hour notation // (e.g., "8:15am") // constructors // ------------ /* Initializes this time of day to be in 12-hour mode and to correspond ** to the time 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 TimeOfDay1224(int h, int m, boolean isPM) { setTo(h, m, isPM); this.in24Mode = false; } /* Initializes this time of day to be in 24-hour mode and to correspond ** to the time described by the two parameters, which are assumed to ** express the time in 24-hour mode. ** An exception is thrown if 'h' is outside the range [0..23] or if ** 'm' is outside the range [0..60). */ public TimeOfDay1224(int h, int m) { // STUB! } /* Initializes this time of day to be in 24-hour mode and to correspond ** to midnight. */ public TimeOfDay1224() { this(0,0); } // observers // --------- // Returns a string describing this time of day. If in 24-hour mode, // a string such as "19:08" is returned; if in 12-hour mode, a string // such as "5:13pm" is returned. @Override public String toString() { return "Glorp!"; // STUB! } // mutators // -------- /* 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 ** (e.g., "08:02", "20:41"). */ public void setTo24Mode() { in24Mode = true; } /* 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 clock (e.g., "8:05pm", "11:37am"). */ public void setTo12Mode() { } // STUB! /* Sets this time to that described by the three parameters, which, in ** effect, provide a description of the time in term of the 12-hour clock. */ public void setTo(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 > MID_HOUR) { 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"); } // STUB! // Here translate the the three parameters (h, m, isPM) (that provide, // in effect, a 12-hour clock description of a time) into the pair // (h',m') that describes the same time on the 24-hour clock. // Then pass h' and m' to the parent class's setTo() method. } // 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 = MID_HOUR; } else if (h <= MID_HOUR) { result = h; } else { result = h - MID_HOUR; } 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 <= MID_HOUR */ private int from12To24(int h, boolean isPM) { int result; if (isPM) { if (h == MID_HOUR) { result = MID_HOUR; } else { result = h + MID_HOUR; } } else { // !isPM if (h == MID_HOUR) { result = 0; } else { result = h; } } return result; } }