This is what I need to do.... Add an alarm feature to the Clock class from Assig
ID: 3840988 • Letter: T
Question
This is what I need to do.... 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. Also add a toString method to return the current time, and if an alarm is set, return the alarm time also.
This is my code
public class Clock{
public Clock() {
}
public int getHours(){
int hour = new java.util.Date().getHours();
return hour;
}
public int getMinutes(){
int minute = new java.util.Date().getMinutes();
return minute;
}
public String getTime(){
String date = "";
String hr = Integer.toString(getHours());
if (hr.length() == 1)
date = date + "0" + hr + ":";
else
date = date + hr + ":";
String min = Integer.toString(getMinutes());
if (min.length() == 1)
date = date + "0" + min;
else
date = date + min;
return date;
}
//////////////////////////////////////////////////////////
public class ClockTester {
public static void main(String[] args) {
Clock c1 = new Clock();
System.out.println("Clock: "+c1.getTime());
}
}
JAVA OOP thank you!
Explanation / Answer
This is Clock class:
package com.alarmclock;
import java.io.ObjectInputStream.GetField;
public class Clock {
int alarmHour;
int alarmMinute;
//counstructor
public Clock() {
alarmHour=0;
alarmMinute=0;
}
public int getHours(){
int hour = new java.util.Date().getHours();
return hour;
}
public int getMinutes(){
int minute = new java.util.Date().getMinutes();
return minute;
}
public String getTime(){
String date = "";
String hr = Integer.toString(getHours());
if (hr.length() == 1)
date = date + "0" + hr + ":";
else
date = date + hr + ":";
String min = Integer.toString(getMinutes());
if (min.length() == 1)
date = date + "0" + min;
else
date = date + min;
return date;
}
//setter and getters
public int getAlarmHour() {
return alarmHour;
}
public void setAlarmHour(int alarmHour) {
this.alarmHour = alarmHour;
}
public int getAlarmMinute() {
return alarmMinute;
}
public void setAlarmMinute(int alarmMinute) {
this.alarmMinute = alarmMinute;
}
public void setAlarm(int alarmH, int alarmM)
{
// make sure the alarm number is valid
if((alarmH >= 0) && (alarmH <= 24)){
alarmHour = alarmH;
}
else{
System.out.println("Fatal error: invalid alarm hour");
}
if((alarmM >= 0) && (alarmM <= 59)){
alarmMinute = alarmM;
}
else{
System.out.println("Fatal error: invalid alarm munute");
}
// set that alarm
setAlarmHour(alarmHour);
setAlarmMinute(alarmMinute);
// turn on alarm
}
public String getCurrentAlarmTime()
{
return "The alarm is set to " + alarmHour + ":" + alarmMinute ;
}
//Facilitators
public String toString()
{
return "The current time is " + getTime() + " The alarm is set to " + getAlarmHour() + ":" + getAlarmMinute();
}
public void turnOnAlarm(int alarm)
{
// make sure the alarm number is valid
// turn on alarm
soundAlarm();
}
public void turnOffAlarm(int alarm)
{
// turn off alarm
System.out.println("Alarm Off");
}
private void soundAlarm()
{
System.out.println("Alarm!!**************************");
}
}
This is driver class:
package com.alarmclock;
public class ClockTester {
public static void main(String[] args) {
Clock c1 = new Clock();
c1.setAlarm(10, 43);
System.out.println( c1.getCurrentAlarmTime());
System.out.println( c1.toString());
String str1=c1.getTime();
String str2=c1.getHours()+":"+c1.getMinutes();
while(true){
if(str1.equals(str2));{
c1.turnOnAlarm(1);
c1.turnOffAlarm(1);
break;
}
}
}
}
Output:
The alarm is set to 10:43
The current time is 10:43
The alarm is set to 10:43
Alarm!!**************************
Alarm Off
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.