3 Replies - 267 Views - Last Post: 29 January 2013 - 03:32 AM Rate Topic: -----

#1 EmmaOsx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-January 13

Confused in changing timezone? Java experts guidance

Posted 28 January 2013 - 07:23 PM

Valid Time Zones are integers ranging from -12 to +12 (including 0) Be sure to add a method called getZone() to return the Time Zone as an integer. Also provide a setZone() method, even though the author didn't implement comparable mutator functionality in the original Clock class

Be sure method toString in ExtClock adds the Time Zone, properly. For example, if the time here in California were 7:30 PM, your toString() should return the String: 19:30:00 UTC-8 because the Time Zone for California is -8 (UTC-8). Reuse any code from Clock that you can (hint: using the super reference).

Add a method setUTC(int) that takes a new Time Zone as an integer to set, adjusting the current time in the process, accordingly, to keep in consistent. For example, if the current Pacific or Los Angeles time is set as 19, 30, 0, -8 (that is, 19:30:00 UTC-8) and you change the time zone to -6 (Central or Chicago time) then the current time should adjust itself to 21:30:00 UTC-6 because Chicago is two hours ahead of us. Notice that if you change the Time Zone to +1 (Paris) your toString() should return the String: 04:30:00 UTC+1 because Paris is (usually) nine hours ahead of us (since we do not track any date, we do not need to account for daylight saving or summer time). Also notice that the plus sign shows for positive valued UTC Zones. The hours, minutes and seconds are zero padded, but the time zone is not, and, there is one space between the seconds and UTC, and no space between UTC and the plus or minus sign and the zone.

//class Clock

public class Clock
{
    private int hr;  //store hours
    private int min; //store minutes
    private int sec; //store seconds

        //Constructor with parameters, to set the time
        //The time is set according to the parameters
        //Postcondition: hr = hours; min = minutes; sec = seconds
    public Clock(int hours, int minutes, int seconds)
    {
        setTime(hours, minutes, seconds);
    }
    
        //Default constructor
        //Postcondition: hr = 0; min = 0; sec = 0
    public Clock()
    {
        setTime(0, 0, 0);
    }

        //Method to set the time
        //The time is set according to the parameters
        //Postconditions: hr = hours; min = minutes; sec = seconds
    public void setTime(int hours, int minutes, int seconds)
    {
        if(0 <= hours && hours < 24)
            hr = hours;
        else
            hr = 0;

        if(0 <= minutes && minutes < 60)
            min = minutes;
        else
            min = 0;

        if(0 <= seconds && seconds < 60)
            sec = seconds;
        else
            sec = 0;
    }

        //Method to return the hours
        //Postconditions: the value of hr is returned
    public int getHours()
    {
        return hr;
    }

        //Method to return the minutes
        //Postconditions: the value of min is returned
    public int getMinutes()
    {
        return min;
    }

        //Method to return the seconds
        //Postconditions: the value of sec is returned
    public int getSeconds()
    {
        return sec;
    }

        //Method to print the time
        //Postconditions: Time is printed in the form hh:mm:ss
    public void printTime()
    {
        if(hr < 10)
            System.out.print("0");
        System.out.print(hr + ":");

        if(min < 10)
            System.out.print("0");
        System.out.print(min + ":");

        if(sec < 10)
            System.out.print("0");
        System.out.print(sec);
    }

        //Method to increment the time by one second
        //Postconditions: The time is incremented by one second
        //If the before-increment time is 23:59:59, the time
        //is reset to 00:00:00
    public void incrementSeconds()
    {
        sec++;
        if(sec > 59)
        {
            sec = 0;
            incrementMinutes(); //increment minutes
        }
    }

        //Method to increment the time by one minute
        //Postconditions: The time is incremented by one minute
        //If the before-increment time is 23:59:53, the time
        //is reset to 00:00:53
    public void incrementMinutes()
    {
        min++;
        if(min > 59)
        {
            min = 0;
            incrementHours(); //increment hours
        }
    }

        //Method to increment the time by one hour
        //Postconditions: The time is incremented by one hour
        //If the before-increment time is 23:45:53, the time
        //is reset to 00:45:53
    public void incrementHours()
    {
        hr++;
        if(hr > 23)
            hr = 0;
    }

        //Method to compare the two times
        //Postconditions: Returns true if this time is equal to
        //                otherTime; otherwise returns false
    public boolean equals(Clock otherClock)
    {
        return(hr == otherClock.hr
                && min == otherClock.min
                && sec == otherClock.sec);
    }
    
        //Method to copy time
        //Postcondition: The data members of otherTime are copied
        //               into the correspoding data members of
        //               this time.
        //               hr = otherTime.hr; min = otherTime.min;
        //               sec = otherTime.sec;
    public void makeCopy(Clock otherClock)
    {
        hr = otherClock.hr;
        min = otherClock.min;
        sec = otherClock.sec;
    }

        //Method to return a copy of time
        //Postcondition: A copy of the object is created
        //               and a reference of the copy is returned
    public Clock getCopy()
    {
        Clock temp = new Clock();

        temp.hr = hr;
        temp.min = min;
        temp.sec = sec;

        return temp;
    }

    public String toString()
    {
        String str = "";

        if(hr < 10)
            str = "0";
        str = str + hr + ":";

        if(min < 10)
            str = str + "0" ;
        str = str + min + ":";

        if(sec < 10)
            str = str + "0";
        str = str + sec;

        return str;
    }
}



//class ExtClock
/*Chesong Lee implements TimeZone to superclass Clock*/

public class ExtClock extends Clock 
{
	private int timeZone;//store timezones
	

	public ExtClock()
	{
			setTime(0, 0, 0);
	}
	
	public ExtClock (int hours, int minutes, int seconds, int zone)
	{
		super(hours, minutes, seconds);
		if (zone>12 || zone<-12)
		{
		zone=0;
		}
		timeZone=zone;
	}
	
	public void setTime(int hours, int minutes, int seconds, int zone)
	{
		
		timeZone = zone;  
	}
	
	public void setZone(int zone)
	{
		timeZone = zone;
	}
	public int getZone()
	{	
	
		return timeZone;
	}
	public void setUTC(int zone)
	{

	}
	
	public String toString()
	{	
	
		return (super.toString()+" UTC"+timeZone);
	}
}



public class testClock
{
	public static void main(String [] args)
	{
		ExtClock clock = new ExtClock(19, 30, 0, -8);
		System.out.println(clock);
	}
}


Am I going in the right step? How would I adjust the time?

Is This A Good Question/Topic? 0
  • +

Replies To: Confused in changing timezone? Java experts guidance

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • View blog
  • Posts: 3,166
  • Joined: 05-April 11

Re: Confused in changing timezone? Java experts guidance

Posted 28 January 2013 - 07:51 PM

What kind of difficulties are you having? We are not going though the whole code checking if you fulfilled the requirements

We will gladly help if you narrow it down a bit :)
Was This Post Helpful? 0
  • +
  • -

#3 EmmaOsx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-January 13

Re: Confused in changing timezone? Java experts guidance

Posted 28 January 2013 - 09:39 PM

How would I change the time? when the time zones change?
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,996
  • Joined: 20-September 08

Re: Confused in changing timezone? Java experts guidance

Posted 29 January 2013 - 03:32 AM

A time zone just provides an offset from the universal time - think about it. Incidentally, mutator method should follow this pattern:

public void setX(int x) {
  this.x = x;
}
c

This post has been edited by g00se: 29 January 2013 - 03:33 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1