CS 161 Assignment #5

Due Wednesday, February 28th by 11:59pm
(Not accepted after 3/1)

Introduction

The word is out! Your OrcaCard class became an overnight sensation. 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 OrcaCard 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: Download and modify ClockAssignment instead of creating a new project via BlueJ or using the code from the book. The project contains the same two classes as the Clock project from the book that we looked at in class. As you'll recall, the book's Clock project represents a 24-hour clock that displays hours and minutes. It uses two instances of the 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: 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 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)

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:

Grading

This assignment will be graded out of a total of 80 points.

Submitting

Before submitting, make sure your code compiles and is commented properly, and test each of your methods thoroughly. When you're convinced that it's ready to go, submit the assignment using BlueJ's submission tool as described here.


Brad Richards, 2024