- #1
Albert12
- 9
- 0
Java Help!
I have Duty for my Jave class, My teacher needs to disply city and time by Longitude. which I set up to GMT but I have trouble problem. code seems to me work fine, I can't figure out wht's problems. it will be nice to get from you help.
disply:
I have Duty for my Jave class, My teacher needs to disply city and time by Longitude. which I set up to GMT but I have trouble problem. code seems to me work fine, I can't figure out wht's problems. it will be nice to get from you help.
Code:
package worldclockdriver;
import java.util.Date;
public class WorldClockDriver {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Clock[] GMTClocks = new Clock[5]; // Instantiate an array that will contain 5 Clock objects
for(int i=0; i<5; i++){
Clock MyClock = new Clock("Thursday", 0 ,0); // Instantiate a clock object: Thursday 12:00 am
Date Now = new Date(); // Instantiate a Date object, contains current time in GMT time zone
long milli = Now.getTime(); // Obtain current time in Greenwich (GMT) in milliseconds elapsed since Thursday, Jan. 1, 1970
MyClock.advance(milli/1000/60); // Modify MyClock to reflect current hours and minutes in GMT time zone
GMTClocks[i] = MyClock; // populate array with GMT Clocks
}
WorldClock WC1 = new WorldClock("St. Louis", "90 W", GMTClocks[0]); // Instantiate a CST (St. Louis local time) clock
System.out.println(WC1);
WorldClock WC2 = new WorldClock("Beijing", "120 E", GMTClocks[1]); // Instantiate a Beijing local time clock
System.out.println(WC2);
WorldClock WC3 = new WorldClock("Bombay", "82 E", GMTClocks[2]); // Instantiate a Bombay ocal time (Indian Standard Time) clock
System.out.println(WC3);
WorldClock WC4 = new WorldClock("Moscow", "45 E", GMTClocks[3]); // Instantiate a Moscow local time clock
System.out.println(WC4);
WorldClock WC5 = new WorldClock("London", "0 W", GMTClocks[4]); // Instantiate a London local time (GMT) clock
System.out.println(WC5);
}
}
class WorldClock{
private String City;
private String Longitude;
private Clock TimeandDay;
public WorldClock(String s, String l, Clock c){
/* Insert code here. This constructor must initialize the object *
* with relevant values for City, Longitude and TimeandDay *
* Note: TimeandDay is a GMTClock object, which must be advanced *
* or reversed to reflect the current local time */
////// I just marked this code with blue because my I be I have problem that I don't know here:
[B][COLOR="Blue"]
City = s;
Longitude = l;
TimeandDay = c;
String []splits = l.split(" ");
long m = Integer.parseInt(splits[0]);
String t = splits[1];
m = m*4;[/COLOR][/B]}
public String toString(){
return(City + " (" + Longitude + ") " + TimeandDay.toString());
}
}//end of class Worldclock
class Clock{
/************************************************** *************
* Objects of this class represent time (in hours and minutes) *
* The time data is stored in 24 hour (HHMM military format) *
************************************************** *************/
private String City;
private int day_of_week; // valid range of values is 1 thru' 7
private long hours; // range of values is 0 thru' 23
private long minutes; // range of values is 0 thru' 59
private String[] dow = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
public Clock(String weekday, int h, int m){
hours = h;
minutes = m;
day_of_week=1;
for (int i=0; i<dow.length; i++){
if (weekday.equals(dow[i])){
day_of_week = i+1;
break;
}
}
}
public void advance(long m){
final long WEEKMINUTES = 7*24*60;
final long DAYMINUTES = 24*60;
final long HOURMINUTES = 60;
long totalmin = (day_of_week>1? (day_of_week-1)*DAYMINUTES + hours*HOURMINUTES + minutes + m : hours*HOURMINUTES + minutes + m);
totalmin = (totalmin < 0? (totalmin%WEEKMINUTES)+WEEKMINUTES : totalmin);
day_of_week = (int)((totalmin/DAYMINUTES)>0? (totalmin/DAYMINUTES)+1 : 1);
day_of_week = (day_of_week>7? day_of_week%7 : day_of_week);
hours = (totalmin%DAYMINUTES)/HOURMINUTES;
minutes = (totalmin%DAYMINUTES)%HOURMINUTES;
}
public void reverse(long m){
advance(-1*m);
}
public String toString(){
String S = dow[day_of_week-1] + ", ";
S = S + (hours>12? hours-12 : hours<1? 12 : hours) + ":" + (minutes<10? ("0"+minutes) : minutes) + (hours>=12? " pm" : " am");
return S;
}
} // end of class Clock
The Attempt at a Solution
disply:
Code:
St. Louis (90 W) Friday, 1:47 am
Beijing (120 E) Friday, 1:47 am
Bombay (82 E) Friday, 1:47 am
Moscow (45 E) Friday, 1:47 am
London (0 W) Friday, 1:47 am
BUILD SUCCESSFUL (total time: 1 second)
Last edited: