Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is one question: Create a class that simulates an alarm clock. In this clas

ID: 645476 • Letter: T

Question

This is one question:

Create a class that simulates an alarm clock. In this class you should:

? Store time in hours, minutes, and seconds. Note if time is AM or PM. (Hint: You should have

separate private members for the alarm and the clock. Do not forget to have a character

variable representing AM or PM.)

? Initialize the clock to a specified time.

? Allow the clock to increment to the next second. (Hint: You need to take into account things

like if the clock's time is 11:59:59 AM and you increment by a second, the time will be

12:00:00 PM. You may need to consider some iterated if statements.)

? Set the alarm and have the alarm print out "WAKE UP" when the set time is reached. (Hint:

You may wish to create a private function that provides the wished-for printout when the alarm

time is reached and the alarm clock is on.)

? Display the present time.

? Use the class in a program that uses the functions requiring displaying of time and setting of

the alarm.

Include 2 constructors. One constructor should be the default constructor that will initialize the object

to 12:00:00 AM. The second constructor should take parameters for hours, minutes, seconds, and

AM/PM. Both constructors will provide the private members with the time. In addition, have both

constructors set the alarm clock as off. (You will need a Boolean variable that determines whether the

alarm is on or off). The function or method you use to set the alarm will set the alarm on.

Please include a working output. Thanks

Explanation / Answer

/* class AlarmClock * * This class contains all of the information to implement an alarm clock * It stores up to two alarms. You may set the alarms, check whether the * alarm as gone off, update the time, etc. */ public class AlarmClock { // these are instance variables - each alarm clock has its // own copy of these variables // current time private int currentHour; private int currentMinute; private int currentSecond; // time of the alarm(s) private int[] alarmHour; private int[] alarmMinute; // whether alarm is on or off private boolean[] alarmOn; // this is the constructor - this is called when "new" // is called, when the object is first created // this contains code to initialize all variables in object // this is a special method - it has no return value (and no void) public AlarmClock() { currentHour = currentMinute = currentSecond = alarmHour = new int[2]; alarmMinute = new int[2]; alarmOn = new boolean[2]; // set both alarms to off } // now we put all of the methods // each method that we want code from outside the class to call is public // each method that we only want code from inside the class to call is private /* setAlarm * inputs: alarm number, hour, minute * outputs: none * purpose: sets one of the alarms to the hour and minute specified * It also turns on the alarm */ public void setAlarm(int alarm, int hour, int minute) { // make sure the alarm number is valid // set that alarm // turn on alarm } /* turnOffAlarm * inputs: the alarm number * outputs: none * purpose: This turns off an alarm. */ public void turnOffAlarm(int alarm) { // make sure the alarm number is valid // turn off alarm } /* turnOnAlarm * inputs: the alarm number * outputs: none * purpose: This turns on an alarm. This assumes the alarm time has * already been set. */ public void turnOnAlarm(int alarm) { // make sure the alarm number is valid // turn on alarm } /* setTime * inputs: hour, minute, second * outputs: none * purpose: This updates the current time to the values * passed in. */ public void setTime(int hour, int minute, int second) { currentHour = hour; currentMinute = minute; currentSecond = second; } /* tick * inputs: none * outputs: none * purpose: This gets called each second. It updates the current time * of the clock and sets of the alarm if necessary */ public void tick() { // print out tick, then tock, then tick, etc. if (seconds % 2 == 0) System.out.println("Tick"); else System.out.println("Tock"); // increment the time // check to see if an alarm should go off } /* soundAlarm * inputs: none * outputs: none * purpose: this gets called by tick when the alarm goes off * It sounds an alarm */ private void soundAlarm() { System.out.println("Alarm!!**************************"); } } // end of class AlarmClock

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote