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

Need help, needs to be in seperate files \"extends\" to one another through a te

ID: 3838452 • Letter: N

Question

Need help, needs to be in seperate files "extends" to one another through a tester class

Implement a class Clock with getHours and getMinutes methods to return the current time. (Use new java.util.Date.toString() and extract the time from that string.) Also provide a getTime method that returns a string with the hours and minutes by calling your getHours and getMinutes methods. Provide a subclass WorldClock whose constructor accepts a time offset. For example, from California, a new WorldClock(3) will show the time in New York, three time zones ahead. Your WorldClock subclass should override some methods from Clock, but should not override getTime.

Hint: How to extract integer hour and minute from Date() object:

import java.util.Scanner;
public class Clock {
public static void main(String[] args){
String s=new java.util.Date().toString();
Scanner sc=new Scanner(s.substring(11,13));
int hour=sc.nextInt();
sc=new Scanner(s.substring(14,16));
int minute=sc.nextInt();
System.out.println(hour+":"+minute);
}}

Explanation / Answer

Hi, Please find my implementation.

######### Clock.java ########

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;

   }

}

############   WorldClock.java ######
  
public class WorldClock extends Clock{
  
   private int offset;   
  
   public WorldClock(int offset) {   
       this.offset = offset;   
   }   
  
   public int getHours(){
       String s=new java.util.Date().toString();   
       int hour= Integer.parseInt(s.substring(11,13));  
       hour = (hour + offset)%24;
       return hour;
   }   
  
   public int getMinutes(){
       String s=new java.util.Date().toString();   
       int minute= Integer.parseInt(s.substring(14,16));   
       return minute;
   }   
  
}   
  

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