public final class SimpleTime5 { final int hour, minute; static final int HOURS_IN_DAY = 24; static final int MINUTES_IN_HOUR = 60; static final int MINUTES_IN_DAY = HOURS_IN_DAY*MINUTES_IN_HOUR; SimpleTime5 (final int h) { this (h, 0); } SimpleTime5 (final int h, final int m) { assert 0<=h && h<HOURS_IN_DAY; assert 0<=m && m<MINUTES_IN_HOUR; this.hour=h; this.minute=m; } @java.lang.Override public String toString () { if (hour==0 && minute==0) { return ("midnight"); } else if (hour==12 && minute==0) { return ("noon"); } else { return String.format ("%02d:%02d", hour, minute); } } /* Design for immutabililty. */ public SimpleTime5 add (final int m) { // Wrap around to the next day. final int totalMinutes = Math.floorMod (MINUTES_IN_HOUR*hour + minute + m, MINUTES_IN_DAY); assert 0<=totalMinutes && totalMinutes<MINUTES_IN_DAY; // Integer division truncates return new SimpleTime5 (totalMinutes/MINUTES_IN_HOUR, totalMinutes%MINUTES_IN_HOUR); } public boolean before (final SimpleTime5 t) { return ((this.hour < t.hour) || ((this.hour == t.hour) && (this.minute < t.minute))); } public static void main (final String[] args) { final SimpleTime5 time = new SimpleTime5 (9, 30); final SimpleTime5 ten = new SimpleTime5 (10); System.out.println (time.before(ten)); System.out.println (time.add(45).add(30)); System.out.println (time.before(ten)); } }