import java.util.Scanner; /* Java application having as its purpose to test the features of the ** TimeOfDay class. ** ** Author: R. McCloskey */ public class ToD_Tester { private static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { TimeOfDay t = getToD("Specify a time of day ..."); display("Initial time is ", t); System.out.println("\nTesting advanceOneMinute()..."); testAdvanceOneMinute(t, 7); System.out.println("\n\nTesting retreatOneMinute()..."); testRetreatOneMinute(t, 7); System.out.println("\n\nTesting toString24()..."); System.out.println("In 24-hour mode, time is " + t.toString24()); System.out.println("\n\nTesting isBetween()..."); testIsBetween(t); System.out.println("\n\nTesting the setMode methods..."); testSetMode(t); } /* Prints the given string (str) followed by the toString() and ** toString24() images of the given TimeOfDay object. */ private static void display(String str, TimeOfDay tod) { System.out.print(str); System.out.print(tod); System.out.print(" (" + tod.toString24() + " in 24-hour mode)"); System.out.println(); } /* Prompts user to enter input describing a time of day and returns ** the TimeOfDay object that represents that time of day. */ private static TimeOfDay getToD(String prompt) { System.out.println(prompt); int amOrPm = getInt(" Enter 1 for AM, any other number for PM: "); int h = getInt(" Enter hour (in range 1..12): "); int m = getInt(" Enter minute (in range 0..59): "); return new TimeOfDay(h, m, amOrPm != 1); } /* Invokes the advanceOneMinute() method on the given TimeOfDay object the ** specified number (k) of times. After each time, the object is displayed. */ private static void testAdvanceOneMinute(TimeOfDay tod, int k) { for (int i = 1; i <= k; i++) { tod.advanceOneMinute(); display("After calling advanceOneMinute(), time is ", tod); } } /* Invokes the retreatOneMinute() method on the given TimeOfDay object the ** specified number (k) of times. After each time, the object is displayed. */ private static void testRetreatOneMinute(TimeOfDay tod, int k) { for (int i = 1; i <= k; i++) { tod.retreatOneMinute(); display("After calling retreatOneMinute(), time is ", tod); } } /* Tests the isBetween() method by having the user specify two "new" ** times of day and then calling isBetween() to compare the three ** times of day in every possible way. */ private static void testIsBetween(TimeOfDay tod) { TimeOfDay t2 = getToD("Specify a 2nd time of day..."); System.out.println(); TimeOfDay t3 = getToD("Specify a 3rd time of day..."); System.out.println(); testIsBetweenAux(tod, t2, t3); testIsBetweenAux(tod, t3, t2); testIsBetweenAux(t2, t3, tod); testIsBetweenAux(t2, tod, t3); testIsBetweenAux(t3, t2, tod); testIsBetweenAux(t3, tod, t2); } /* Reports the result of the call time.isBetween(start, stop). */ private static void testIsBetweenAux(TimeOfDay time, TimeOfDay start, TimeOfDay stop) { System.out.print("Is " + time + " between " + start + " and " + stop + "?"); if (time.isBetween(start, stop)) { System.out.println(" YES"); } else { System.out.println(" NO"); } } private static void testSetMode(TimeOfDay tod) { tod.setTo24Mode(); System.out.println("Setting time to 24-hour mode..."); System.out.println("now toString() yields " + tod); tod.setTo12Mode(); System.out.println("\nSetting time back to 12-hour mode..."); System.out.println("now toString() yields " + tod); } /* Displays the given prompt and returns the integer value that is entered ** in response at the keyboard. If the response is not an integer, the ** prompt is repeated until such time as a valid response is given. */ private static int getInt(String prompt) { int result = -1; boolean keepGoing = true; while (keepGoing) { System.out.print(prompt); try { result = keyboard.nextInt(); keepGoing = false; } catch (Exception e) { System.out.println("ERROR: Invalid non-integer response; try again\n"); } } return result; } }