Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 2 - An Alarm In this part you are adding a new class Alarm that will use your ClockDisplay12 class, which in turn uses

Part 2 - An Alarm

In this part you are adding a new class Alarm that will use your ClockDisplay12 class, which in turn uses NumberDisplay.

You will not be changing ClockDisplay12 or NumberDisplay, just adding Alarm.

An Alarm object is made up of a ClockDisplay12 object and a boolean flag indicating whether or not the alarm is set (true means it is set; false means it isn't set).

The Alarm class has the following methods:

o A default constructor (no parameters) that sets the clock to midnight and the alarm off.

o A constructor with 4 parameters: The hours, minutes, String for am or pm, and a boolean indicating if the alarm is on or not.

o setTime with three parameters (hours, minutes, am/pm String) that sets the alarm time.

o turnOn turns the alarm on.

o turnOff turns the alarm off.

o getTime returns a String representing the current alarm time.

o isSet returns true if the alarm is set, false otherwise.

Note that many of these methods will invoke methods in ClockDisplay12.

Make sure that you have Javadoc comments in your code.

Test the Alarm class thoroughly.

Please don't change the code in ClockDisplay12 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; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions