Question
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,
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.
I cant get my alarm to go off??!! please help this is my code
public class Clock { int alarmHour; int alarmMinute; 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; } 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){ if((alarmH >= 0) && (alarmH <= 24)){ alarmHour = alarmH; } if((alarmM >= 0) && (alarmM <= 59)){ alarmMinute = alarmM; } } public String getCurrentAlarmTime() { return "The alarm is set to " + alarmHour + ":" + alarmMinute ; } @Override public String toString(){ return "The current time is " + getTime() + " The alarm is set to " + getAlarmHour() + ":" + getAlarmMinute(); } public void turnOnAlarm(int alarm){ soundAlarm(); } public void turnOffAlarm(int alarm){ System.out.println("Alarm Off"); } private void soundAlarm(){ System.out.println("Alarm!! "); } } /////////////////////////////////////////////////////////////////////
public class WorldClock extends Clock{ private int offset; public WorldClock(int offset){ this.offset = offset; } public int getHours(){ int hour = new java.util.Date().getHours(); hour = (hour + offset) % 24; return hour; } public int getMinutes(){ int minute = new java.util.Date().getMinutes(); return minute; } }
//////////////////////////////////////////////////////////////////
public class ClockTester { public static void main(String[] args) { Clock c1 = new Clock(); Clock c2 = new WorldClock(3); System.out.println("Clock: "+c1.getTime()); System.out.println("WorldClock: "+c2.getTime()); c1.setAlarm(06,30); System.out.println(c1); } }
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