CMPU 102 Assignment 2: Implementing a Digital Clock Due on Friday, Feb 17th, by midnight. Problem Specification: Implement a European-style 24-hour display that shows the time from 00:00 (midnight) to 23:59 (one minute before midnight). Programming Assignment: For this assignment, you will implement a digital clock. The clock does not have to be very fancy, just a two-digit, colon, two-digit output is enough for this digital display (e.g., 13:09 (one oh nine pm on 12-hour clocks)). A digital display can be viewed abstactly as two separate two-digit displays, one for the hours and one for the minutes. The hours display starts at 00, increases by one each 60 minutes, and rolls back to zero after reaching its upper limit of 24. The pair of digits representing the minutes display starts at 00, increases by one each minute and rolls back after reaching its upper limit of 60. We can view the hours and minutes displays as objects of the same type that can display values from 00 to a specified upper limit. The value of each display object can be incremented, but if the value reaches the upper limit, it resets to zero. Part I: Write a class to produce 2-digit output (DoubleDigit) For the first part of this assignment, implement an object-creating class with file name DoubleDigit.java and class name DoubleDigit. The DoubleDigit class needs to maintain two data fields: an int called upperLimit to hold the highest value allowed for this object and an int called displayValue to hold the current int value of the display. Note that an object-making class is one that usually has at least one constructor and non-static methods and fields. When DoubleDigit objects are created, the upperLimit of the display should be initialized and the displayValue should be set to begin at 0 in a one- parameter constructor. The DoubleDigit class should contain the following constructor and instance methods: 1. A one-parameter constructor should initialize the upperLimit to be equal to the int argument of the constructor and should set displayValue to 0. 2. A one-parameter setDisplayValue method consumes an int and sets the displayValue field to the int parameter value if that value is an integer between 0 and the upperLimit. This should return void. The displayValue field should be set to the value of the int parameter if the int is in the range 0...upperLimit. It should do nothing if the user enters a number that is out of range 0...upperLimit. 3. A zero-parameter method called incrementValue() to increment the displayValue by one, rolling over to zero if displayValue == upperLimit, and returning void. 4. A zero-parameter getDisplayValue method to return the current display value as a two-digit String (so if the current display value is less than 10, it will be padded on the left with a 0). To get a value in this format, you can use the DecimalFormat class of the java.text package. ----- Experiment with commands below in Interactions window ------ To experiment with the DecimalFormat class in the interactions window of DrJava, create a DecimalFormat object called df by calling the constructor, as follows: java.text.DecimalFormat df = new java.text.DecimalFormat("00"); The "00" string tells the df object to output 2-character Strings and to pad the left side with a 0 for values less than 10. In the interactions window, after the declaration and instantiation of the df object, call the format method on the DecimalFormat object to produce a 2-character String and learn how the format method works. String x = df.format(5); // should return the String "05" String y = df.format(0); // should return the String "00" String z = df.format(53); // should return the String "53" ------------------------------------------------------------------------- Part II: Write a tester class TestDoubleDigits for the DoubleDigits class Write a short tester class with a main method that creates a DoubleDigit object and calls each method defined in the DoubleDigit class on that object. This is a very important rule of program implementation: Every class should be tested independently. You should submit this tester class along with your other program files even though it is not technically needed for the final product. Part III: Write a class called ClockDisplay that displays output like a digital clock For this part of the assignment, create an instantiable (i.e. object creating) class that includes the following instance variables: DoubleDigit hours DoubleDigit minutes The class should have constants that are final and static: static final int HOURS_IN_DAY = 24; static final int MINUTES_IN_HOUR = 60; The ClockDisplay class should include the following constructor and instance methods: 1. A zero-parameter constructor that creates DoubleDigit objects hours and minutes, with upper limits of HOURS_IN_DAY and MINUTES_IN_HOUR, respectively. 2. A zero-parameter tick method that increments (adds 1 to) the minutes display, checks for roll-over, and increments the hours display if minutes is 0. This method should have a void return. Its action should be to change the value of the minutes (and possibly hours) fields in the ClockDisplay object. 3. A two-parameter setTime method that takes as input a number of hours and a number of minutes and sets the hours and minutes displays to the specified int values. This is a method that returns void but has the effect of changing the value of the hours and minutes fields. You can assume that the user will enter values within the lower and upper limits for hours and minutes. These limits will be enforced in the StartClockDisplay class. 4. A 0-parameter getTime method that returns the current time as a String in the format: "04:30" Part IV: The main program - StartClockDisplay.java The tester class for the ClockDisplay class, StartClockDisplay, should do the following IN ITS MAIN METHOD: 1. Create a ClockDisplay object local to the main method called clock. 2. do-while (menu choice not equal to Quit (4)): NOTE: The menu should print all in one JOptionPane, so put the whole menu in a single String argument to JOptionPane.showInputDialog. Embed "/n"s for newlines and break the string at each "/n". Example: "Please choose one of the\n"+ "following menu options:\n\n"+ "1 - Display the time\n"+ "2 - Fast forward the time\n"+ "3 - Reset the time\n"+ "4 - Quit\n" The String read from this prompt should be converted to an int. One possible way to check for the correct action to take if the entry is 1...4 is to use a switch statement inside the do-while: If menu choice is 1, the program action should be to call the getTime method of the clock object and display the result. This is the only menu choice that should display the time. If menu choice is 2, prompt for number of minutes to advance the clock. Assume the user will enter non-negative integer values. Advance the clock by repeatedly calling the tick method of the ClockDisplay class (in a loop). If menu choice is 3, prompt for the hour to reset the current hour and then prompt for the minutes to reset the appropriate minutes values of the ClockDisplay object. Check each value after it is entered and, while the entry is not within the hours limit of 0...23 or the minutes limit of 0...59, keep prompting for a value that is within the limits. If the menu choice is 4, the program should break out of the switch and also drop out of the do-while. The default case in the switch should be for invalid options, just break. 4. After the end of the do-while, output a graceful ending message and stop. A call to System.exit(0); will cause the window to close and will halt execution of the program. System.exit(0); should be the last line in the main method if you choose to use it. If DrJava complains, tell it not to do so again. Be sure to test all menu options in the StartClockDisplay class and hand-check your results. To make step 2 less complex, assume the user will enter only whole number values. You should use JOptionPane class methods to prompt for input and to display the output. What you should hand in: When your program compiles and runs correctly, submit the hw2 folder as a zipped file containing 4 java files and 4 java classes. Each .java file should include a descriptive header comment and should have intuitively named variables. It should contain comments explaining the code, in particular, the purpose of every method. Each class should have a header comment with your name and the purpose of the program. Each method in your classes should have header comments explaining the input, output, and purpose.