Display city and time by Longitude in JAVA

  • Context: Comp Sci 
  • Thread starter Thread starter Albert12
  • Start date Start date
  • Tags Tags
    Java Longitude Time
Click For Summary

Discussion Overview

The discussion revolves around a Java programming assignment that requires displaying the current time in various cities based on their longitudes. Participants are addressing issues related to the implementation of the WorldClock class and the logic for adjusting time according to longitude.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant expresses difficulty in implementing the WorldClock constructor to correctly adjust time based on longitude, indicating that the code compiles but does not produce the expected results.
  • Another participant suggests that the constructor should parse the longitude to determine if it is west or east of Greenwich and adjust the clock accordingly, noting that St. Louis should be 6 hours behind GMT.
  • A different participant questions the absence of a GregorianCalendar in the implementation, which could simplify time zone handling.
  • One participant mentions that the assignment does not prohibit additional code but emphasizes the importance of understanding any added code.
  • Another participant shares an attempt to modify the code by adding an array for longitudes and implementing conditional logic based on the direction (W or E), but still does not achieve the desired outcome.
  • One participant critiques the approach of hardcoding city data into a single array, suggesting that a mathematical understanding of time differences should precede coding efforts.
  • Another participant agrees with the critique, reiterating the need for logic in the WorldClock constructor to calculate time adjustments based on longitude.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper logic in the WorldClock constructor to handle time adjustments based on longitude. However, there are differing opinions on the best approach to implement this, with some advocating for mathematical clarity before coding and others focusing on specific code modifications.

Contextual Notes

There are unresolved issues regarding the handling of time differences and the overall structure of the code, including the effectiveness of hardcoding city data and the potential use of built-in Java classes for time management.

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 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K