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

This is what I need to do.... Add an alarm feature to the Clock class from Assig

ID: 3840458 • 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 int getHours(){
String s=new java.util.Date().toString();
int hour= Integer.parseInt(s.substring(11,13));
return hour;
}
public int getMinutes(){
String s=new java.util.Date().toString();
int minute= Integer.parseInt(s.substring(14,16));
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 WorldClock extends Clock{
    private int offset;
    public WorldClock(int offset){
        this.offset = offset;
}
public int getHours(){
    int hour = (super.getHours() + offset)%24;
    return hour;
}
public int getMinutes(){
    return super.getMinutes();
}
}

///////////////////////////////////////////////////////////////

public class ClockTester {
    public static void main(String[] args) {
    Clock clock1 = new Clock();
    Clock clock2 = new WorldClock(3);
    System.out.println("Clock: "+clock1.getTime());
    System.out.println("WorldClock: "+clock2.getTime());

}
}

Thank you

Explanation / Answer

Please find my implementation.

########

public class Clock {

  

   private int alarmHour;

   private int alarmMinute;

  

   public Clock() {

       alarmHour = -1;

       alarmMinute = -1;

   }

  

   public void setAlarm(int hours, int minutes){

       alarmHour = hours;

       alarmMinute = minutes;

   }

   public int getHours(){

       String s=new java.util.Date().toString();

       int hour= Integer.parseInt(s.substring(11,13));

       return hour;

   }

   public int getMinutes(){

       String s=new java.util.Date().toString();

       int minute= Integer.parseInt(s.substring(14,16));

       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;

      

       if((alarmHour != -1) &&(getHours() >= alarmHour) && (getMinutes() >= alarmMinute)){

           date = date + " Alarm!";

           alarmHour = -1;

           alarmMinute = -1;

       }

      

       return date;

   }

  

   @Override

   public String toString() {

       String str = getTime();

      

       if(alarmHour != -1)

           str = str + " Alarm time: "+alarmHour+":"+alarmMinute;

       return str;

              

   }

}

###########

public class WorldClock extends Clock{

  

   private int offset;

  

   public WorldClock(int offset) {

       this.offset = offset;

   }

  

   public int getHours(){

       int hour = (super.getHours() + offset)%24;

       return hour;

   }

  

   public int getMinutes(){

       return super.getMinutes();

   }

}

##########

public class ClockTester {

  

   public static void main(String[] args) {

      

       Clock clock1 = new Clock();

       Clock clock2 = new WorldClock(4);

      

       System.out.println("Clock: "+clock1.getTime());

       System.out.println("WorldClock: "+clock2.getTime());

      

       clock1.setAlarm(11, 30);

      

       System.out.println(clock1);

      

   }

}

/*

Sample run:

Clock: 10:51

WorldClock: 14:51

10:51

Alarm time: 11:30

*/

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