// a small program to exercise the named timer // Beth Katz, November 2002 public class NamedTimerPlay { // initialize timer's name and time public static NamedTimer initializeTimer(String timerName, TimeInput ti) { System.out.print("Enter time for " + timerName + " in minutes:seconds => "); ti.getNextTime( ); NamedTimer theTimer = new NamedTimer(timerName, ti.getMinutes( ), ti.getSeconds( )); System.out.println( theTimer.toString( ) ); return theTimer; } // run some time off timer t public static void runOffTime(NamedTimer t, TimeInput ti) { System.out.print("Time " + t.getName( ) + " uses in minutes:seconds? "); ti.getNextTime( ); t.subtract(ti.getMinutes( ), ti.getSeconds( )); } // print both timer's current values neatly public static void printTimers(NamedTimer white, Timer black) { System.out.println(" " + white.toString( ) + " " + black.toString( ) ); } public static void main(String[ ] args) { NamedTimer white; NamedTimer black; boolean blacksTurn = false; TimeInput ti = new TimeInput( ); white = initializeTimer("White", ti); black = initializeTimer("Black", ti); printTimers(white, black); while ( !(white.outOfTime( ) || black.outOfTime( )) ) { if (blacksTurn) { blacksTurn = false; runOffTime(black, ti); } else { blacksTurn = true; runOffTime(white, ti); } printTimers(white, black); } if (white.outOfTime( )) { System.out.println("White ran out of time"); } else { System.out.println("Black ran out of time"); } } }