How can I impliment my world clock in my tester program? my assinment is .... Im
ID: 3839163 • Letter: H
Question
How can I impliment my world clock in my tester program? my assinment is ....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. 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 timeoffset;
public WorldClock(int offset){
this.timeoffset = offset;
}
public int getHours(){
String s=new java.util.Date().toString();
int hour= Integer.parseInt(s.substring(11,13));
hour = (hour + timeoffset)%24;
return hour;
}
public int getMinutes(){
String s=new java.util.Date().toString();
int minute= Integer.parseInt(s.substring(14,16));
return minute;
}
}
////////////////////////////////////////////////////////////////////////////////////import java.util.Scanner;
public class ClockTester {
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);
}}
I go it to give the time but how do I get the world clock? Using what I got please and thank you!
Explanation / Answer
Please find my code.
########### 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(){
int hour = (super.getHours() + offset)%24;
return hour;
}
public int getMinutes(){
return super.getMinutes();
}
}
############## ClockTester.java ##########
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());
}
}
/*
Sample run:
Clock: 08:46
WorldClock: 12:46
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.