How can I impliment my world clock in my program? my assinment is .... Implement
ID: 3838520 • Letter: H
Question
How can I impliment my world clock in my 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?
Explanation / Answer
import java.util.StringTokenizer; import java.text.DecimalFormat; public class Time { // instance data members - values for each different time object private int hours; private int minutes; private boolean morning; // constructors ----------------------------------------------- /** Default constructor */ public Time() { // default constructor, time is midnight hours = 12; minutes = 0; morning = true; } /** Standard time constructor @param h the hours @param m the minutes @param ap morning or afternoon indicator */ public Time(int h, int m, char ap) { setHours(h); setMinutes(m); setWhen(ap); } /** Military time constructor @param t the military time */ public Time(int t) { // t is military time int h = t / 100; setMinutes(t % 100); if (h == 0) { hours = 12; morning = true; } else if (h > 12) { setHours(h - 12); morning = false; } else if (h < 12) { setHours(h); morning = true; } else { // h == 12 == noon hours = 12; morning = false; } } /** String constructor: "10:30 am" "2:15p" "06:01AM" @param s the full standard time */ public Time(String s) { s = s.toLowerCase(); int index = s.indexOf('a'); morning = true; if (index < 0) { index = s.indexOf('p'); morning = false; } if (index < 0) { // error - no a or p index = s.length(); } s = s.substring(0,index); StringTokenizer tok = new StringTokenizer(s, ": "); setHours(Integer.parseInt(tok.nextToken())); setMinutes(Integer.parseInt(tok.nextToken())); } // instance methods -------------------------------------------- /** Set the hours, validity check @param h the hours value @return true if valid, false otherwise */ private boolean setHours(int h) { if ( h > 0 && h = 0 && m < 60) { minutes = m; return true; } System.out.println("ERROR: invalid minutes " + m); minutes = 0; return false; } /** Set the morning indicator @param ap the character a or p */ private void setWhen(char ap) { ap = Character.toLowerCase(ap); if (ap == 'a') morning = true; // check for invalid data later else morning = false; } /** Calculate current time plus more hours @param h the hours to add @return the new time result */ public Time addHours(int h) { // get military // add hours // check if next day // convert back int mil = military(); int result = (mil / 100 + h) % 24; // new hours value result = result * 100 + minutes; return new Time(result); } /** Add minutes to the time Pre-condition: 0 = 60 && m > 0) { System.out.println("Error: can't add more than 59 minutes"); return; } minutes = minutes + m; if (minutes > 59) { minutes = minutes - 60; hours++; if (hours == 12) { if (morning) morning = false; else morning = true; // morning = !morning; } if (hours > 12) hours = hours - 12; } } /** Display the time on standard output */ public void display() { System.out.print(toString()); } // gets called implicitly when needed to add object to a string /** Create a string representation of the object @return the string */ public String toString() { // change to using Decimal Format DecimalFormat two0 = new DecimalFormat("00"); String s = two0.format(hours) + ":" + two0.format(minutes); s += (morning ? " am" : " pm"); return s; } /** Calculate the elapsed time in minutes between two times 2:15pm until 10:30pm = 8*60 + 15 10:30pm until 2:15pm = 15*60 + 45 @param t the end time @return the elapsed time in minutes */ public int elapsed(Time t) { int t1 = military(); // for implicit parameter t1 = t1 / 100 * 60 + minutes; // elapsed mins since midnight int t2 = t.military(); t2 = t2 / 100 * 60 + t.minutes; int diff = t2 - t1; if (diff < 0) diff = diff + 24*60; return diff; } /** Compare two times @param the time to compare to @return 0 if equal, - if implicit explicit */ public int compareTo(Time t) { return military() - t.military(); } /** Check if the time is in the afternoon @return true if it is, false otherwise */ public boolean isAfternoon() { return !morning; } /** Convert the time to military format @return the military time */ public int military() { int mil; if (morning) { if (hours != 12) mil = hours * 100; else mil = 0; } else { if (hours != 12) mil = (hours+12) * 100; else mil = hours * 100; } return mil + minutes; } /** Convert the time to military format @return the military time */ public String militaryString() { DecimalFormat four0 = new DecimalFormat("0000"); return four0.format(military()); } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.