Comp Sci Display city and time by Longitude in JAVA

AI Thread Summary
The discussion focuses on a Java programming challenge involving the display of city names and local times based on their longitudes. The user is struggling to implement logic in the WorldClock constructor to adjust the time correctly according to the longitude, which should advance or reverse the clock by one hour for every 15 degrees east or west of Greenwich. Suggestions emphasize the need for a mathematical formula to determine time differences rather than relying on hardcoded values. Additionally, there's a recommendation to avoid using arrays for city longitudes, advocating for a more dynamic approach. The overall goal is to ensure accurate local time representation for various cities based on their geographic coordinates.
Albert12
Messages
9
Reaction score
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.


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]
City = s;
Longitude = l;
TimeandDay = c;

String []splits = l.split(" ");
long m = Integer.parseInt(splits[0]);
String t = splits[1];
m = m*4;[/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:
Physics news on Phys.org


Your WorldClock constructor doesn't seem to be working correctly. When this constructor is called, it is passed three parameters: a city name, the longitude, and a Clock instance. For every 15 degrees of latitude west of the Greenwich Meridian, the clock should be reversed one hour. For every 15 degrees of latitude east of the Greenwich Meridian, the clock should be advanced one hour. You need to have logic in your constructor to parse the latitude, figure out if it's west or east of Greenwich, and reverse or advance the clock accordingly. For example, St. Louis is 90 deg. west, so the time should be reversed by 6 hours. If it's 12:00 midnight in London, it will be 4:00pm the day before. For Bombay (Mumbai), the time would be 5:00am, I think.
 


Was there a requirement in the assignment to not use a GregorianCalendar? It has time zone handling built in.
 


no there is not any requirments, he said if you add any code you should understand it well, not just copy it.
 


I just changed a little bit in my code and still dint give me the result that I need. I add a new arrry to class clack and I add if statement to chose "W" or "E" depent on the time.
Code:
String[] splits = {"90", "W","120", "E","82","E","45","E","0", "W"};

the other code I add :

Code:
City = s;
Longitude = l;
TimeandDay = c;



String []splits = Longitude.split(" ");


int m = Integer.parseInt(splits[0]);

int m1  = m*4;
String t = splits[1];


     if (t.endsWith("W")){

         m1=m1-m;
    }
     else {

     if (t.endsWith("E")){
    m1=m1+m;
    }

    }}
 


If you're going to hardcode the cities, why would you put everything into a single array and then try to parse out the data that you need? What if the cities change? You need to step back and consider the time differences mathematically. Figure out the formula that you need to know for the time differences before you try to code it. Once you have that formula, the next step should be more apparent.
 


I agree with Borg that the array with latitudes is not a good idea. As I said before, you need logic in your Worldclock constructor that takes the latitude (including the W or E suffix) and figures out how many hours to advance or reverse the Clock's time. Each 15 degrees W or E corresponds to one hour back or ahead.
 

Similar threads

Replies
2
Views
1K
Replies
7
Views
2K
Replies
7
Views
3K
Replies
5
Views
3K
Replies
8
Views
1K
Replies
12
Views
2K
Replies
15
Views
2K
Replies
1
Views
1K
Replies
1
Views
2K
Back
Top