David Chiu

Professor of Computer Science
University of Puget Sound
Thompson Hall 303 | dchiu@pugetsound.edu


home | teaching | research | people | service | faq for students | contact

CS 161 - Intro to Computer Science

Homework: Alarm Clock

Clocks are cool. At a glance, they seem like just a glorified counter, ticking seconds, minutes, and hours up by one. But when we dig into them a bit more, we realize that there’s actually a lot more detail that goes into properly implementing them. For instance, the minutes reset to 0 when they reach 60. The hours reset to 1 when they reach 13. We also need to keep track of am and pm, and that they change when the clock strikes 12.

In this project, you will put in the work to write a class of (alarm) clocks from scratch! I’ll take you through the process of identifying your instance variables, and get you started on writing constructors and methods. You will have to have a pretty good handle on integer operations and writing if-else statements.

Student Outcomes

Required Files

There are no starter files provided for this project. To create a new project, follow these instructions:

  1. Open BlueJ. It might show you the most recently opened project, but that’s okay.
  2. Click on the BlueJ menu (called File menu in Windows) on top.
  3. Select New Project...
  4. BlueJ will ask you for a project name. You can name it Clock or whatever you see fit.
  5. Then it asks you for the location to store the project. Click Choose and I’d put it along with your other projects for this class (hopefully in a Google Drive folder).
  6. Then click OK. BlueJ will automatically give you a sample class. You can remove it, and start clean.

Instructions

Alarm Function

When you’ve tested everything you’ve written so far, let’s add some new functionalities. Specifically, we want to add an alarm function.

Example Output

Here’s a sample interaction of my working clock. If you type out the commands in your BlueJ Code Pad, you should get outputs that match mine. The lines below starting with > denote output to the terminal.

Specifically, I create a clock given a time of 11:57 pm. Then I tick up 3 times and show that it is now properly showing 12:00 am (the minutes wrapped around to 00, and the hour incremented to 12; additionally, pm flipped to am.) Next, I set an alarm for 11:58 pm, which we’re already past. So I tick down twice, and the clock reaches 11:58 pm, setting off the alarm. Finally, I print off the state of the alarm to show that it is currently set. So I unset the alarm, and print off the state again.

Clock myClock = new Clock(11, 57, true);
myClock.showTime();
> 11:57 pm

myClock.tickUp();
myClock.tickUp();
myClock.tickUp();
myClock.showTime();
> 12:00 am

myClock.setAlarm(11, 58, true);
myClock.tickDown();
myClock.tickDown();
> ****** BEEP BEEP BEEP ******

myClock.showTime();
> 11:58 pm

System.out.println(myClock.isAlarmSet());
> true

myClock.unsetAlarm();
System.out.println(myClock.isAlarmSet());
> false

Optional Extensions

If you have some time to spare and want to extend your clock, try doing the following optional challenges:

Grading

This assignment will be graded out of a total of 85pts.

[5pts] Appropriate instance variables have been defined for this class.
       No local variables are defined as instance variables.

[10pts] The constructor is properly implemented and defenseive to bad inputs. Upon 
        receiving a bad hour, set the hour to 12. Set the minutes to 0 if a bad input
        is given to it.

[15pts] tickUp() moves the clock ahead by 1 minute, which may cause am/pm to flip. It may   
        also cause the hour and minutes to "wrap around" to the beginning.

[15pts] tickDown() moves the clock back by 1 minute, which may cause am/pm to flip. It may   
        also cause the hour and minutes to "wrap around" to the beginning.

[15pts] The showTime() method accepts an input and prints the current time in either 
        If either hour or minute is single digit, then a leading 0 is appended.

[10pts] The setAlarm(),  unsetAlarm(), and isAlarmSet() methods are properly implemented.

[10pts] tickUp() and tickDown() further print out ***** BEEP BEEP BEEP **** if the time now 
        matches the alarm that is set.

[5pts] You include sufficient inline and block comments to explain the logic of your methods.

Submitting Your Assignment

Follow these instructions to submit your work. You may submit as often as you’d like before the deadline. I will grade the most recent copy.

Credits

Written by David Chiu. 2023.