/* An instance of this class represents a time of day (e.g. 6:34AM) ** precise to the minute. ** ** Date: January 2025 ** Authors: R. McCloskey and CMPS 144 students */ public class TimeOfDay { // symbolic constants // ------------------ public static final int HOURS_PER_DAY = 24; public static final int MINUTES_PER_HOUR = 60; // instance variables // ------------------ private int hour; // # of complete hours to have elapsed since midnight private int minute; // # of complte minutes to have elapsed since the // beginning of current hour. // class invariant: // 0 <= hour < HOURS_PER_DAY and 0 <= minute < MINUTES_PER_HOUR // constructors // ------------ /* Initializes this TimeOfDay object to represent the time of day ** described by the two parameters, which represent, respectively, the ** # of hours since midnight and the # minutes since beginning of ** current hour. */ public TimeOfDay(int h, int m) { this.hour = h; this.minute = m; } /* Initializes this TimeOfDay object to represent midnight. */ public TimeOfDay() { this(0,0); // invoke the constructor above and pass to it a pair of 0's } // observers // --------- /* Reports whether or not the time of day represented by this TimeOfDay ** object falls in the AM (as opposed to the PM). The assumption is that ** the first half of the day is in the AM and that the PM begins at the ** day's midpoint. */ public boolean isAM() { return this.hour < HOURS_PER_DAY / 2; // overly-verbose alternative: //if (hour < HOURS_PER_DAY / 2) { return true; } //else { return false; } } /* Returns a description of the time of day represented by this ** TimeOfDay object, using 24-hour notation. ** Examples: "5:34", "17:06" */ public String toString() { // Call twoDigitImageOf() to produce a string describing the number of // minutes since the beginning of the current hour, including a leading // zero if necessary. String minuteStr = twoDigitImageOf(this.minute); return this.hour + (':' + minuteStr); } //RWM: Using an instance of the class java.text.DecimalFormat (from // Java's "standard library"), rather than the twoDigitImageOf() // method, would probably be considered a better approach. /* Returns a value indicating the relationship (either "earlier than", ** "equal to", or "later than") between the times of day represented ** by this TimeOfDay object and the given one (t). ** "Earlier than" is indicated by a negative return value; ** "equal to" is indicated by a return value of zero; ** "later than" is indicated by a positive return value. */ public int compareTo(TimeOfDay t) { return 57; // STUB } /* Reports whether or not this TimeOfDay object represents a time of day ** that is earlier in the day than the time of day represented by the ** given TimeOfDay object (t). */ public boolean isEarlierThan(TimeOfDay t) { if (this.hour < t.hour) { return true; } else if (this.hour > t.hour) { return false; } else { // The 'hour' values of the two objects are the same, // so the result is determined by their 'minute' values. return this.minute < t.minute; } } // mutators // -------- /* Makes this TimeOfDay object identical to the given one. */ public void setTo(TimeOfDay t) { this.hour = t.hour; this.minute = t.minute; } /* Changes the state of this TimeOfDay object so that the time of day that ** it represents has advanced by the given numbers of hours and minutes. */ public void advanceBy(int h, int m) { // Translate h hours and m minutes into a number of minutes; int minutesForward = (MINUTES_PER_HOUR * h) + m; // Advance by that many minutes, one at a time. for (int i=0; i != minutesForward; i++) { this.advanceByOneMinute(); } } /* Changes the state of this TimeOfDay object so that the time of day that ** it represents has decreased (i.e., gone backwards in time) by the given ** numbers of hours and minutes. */ public void decreaseBy(int h, int m) { // STUB } // utility methods // --------------- /* Returns the "String image" of the given integer, with a leading zero ** included if it is less than 10. For example, 45 maps to "45" and ** 3 maps to "03". ** pre: k >= 0 */ private String twoDigitImageOf(int k) { String prefix; // Make prefix equal to either "0" or "" (the empty string) according // to whether or not, respectively, k is less than 10. if (k < 10) { prefix = "0"; } else { prefix = ""; } return prefix + k; } //RWM: Using Java's "ternary operator", the if-else statement above // could have been written as // prefix = k < 10 ? "0" : ""; // /* Changes the state of this TimeOfDay object so that the time of day that ** it represents has advanced by one minute. */ protected void advanceByOneMinute() { minute = minute + 1; if (minute == MINUTES_PER_HOUR) { minute = 0; // roll over to zero and advance the hour hour = hour + 1; if (hour == HOURS_PER_DAY) { hour = 0; // roll over to zero } } } /* Changes the state of this TimeOfDay object so that the time of day that ** it represents has decreased (i.e., gone backwards) by one minute. */ protected void decreaseByOneMinute() { // STUB } }