-
Inheritance program
Hello I am very new to Java I have clock class and I would like to make a Derived class from the clock class here is what I have so far. But I am having trouble understanding how to do this. I looked for some code online but found none. Any help in writing this would be greatly appreciated.
public class Clock
{
private int hour;
private int minute;
private int second;
public Clock()
{
//initialize to default values
hour = 0;
minute = 0;
second = 0;
}
public Clock(int h, int m, int s)
{
setHour(h);
setMinute(m);
setSecond(s);
}
/* Valid integers for hour is in the range of 0 – 24 */
public void setHour(int h)
{
if((h >= 0) && (h <= 24))
hour = h;
else System.out.println("Fatal error: invalid hour");
}
/* Valid integers for minute is in the range 0 – 59 */
public void setMinute(int m)
{
if((m >= 0) && (m <= 59))
minute = m;
else System.out.println("Fatal error: invalid minute");
}
/* Valid integers for second is in the range 0 – 59 */
public void setSecond(int s)
{
if((s >= 0) && (s <= 59))
second = s;
else System.out.println("Fatal error: invalid second");
}
public int getHour()
{
return hour;
}
public int getMinute()
{
return minute;
}
public int getSecond()
{
return second;
}
//Facilitator methods
public String toString()
{
return "The current time is: " + hour + ":" + minute + ":" + second;
}
public boolean equals(Object o)
{
if (0 == null)
return false;
else {
if (getClass() != o.getClass())
return false;
else {
Clock otherClock = (Clock) o;
return((hour == otherClock.hour) &&
(minute == otherClock.minute) &&
(second == otherClock.second));
}
}
}
I have these for the derived class:
public class AlarmClock extends Clock
public AlarmClock()
public AlarmClock(h,m,s,ah,am,as)
public void setAlarmHour(int h)
get hours ()
get Minutes ()
get Seconds()
set hours()
Set Minutes()
Set seconds()
the meat of the methods are giving me trouhle
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks