Exercise 9: Inheritance

Copy the files Clock.java and AlarmTest.java to the directory exercises/ex9 in your cloned repository. The first of these files contains a slightly modified version of the Clock class discussed in Lectures 5 & 6.

Create a new file named AlarmClock.java in the same directory. In this file, create a class named AlarmClock that simulates an alarm clock. The class should have the features shown on the UML diagram below. Note, in particular, the relationship here between AlarmClock and Clock.

UML representation of AlarmClock class

Note that the constructors of AlarmClock are for setting the time on the clock, not for setting the alarmHours and alarmMinutes fields! Those fields are given their values by calling the setAlarm() method. The values of the fields dictate the time at which the alarm should be triggered. Your implementation of setAlarm() should check the values provided for the two fields, throwing an IllegalArgumentException with a message of “invalid alarm hours” or “invalid alarm minutes”, as appropriate.

The isRinging() method should return true if the alarm is ringing, false if it is not. The alarm switches from a non-ringing state to a ringing state when the time on the alarm clock reaches the alarm time, and the alarm clock should remain in the ringing state for a total of 15 seconds.

The display() method should override the version from Clock with a new version providing visual feedback that the alarm is ringing (see below). It can use the isRinging() method to determine when this visual feedback must be provided. As a result of these changes, the output produced by calling the run() method will also change. Below is a small sample of the output generated by this method when the alarm has been set for 7:30.

07:29:58
07:29:59
07:30:00 - WAKE UP!
07:30:01 - WAKE UP!
07:30:02 - WAKE UP!

The output should revert to showing only the time after the alarm has rung for a total of 15 seconds. You can check whether your code duplicates this behaviour using the AlarmTest program that we have provided.

When you are confident that AlarmClock is working as required, go to Submit My WorkExercises 6-10Exercise 9 in Minerva to submit your AlarmClock.java file (and only that file) for grading. You can improve your solution and resubmit as many times as you like until the deadline. Also, don’t forget to commit all your work to your Git repository and push those commits up to GitLab.