CS 161 Assignment #3

Due Monday, September 28th by 11:59pm
(Not accepted after October 2nd)

Introduction

The word is out! Your Calculator class became an overnight sensation, and has been downloaded from your website approximately three billion times. A copy found its way to a local startup company that figures they can make a fortune on web-based digital alarm clocks. They were so impressed with your calculator that they want to pay you large sums of money to help them with a little problem: They need someone to write a Java class that models a clock display — one that shows hours, minutes, and seconds, and that can print an alarm message if the time hits a specified value. You figure it should be pretty easy to extend the clock display project from the "Objects First with Java" textbook, so you sign the contract and start writing Java code...

The Assignment

I've created a new project to get you started. You must download and modify UPS_Clock instead of creating a new project via BlueJ or using the code from the book, otherwise you won't be able to submit the assignment when you're finished. As shown in the diagram below, it contains the same two classes as the Clock project from the book:

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:

If you only made these changes, you should have a 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:
> 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)
Your 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: Some sample interactions are shown below. (To keep things tidy, the 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 since the alarm is specified for a given hour and minute, alarm notices are printed for each tick after the hour and minute are hit, and would continue to be generated for each new second until the minute rolls over (in this case to "09:01:00").
> 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)
> clock.timeTick();
Alarm!
> clock.getTime()
 "09:00:00"   (String)
> clock.timeTick();
Alarm!
> clock.getTime()
 "09:00:01"   (String)
> clock.timeTick();
Alarm!
> clock.clearAlarm();
> clock.timeTick();
> clock.timeTick();

Extending the Assignment

There's no extra credit, but if you're looking for more of a challenge, consider implementing one or more of the following features:

Submitting

Before submitting, test each of your methods thoroughly and double check for comments above each method. When you're convinced it's ready to go, submit the project just like you did in the first lab. (The lab is still online, and you can refer back to it if necessary.) If the submit menu item is greyed out, save this file to your desktop. Quit BlueJ, move "submission.defs" into your project folder, restart BlueJ, and try submitting again.


Brad Richards, 2009