// Class Clock is used to store a time // Comparable public class Clock implements Comparable { private int hours; private int minutes; // pre: minutes >= 0 and minutes < 60 // pre: hours >= 0 public Clock(int hours, int minutes) { if (minutes < 0 || minutes >= 60 || hours < 0) { throw new IllegalArgumentException(); } this.hours = hours; this.minutes = minutes; } // post: returns the integer hours for this clock public int getHours() { return hours; } // post: returns the minutes portion of this clock (not counting hours) public int getMinutes() { return minutes; } public String toString() { return hours + "h " + minutes + "m"; } public Clock add(Clock other) { int newHours = hours + other.hours; int newMinutes = minutes + other.minutes; if (newMinutes > 59) { newHours++; newMinutes -= 60; } Clock newClock = new Clock(newHours, newMinutes); return newClock; } // this < other: return number < 0 // this > other: return number > 0 // this == other: return 0 public int compareTo(Clock other) { if (hours > other.hours) { return 1; } else if (hours < other.hours) { return -1; } else { // compare minutes return minutes - other.minutes; } } }