Question: JAVA OOP Add an alarm feature to the Clock class from Assignment #17. When setAlarm(hours, minutes) is called, the clock stores the alarm. When you
JAVA OOP
Add an alarm feature to the Clock class from Assignment #17. When setAlarm(hours, minutes) is called, the clock stores the alarm. When you call getTime, and the alarm time has been reached or exceeded, return the time concatenated with the string "Alarm!" and then clear the alarm. Make sure the alarm works with objects of the subclass WorldClock.
Also add a toString method to return the current time, and if an alarm is set, the alarm time also.
The code for class Clock and WorldClock
**Class Clock**
public class Clock { //declares our super class clock has subclass WorldClock
public Clock() { //declares our constructor for clock
}
public int getMinute() { //declaring our method for getMinute
String s = new java.util.Date().toString(); //sets s as a new object to use with the substring method
return Integer.parseInt(s.substring(14, 16)); //returns the integer for minute
}
public int getHour() {
String s = new java.util.Date().toString(); //sets s as a new object to use with the substring method
return Integer.parseInt(s.substring(11, 13)); //returns the integer for hour
}
public String getTime() { //declaring the method to return our time
return getHour() + ":" + getMinute(); //returns our methods getHour() and getMinute() by calling them in the hour:minute format
}
}
**Subclass WorldClock**
public int getHour() { //declaring getHour method to override our super class method
int hour = super.getHour() + diff; //declares int hour and adds offSet value to hour
if (hour > 23) { //if statement for 24 hour clock, 0-23 is the range, if hour is over 23 while (hour > 23) { //while hour's value is over 23 do this
hour = 23 - hour; //set hour equal to 23 - hour's value
}
}
if (hour < 0) { //if statement for hour's value is less than 0
while (hour < 0) { //while hour's value is less than 0 do this
hour = 23 + hour; //set hour equal to 23 + hour's value }
}
return hour; //our return statement for hour in the getHour method }
public static void main(String[] args) { //main method declaration in our subclass WorldClock
Clock c = new Clock(); //creating a new Clock object called c no parameters //Clock objects are still usable even though this is a subclass //this object only uses the super methods
System.out.printf("%02d:%02d ", c.getHour(), c.getMinute()); //the formatting to print object c's hours and minutes
WorldClock wc = new WorldClock(-2); //the -2 as the parameter is the difference or time offset //creating a new WorldClock object called wc //WorldClock objects can use both super and sub class methods
System.out.println(wc.getTime()); //printing the time using getTime method
}
}
My results:
(ran from WorldClock's main method)
run: 21:32 19:32 BUILD SUCCESSFUL (total time: 0 seconds)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
