/* An instance of this class represents a time of day, precise to the ** minute. Example: 5:34PM. ** ** Author: R. McCloskey ** Date: Feb. 2026 */ public class TimeOfDay { // class constants // --------------- protected int HOUR_CEILING = 12; protected int MINUTES_PER_HOUR = 60; protected int MINUTES_PER_HALF_DAY = HOUR_CEILING * MINUTES_PER_HOUR; // instance variables // ------------------ protected boolean isAM; // true if this time is before noon protected int hour; // # hours since either midnight or noon, // depending upon the value of isAM protected int minute; // # minutes since beginning of this hour // class invariant: 0 <= hour < HOUR_CEILING // class invariant: 0 <= minute < MINUTES_PER_HOUR // constructor // ----------- /* Initializes this time of day to be that described by the ** three parameters. ** pre: 0 <= h < 12 && 0 <= m < 60 */ public TimeOfDay(int h, int m, boolean AM) { this.isAM = AM; this.hour = h; this.minute = m; } // observers // --------- @Override public String toString() { String AM_PM_Str = isAM ? "AM" : "PM"; return String.format("%d:%02d%s", hour, minute, AM_PM_Str); } /* Returns how many minutes past midnight is this time of day. */ public int minutesSinceMidnight() { // compute # of hours since either midnight (if AM) or noon (if PM) int hoursPastThreshold = hour % HOUR_CEILING; // compute # of hours since midnight int hoursPastMidnight; if (isAM) { hoursPastMidnight = hoursPastThreshold; } else { hoursPastMidnight = hoursPastThreshold + HOUR_CEILING; } return hoursPastMidnight * MINUTES_PER_HOUR + minute; } // mutators (none) // -------- }