Question
Part 3 - An Alarm Clock In this part you are adding a new class AlarmClock that will use your Alarm class, which uses the
Part 3 - An Alarm Clock In this part you are adding a new class AlarmClock that will use your Alarm class, which uses the ClockDisplay12 class, which in turn uses NumberDisplay. You will not be changing Alarm, ClockDisplay12 or NumberDisplay, just adding AlarmClock. An AlarmClock object is made up of a ClockDisplay12 object, representing the current time, and an Alarm object represting the alarm (time and whether or not it's set). The AlarmClock class has the following methods: o A default constructor (no parameters) that sets the clock to midnight, the alarm to midnight, and the alarm off. o A constructor with 7 parameters: The hours, minutes, String for am or pm, for the time, the hours, minutes, String for am or pm, for the alarm time, and a boolean indicating if the alarm is on or not. o setTime with three parameters (hours, minutes, am/pm String) that sets the clock time. o setAlarmTime with three parameters (hours, minutes, am/pm String) that sets the alarm time. o clockTick that makes the clock tick (moves the minutes ahead by 1), and, if appropriate, rings the alarm, and turns it off. We simulate ringing the alarm by outputting "RING RING RING" to the console (i.e. use System.out.println()). o setAlarm turns the alarm on. o unsetAlarm turns the alarm off. o getTime returns a String representing the current clock time. o getAlarmTime returns a String representing the current alarm time. o isAlarmSet returns true if the alarm is set, false otherwise. Note that many of these methods will invoke methods in Alarm and in ClockDisplay12. Make sure that you have Javadoc comments in your code. Test the AlarmClock class thoroughly.
Please don't change the code in ClockDisplay12 Alarm or NumberDisplay.
ClockDisplay12:
public class ClockDisplay12 { private NumberDisplay hours; private NumberDisplay minutes; private String displayString; // simulates the actual display private final String AM = "a.m"; // final variable for AM private final String PM = "p.m"; // final variable for PM private String current; // variable storing the current state /** * Constructor for ClockDisplay objects. This constructor * creates a new clock set at 12:00. */ public ClockDisplay12() { hours = new NumberDisplay(12); minutes = new NumberDisplay(60); current = AM; // making the current state to AM in default updateDisplay(); } /** * Constructor for ClockDisplay objects. This constructor * creates a new clock set at the time specified by the * parameters. */ public ClockDisplay12(int hour, int minute, String amPm) { hours = new NumberDisplay(12); minutes = new NumberDisplay(60); // making the current state according to the given state if(amPm.equals(AM)) { current = AM; }else { current = PM; } setTime(hour, minute); } /** * This method should get called once every minute - it makes * the clock display go one minute forward. */ public void timeTick() { minutes.increment(); // changing the current variable when minute is rolled over and hour is 0 if(minutes.getValue() == 0) { // it just rolled over! hours.increment(); if(Integer.parseInt(hours.getDisplayValue()) == 0) { if(current == AM) { current = PM; }else { current = AM; } } } updateDisplay(); } /** * Set the time of the display to the specified hour and * minute. */ public void setTime(int hour, int minute) { hours.setValue(hour); minutes.setValue(minute); updateDisplay(); } /** * Return the current time of this display in the format HH:MM. */ public String getTime() { return displayString; } /** * Update the internal string that represents the display. */ private void updateDisplay() { // changing the value of hour if hour to 12 if hour is 0 String hour = hours.getDisplayValue(); if(Integer.parseInt(hour) == 0) { hour = "12"; } displayString = hour + ":" + minutes.getDisplayValue() + " " + current; System.out.println(displayString); } } NumberDisplay: class NumberDisplay { private int limit; private int value; /** * Constructor for objects of class NumberDisplay. * Set the limit at which the display rolls over. */ public NumberDisplay(int rollOverLimit) { limit = rollOverLimit; value = 0; } /** * Return the current value. */ public int getValue() { return value; } /** * Return the display value (that is, the current value as a two-digit * String. If the value is less than ten, it will be padded with a leading * zero). */ public String getDisplayValue() { if(value < 10) { return "0" + value; } else { return "" + value; } } /** * Set the value of the display to the new specified value. If the new * value is less than zero or over the limit, do nothing. */ public void setValue(int replacementValue) { if((replacementValue >= 0) && (replacementValue < limit)) { value = replacementValue; } } /** * Increment the display value by one, rolling over to zero if the * limit is reached. */ public void increment() { value = (value + 1) % limit; } }
class Alarm {
private ClockDisplay12 cd12; private boolean alarmSet; /** * Constructor for Alarm. * sets time to midnight and alarm to false */ Alarm() { cd12 = new ClockDisplay12(); cd12.setTime(0,0); alarmSet = false; } /** * Parameterized Constructor for Alarm. * sets time to specified time and alarm to specified flag */ Alarm(int hour,int minute,String amPm,boolean alarmOn) { cd12 = new ClockDisplay12(hour,minute,amPm); cd12.setTime(hour, minute); alarmSet = alarmOn; } /** * sets time to specified time */ void setTime(int hours,int minutes,String amPm) { cd12.setTime(hours, minutes); } /** * Turns on the alarm flag */ void turnOn() {alarmSet = true;} /** * Turns off the alarm flag */ void turnOff() {alarmSet = false;} /** * Returns String representation of alarm time */ String getTime() { return cd12.getTime(); } /** * returns true if alarm flag is set false otherwise */ boolean isSet() {return alarmSet;} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started