// Simple class to represent a period of time in hours, minutes, and seconds. // Demonstrates the Comparable interface. public class TimePeriod implements Comparable { private int hours; private int minutes; private int seconds; public TimePeriod(int seconds) { this.hours = seconds / 3600; this.minutes = (seconds - this.hours * 3600) / 60; this.seconds = seconds % 60; } public TimePeriod(int minutes, int seconds) { this(minutes * 60 + seconds); } public TimePeriod(int hours, int minutes, int seconds) { this(hours * 3600 + minutes * 60 + seconds); } public String toString() { return String.format("%02d:%02d:%02d", this.hours, this.minutes, this.seconds); } // a.compareTo(b) // if a is "less than" b, return a negative number // if a is "greater than" b, return a positive number // if a is "equal to" b, return 0 public int compareTo(TimePeriod other) { if (this.hours != other.hours) { return this.hours - other.hours; } else if (this.minutes != other.minutes) { return this.minutes - other.minutes; } else { return this.seconds - other.seconds; } } }