/* An instance of this class represents a time of day, precise to a ** particular minute. */ public class TimeOfDay { // symbolic constants // ------------------ private static final int HOURS_PER_DAY = 24; private static final int MINUTES_PER_HOUR = 60; // instance variables // ------------------ private int hour; // # of full hours since midnight private int minute; // # of full minutes since beginning of current hour // class invariants: // 0 <= hour < HOURS_PER_DAY // 0 <= minute < MINUTES_PER_HOUR // constructor // ----------- /* Initializes this object to the specified hour and minute. ** pre-condition: 0 <= h < HOURS_PER_DAY && 0 <= m < MINUTES_PER_HOUR */ public TimeOfDay(int h, int m) { this.setTo(h,m); //hours = h; minute = m; } // observers/queries // ------------------ /* With respect to this time of day, returns the number of full hours ** that have passed since midnight. */ public int hourOf() { return this.hour; } /* With respect to this time of day, returns the number of full minutes ** that have passed since the beginning of the current hour. */ public int minuteOf() { return this.minute; } /* Returns true iff this time of day is earlier in the day than is the ** given time of day (t). */ public boolean isEarlierThan(TimeOfDay t) { return this.compareTo(t) < 0; } /* Returns: ** --a negative number if THIS time of day is earlier in the day than t ** --zero if THIS and t are the same time of day ** --a positive number if THIS time of day is later in the day than t */ public int compareTo(TimeOfDay t) { if (this.hour < t.hour) { return -1; } else if (this.hour > t.hour) { return 1; } else { // this.hour == t.hour if (this.minute < t.minute) { return -1; } else if (this.minute > t.minute) { return 1; } else { return 0; } } // alernative method body: // int hourDiff = this.hour - t.hour; // if (hourDiff != 0) { return hourDiff; } // else { return this.minute - t.minute; } } /* Returns a string describing this time of day in 12-hour mode. ** Examples: "5:16AM", "12:05PM" */ public String toString() { if (this.hour == 0) { return "12:" + minute + "AM"; } else if (0 < this.hour && hour < 12) { return hour + ":" + minute + "AM"; } else if (this.hour == 12) { return "12:" + minute + "PM"; } else { // hour is in range 13..23 return (hour - 12) + ":" + minute + "PM"; } } // mutators // -------- /* Sets this time of day to that specified, where h indicates ** hours since midnight and m indicates minutes since the ** beginning of the current hour. ** pre-condition: 0 <= h < HOURS_PER_DAY && 0 <= h < MINUTES_PER_HOUR ** exception: ??? */ public void setTo(int h, int m) { this.hour = h; this.minute = m; } /* Moves this time of day forward by one minute. Note that going ** forward from 11:59pm results in "wrapping around" to the first ** minute of the next day, midnight (12:00am). */ public void goForwardByOneMinute() { if (hour+1 == HOURS_PER_DAY && minute+1 == MINUTES_PER_HOUR) { //hour = 0; minute = 0; setTo(0,0); } else { // time is not 11:59pm if (minute+1 == MINUTES_PER_HOUR) { //minute = 0; hour = hour+1; setTo(hour+1, 0); } else { // "normal" case (i.e., no wraparound occurs) //minute = minute + 1; setTo(hour, minute+1); } } } /* Moves this time of day backward by one minute. Note that going ** backwards from midnight (12:00am) results in "wrapping around" ** the last minute of the previous day (11:59pm). */ public void goBackwardByOneMinute() { } }