NumberDisplay
class to represent the time — one for hours and one for minutes. On this assignment you'll use a third NumberDisplay
instance for seconds and modify the existing code as necessary so that the tick
method increment's the clock's time by one second rather than one minute. You'll also add the ability to set an alarm time, and print a message when the clock reaches the specified time. (I suggest that you start by extending the current ClockDisplay
code so that it keeps track of the time in hours, minutes, and seconds, and wait to implement the alarm until you get the basic clock working.) Your code must meet the following requirements for full credit:
setTime
method should take three arguments as well, and set the clock to the specified hour, minute, and second.
updateDisplay
method should build a string showing the seconds as well as the hours and minutes. (See below for examples.)
timeTick
method should be changed so that it increments the seconds value each time it's called. The minutes field should increment each time the seconds field rolls over (and that might, in turn, increment the hours.)
ClockDisplay
class that behaves as shown here. The codepad is being used in the example below to create a ClockDisplay
object set to two seconds before midnight (23 hours, 59 minutes, and 58 seconds). The timeTick
method is incrementing the seconds field, and it all rolls over as expected:
Your> ClockDisplay clock = new ClockDisplay(23, 59, 58); > clock.getTime() "23:59:58" (String) > clock.timeTick(); > clock.getTime() "23:59:59" (String) > clock.timeTick(); > clock.getTime() "00:00:00" (String) > clock.timeTick(); > clock.getTime() "00:00:01" (String) > clock.setTime(0,0,59); > clock.getTime() "00:00:59" (String) > clock.timeTick(); > clock.getTime() "00:01:00" (String)
ClockDisplay
must also implement an alarm function as well. The clock should remember a specified time (hour and minute), and print an alarm message if the clock's current time hits the alarm time. For full credit, you must do the following:
setAlarm
that takes two integers as parameters, the hour and the minute (as integers) specifying the time that the alarm should go off. (You'll want to save these values for later use.) No alarms should go off unless this method has been called to set an alarm.
clearAlarm
that takes no arguments and that "clears" the current alarm. That is, no alarm should go off after calling clearAlarm
regardless of the number of times timeTick
is called. (There are multiple ways to make this work, but the easiest is probably to set the alarm hour and minute to values that will never occur.)
timeTick
method so that it prints Alarm!
to the terminal window if the time, after incrementing the number of seconds, matches the alarm hour and minute. The message should only appear once though — we don't want it going off every second after that. (See interactions below for an example of the desired behavior.)
ClockDisplay
as its input. It should initialize the current ClockDisplay
(the one whose constructor is running) so that its state is exactly like the ClockDisplay
being passed in. You can see this constructor in use in the interactions below — we create a new ClockDisplay
object called clone
that's set to the same values as our original clock.
//
). Don't forget the comments at the top of the classes either — I'm looking for more than just a sentence or two.
Alarm!
lines are shown as part of the interactions in the codepad, but they should actually appear in the terminal window since they're being printed.) Notice that the alarm only goes off once, when the clock hits 9:00, even as additional timeTick()
methods are called. Yours should do the same. Also, look carefully at the differences between my original clock
and its clone
. Asking one to tick does not change the time on the other, and yours should behave the same way. (If it doesn't, think about what's going on in your copy constructor. Draw a diagram of all of the objects and references involved.)
> ClockDisplay clock = new ClockDisplay(8,59,58); > clock.getTime() "08:59:58" (String) > clock.setAlarm(9,0); > clock.timeTick(); > clock.getTime() "08:59:59" (String) > ClockDisplay clone = new ClockDisplay(clock); > clock.timeTick(); Alarm! > clock.getTime() "09:00:00" (String) > clock.timeTick(); > clock.getTime() "09:00:01" (String) > clock.timeTick(); > clock.clearAlarm(); > clone.getTime() "08:59:59" (String) > clone.timeTick(); Alarm! > clone.getTime() "09:00:00" (String) > clock.getTime() "09:00:02" (String)
updateDisplay
method so that the display string includes the alarm time, but only if an alarm is set.
minuteTick
method that increments the minutes field without changing seconds. The hours should still increment if minutes roll over.