In a previous post about this same problem, I had some particular questions, and I think that they have been answered. To see that topic, please go here: Inheritance and Polymorphism
Since I have fixed the majority of the initial problems from my previous post, I am now making a new post, with more accurate questions.
NOTE TO MODERATORS: If posting the actual homework question from the book like this is inappropriate, please remove it, and let me know how to post the information in a more appropriate manner.
Here is the question as it appears in the book:
Quote
In Chapter 6, the class Clock was designed to implement the time of day
in a program. Certain applications—in addition to storing hours, minutes,
and seconds—might require you to store the time zone. Derive the class
ExtClock from the class Clock by adding a data member to store the time
zone. Add the necessary methods and constructors to make the class functional.
Also, write the definitions of the methods and the constructors.
Finally, write a test program to test your class.
in a program. Certain applications—in addition to storing hours, minutes,
and seconds—might require you to store the time zone. Derive the class
ExtClock from the class Clock by adding a data member to store the time
zone. Add the necessary methods and constructors to make the class functional.
Also, write the definitions of the methods and the constructors.
Finally, write a test program to test your class.
My understanding of this is that I need to take the source code for the class Clock (as shown below), and create a new class ExtClock that extends Clock. It should add functionality for having a timeZone as well as the hours, minutes, and seconds.
I am well aware of the fact that my code does not match up properly, but that is due to me experimenting with various things to see what will happen if I change them. I will post in three parts, the first one is the class Clock that I was provided as a starting point, the other two are my own invention, (with a little help from the wonderful people here on this forum). All three of these classes are in the same project folder and were created using NetBeans 6.8 (if it matters).
Here is my code:
public class Clock
{
private int hr; //store hours
private int min; //store minutes
private int sec; //store seconds
//Default constructor
//Postcondition: hr = 0; min = 0; sec = 0
public Clock()
{
setTime(0, 0, 0);
}
//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);
}
//Method to set the time
//The time is set according to the parameters.
//Postcondition: 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;
} //end setTime
//Method to set the hours
//The hours are set according to the parameters.
//Postcondition: hr = hours;
public void setHours(int hours)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;
} //end setHours
//Method to set the minutes
//The minutes are set according to the parameters.
//Postcondition: min = minutes;
public void setMinutes(int minutes)
{
if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;
} //end setMinutes
//Method to set the seconds
//The seconds are set according to the parameters.
//Postcondition: sec = seconds;
public void setSeconds(int seconds)
{
if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
} //end setSeconds
//Method to return the hours
//Postcondition: the value of hr is returned
public int getHours()
{
return hr;
} //end getHours
//Method to return the minutes
//Postcondition: the value of min is returned
public int getMinutes()
{
return min;
} //end getMinutes
//Method to return the seconds
//Postcondition: the value of sec is returned
public int getSeconds()
{
return sec;
} //end getSeconds
//Method to increment the time by one second
//Postcondition: 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
}
} //end incrementSeconds
//Method to increment the time by one minute
//Postcondition: 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
}
} //end incrementMinutes
//Method to increment the time by one hour
//Postcondition: 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;
} //end incrementHours
//Method to compare two times
//Postcondition: Returns true if this time is equal to
// otherClock; otherwise returns false
public boolean equals(Clock otherClock)
{
return (hr == otherClock.hr
&& min == otherClock.min
&& sec == otherClock.sec);
} //end equals
//Method to return the time as a string.
//Postcondition: Time is returned as a string in the form
// hh:mm:ss
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;
} //end toString
//Method to copy time
//Postcondition: The instance variables of otherClock
// copied into the corresponding data
// are members of this time.
// hr = otherClock.hr;
// min = otherClock.min;
// sec = otherClock.sec;
public void makeCopy(Clock otherClock)
{
hr = otherClock.hr;
min = otherClock.min;
sec = otherClock.sec;
} //end makeCopy
//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(hr, min, sec);
return temp;
} //end getCopy
}//end of class
public class ExtClock extends Clock
{
//declare private variables
private String timeZone;
public ExtClock()
{
setTime(0, 0, 0, "unknown");
}
public ExtClock(int hours, int minutes, int seconds, String zone)
{
super();
timeZone = zone;
}
public void setTime(int hours, int minutes, int seconds, String zone)
{
timeZone = zone;
}
public void setTimeZone(String zone)
{
timeZone = zone;
}
//create public method to get the variable timeZone
public String getTimeZone()
{
return timeZone;
}
//override method for reading out the complete time to include time zone?
@Override
public String toString()
{
return (super.toString() + " " + timeZone);
} //end toString
}//end of class
public class Main
{
public static void main(String[] args)
{
//instantiate a clock here?
ExtClock clockOne = new ExtClock();
System.out.println(ExtClock.class.toString());//really not sure about this one
// ? ? ? call constructor here ? ? ?
}//end of main
}//end of class
What I am asking for:
1: Any logic errors, and a nudge in the right direction on how to fix them.
2: Anything that is outright missing, and a suggestion on the logic of what needs to be put there, (not necessarily code).
3: Anything I did particularly well, and why it is good.
4: Any other comments or suggestions you might have for me.
Thank you in advance for your time!
This post has been edited by CodingInBoise: 21 June 2012 - 02:31 PM

New Topic/Question
Reply



MultiQuote



|