Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Inheritance, timezone will not set back to 0 when greater than 12

$
0
0
Instructions: Valid Time Zones are integers ranging from -12 to +12 (including 0, so we will just ignore the zones that are not integers or that are greater than +12 even though some unusual zones with those features do, in fact, exist for reasons of commerce or politics). 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 (you don't have to provide any set methods for the superclass fields that the author should have provided, but you should be able to figure out how this could have been accomplished in your subclass, if you did).

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).

My question is when I set my Timezone out of the range (greater than 12 or less than 12 it won't set back 0)
Anyone see the problem? What is wrong with my code?

//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

public class ExtClock extends Clock 
{
	private int timeZone;
	
	
	public ExtClock (int hours, int minutes, int seconds, int zones)
	{
		super(hours, minutes, seconds);
		timeZone=zones;
	}
	public ExtClock()
	{
			setTime(0, 0, 0);
	}
	
	public void setTime(int hours, int minutes, int seconds, int zones)
	{
		if( -13 < zones && zones < 13)
            timeZone = zones;
      else
        timeZone = 0;
		  
		
	}
	public int getTimeZone()
	{	
		return timeZone;
	}
	
	public String toString()
	{
		return (super.toString()+" "+timeZone);
	}
}



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

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>