Provided is the source code for the SimpleTime class. It stores the time as thre
ID: 3883591 • Letter: P
Question
Provided is the source code for the SimpleTime class. It stores the time as three integers and does not override the equals method it inherits from its superclass.
Subclass SimpleTime as GlobalTime by providing an integer zone field (named "zone") that allows for time zones values in the range of [-12, 12] (which doesn't comply with real-world conditions, but is ideal for this programming challenge).
Of course, make sure that every feature of the superclass remains functional in the subclass, along with any new features of the subclass that you are required to add (either by general Java conventions or the challenge requirements included here).
The toString of a GlobalTime should format as: H:MM:SS UTC[±][Z]
That is: zero-pad the minute and second and leave no space between the literal "UTC" and the plus or minus sign and no space between the plus or minus sign and the zone. Note that if the zone is 0, then do not append either a plus or a minus sign nor the zone.
A GlobalTime object should be considered equal to a SimpleTime object if their hour, minute, and second match. Do not make any changes to the SimpleTime code. SimpleTime will not perform equals correctly but GlobalTime will.
/*
* Source: SimpleTime.java
* Author: Instructor
* Detail: Superclass for use in the Advanced Java Design Lab
*/
public class SimpleTime
{
private int hour; // valid values 0 - 23
private int minute; // valid values 0 - 59
private int second; // valid values 0 - 59
public SimpleTime()
{
this(0, 0, 0);
}
public SimpleTime(int h)
{
this(h, 0, 0);
}
public SimpleTime(int h, int m)
{
this(h, m, 0);
}
public SimpleTime(int h, int m, int s)
{
setSimpleTime(h, m, s);
}
public SimpleTime(SimpleTime otherTime)
{
this(otherTime.hour, otherTime.minute, otherTime.second);
}
public void setSimpleTime(int h, int m, int s)
{
setHour(h);
setMinute(m);
setSecond(s);
}
public void setHour(int h)
{
hour = ( h >= 0 && h < 24 ) ? h : 0;
}
public void setMinute(int m)
{
minute = ( m >= 0 && m < 60 ) ? m : 0;
}
public void setSecond(int s)
{
second = ( s >= 0 && s < 60 ) ? s : 0;
}
public int getHour()
{
return hour;
}
public int getMinute()
{
return minute;
}
public int getSecond()
{
return second;
}
public String toString()
{
return String.format
(
"%d:%02d:%02d %s",
(hour == 0 || hour == 12) ? 12 : hour % 12, minute, second,
hour < 12 ? "AM" : "PM"
);
}
public static void main (String[] args) // SimpleTime Self-Test
{
SimpleTime t1 = new SimpleTime(20, 30, 40);
SimpleTime t2 = new SimpleTime(20, 30, 40);
SimpleTime t3 = new SimpleTime(20, 30, 50);
System.out.println("Time 1 is: " + t1);
System.out.println("Time 2 is: " + t2);
System.out.println("Time 3 is: " + t3);
System.out.println("Time 1 and 2 match: " + t1.equals(t2));
System.out.println("Time 2 and 3 match: " + t2.equals(t3));
}
}
Explanation / Answer
/**
* The GlobalTime time class defintion
* */
//GlobalTime.java
public class GlobalTime extends SimpleTime
{
//zone of type integer
private int zone;
//default constructor
public GlobalTime() {
super(0, 0, 0);
}
//constructor
public GlobalTime(int h, int m, int s) {
super(h,m,s);
}
//constructor
public GlobalTime(int h, int m, int s,int zone) {
super(h,m,s);
this.zone=zone;
}
public int getZone(){
return zone;
}
//Override toString method
public String toString()
{
if(zone!=0)
return String.format
(
"%d:%02d:%02d %s",
(getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12,
getMinute(),
getSecond(),
(zone>0) ? "UTC+"+zone:"UTC-"+zone);
else
return String.format
(
"%d:%02d:%02d",
(getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12,
getMinute(),
getSecond());
}
//Override equals method
public boolean equals(Object obj) {
GlobalTime other=(GlobalTime)obj;
return getHour()==other.getHour() &&
getMinute()==other.getMinute()&&
getSecond()==other.getSecond()&&
getZone()==other.getZone();
}
}
------------------------------------------------------------------------------------------------------------------------
/*SimpleTime.java
*/
public class SimpleTime
{
private int hour;
private int minute;
private int second;
public SimpleTime()
{
this(0, 0, 0);
}
public SimpleTime(int h)
{
this(h, 0, 0);
}
public SimpleTime(int h, int m)
{
this(h, m, 0);
}
public SimpleTime(int h, int m, int s)
{
setSimpleTime(h, m, s);
}
public SimpleTime(SimpleTime otherTime)
{
this(otherTime.hour, otherTime.minute, otherTime.second);
}
public void setSimpleTime(int h, int m, int s)
{
setHour(h);
setMinute(m);
setSecond(s);
}
public void setHour(int h)
{
hour = ( h >= 0 && h < 24 ) ? h : 0;
}
public void setMinute(int m)
{
minute = ( m >= 0 && m < 60 ) ? m : 0;
}
public void setSecond(int s)
{
second = ( s >= 0 && s < 60 ) ? s : 0;
}
public int getHour()
{
return hour;
}
public int getMinute()
{
return minute;
}
public int getSecond()
{
return second;
}
public String toString()
{
return String.format
(
"%d:%02d:%02d %s",
(hour == 0 || hour == 12) ? 12 : hour % 12, minute, second,
hour < 12 ? "AM" : "PM"
);
}
}
------------------------------------------------------------------------------------------------------------------------
//Test class for GlobalTime
//TestGlobalTime.java
public class TestGlobalTime
{
public static void main(String[] args)
{
System.out.println("Creating GlobalTime object with zero zone");
GlobalTime zerozone=new GlobalTime(10, 15, 15, 0);
System.out.println(zerozone.toString());
System.out.println("Creating GlobalTime object with zone");
GlobalTime gtime=new GlobalTime(10, 15, 15, 10);
System.out.println(gtime.toString());
System.out.println("Checking equality method");
System.out.println(zerozone.equals(gtime));
}
}//end of class
------------------------------------------------------------------------------------------------------------------------
Sample Output:
Creating GlobalTime object with zero zone
10:15:15
Creating GlobalTime object with zone
10:15:15 UTC+10
Checking equality method
false
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.