We have now discussed the GlobalTime solution to Part 01 of this lab in class. N
ID: 3883978 • Letter: W
Question
We have now discussed the GlobalTime solution to Part 01 of this lab in class. Next we will subclass a SimpleDate (also provided) into a TimeStamp by adding a GlobalTime instance as a member. We will also make sure all functionality in the original classes (SimpleDate, SimpleTime and GlobalTime) remain fully operational and work according to user expectations considering the features the new TimeStamp incorporates.
Change the default constructor to create the TimeStamp based on the current system time (Java provides resources in its API).
Add a changeZone(int zone) method that adjusts the TimeStamp to represent the original time to the new zone.
The toString of a TimeStamp should format as: MMM DD, YYYY at H:MM:SS UTC[±][Z]
Of course the equals method should account for the zone when matching times and dates.
Note that the toString method will return a String containing the month as three characters followed by a space and the two digit day of the month followed by a comma, a space and the four digit year followed by another space, "at", another space and the GlobalTime format. Again, remember that if the zone is 0, do not append either a plus or a minus sign nor the zone.
*
* 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));
}
}
*
* Source: SimpleDate.java
* Author: Instructor
* Detail: Superclass for use in the Advanced Java Design Lab
*/
public class SimpleDate
{
private int month; // valid values in: 1 - 12
private int day; // valid values in: 1 - max days in month
private int year; // valid values in: all integers but 0
public SimpleDate()
{
this(1, 1, 1);
}
public SimpleDate(int y)
{
this(1, 1, y);
}
public SimpleDate(int m, int y)
{
this(m, 1, y);
}
public SimpleDate(int m, int d, int y)
{
setDate(m, d, y);
}
public SimpleDate(SimpleDate otherDate)
{
this(otherDate.month, otherDate.day, otherDate.year);
}
public void setDate(int m, int d, int y)
{
setYear(y);
setMonth(m);
setDay(d);
}
public void setMonth(int m)
{
month = ( m >= 1 && m <= 12 ) ? m : 1;
}
public int getMonth()
{
return month;
}
public void setDay(int d)
{
int daysPerMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (d >=1 && d <= daysPerMonth[month])
day = d;
else if
(
month == 2 && d == 29 &&
(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
)
{
day = d;
}
else
{
day = 1;
}
}
public int getDay()
{
return day;
}
public void setYear(int y)
{
year = (!(y == 0)) ? y : 1;
}
public int getYear()
{
return year;
}
public boolean equals(Object o)
{
if (o instanceof SimpleDate)
{
return
(
((SimpleDate) o).month == this.month &&
((SimpleDate) o).day == this.day &&
((SimpleDate) o).year == this.year
);
}
else
{
return false;
}
}
public String toString()
{
return String.format("%02d/%02d/%04d", month, day, year);
}
public static void main (String[] args) // SimpleTime Self-Test
{
SimpleDate d1 = new SimpleDate(2, 1, 2017);
SimpleDate d2 = new SimpleDate(2, 1, 2017);
SimpleDate d3 = new SimpleDate(2, 29, 2017);
System.out.println("Date 1 is: " + d1);
System.out.println("Date 2 is: " + d2);
System.out.println("Date 3 is: " + d3);
System.out.println("Date 1 and 2 match: " + d1.equals(d2));
System.out.println("Date 2 and 3 match: " + d2.equals(d3));
}
}
//GlobalTime.java
// Partial Code
// LAB Assignment 1
public class GlobalTime extends SimpleTime
{
//zone of type integer
int zone;
//default constructor
public GlobalTime()
{
super(0, 0, 0);
zone (0);
}
public GlobalTime(int h)
{
//missing code
zone(0);
}
public GlobalTime(int h, int m)
{
//missing code
zone(0);
}
//constructor
public GlobalTime(int h, int m, int s)
{
super(h,m,s);
zone(0);
}
//constructor
public GlobalTime(int h, int m, int s,int zone)
{
super(h,m,s);
this.zone=zone;
}
public void setTime(int h, int m, int s, int 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());
)
}
public GlobalTime (SimpleTime otherTime)
{
}
//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();
}
}
Explanation / Answer
TimeStamp.java:
import java.util.Calendar;
public class TimeStamp extends SimpleDate {
GlobalTime time;
static Calendar c = Calendar.getInstance();
String months[] = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"};
public TimeStamp() {
super(c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.YEAR));
time = new GlobalTime(c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), c.get(Calendar.SECOND), c.get(Calendar.ZONE_OFFSET));
}
public TimeStamp(int y) {
this(1, 1, y);
time = new GlobalTime();
}
public TimeStamp(int m, int y) {
this(m, 1, y);
time = new GlobalTime();
}
public TimeStamp(int m, int d, int y) {
setDate(m, d, y);
time = new GlobalTime();
}
public TimeStamp(int m, int d, int y, int h) {
setDate(m, d, y);
time = new GlobalTime(h);
}
public TimeStamp(int m, int d, int y, int h, int mi) {
setDate(m, d, y);
time = new GlobalTime(h, mi);
}
public TimeStamp(int m, int d, int y, int h, int mi, int s) {
setDate(m, d, y);
time = new GlobalTime(h, mi, s);
}
public TimeStamp(int m, int d, int y, int h, int mi, int s, int zone) {
setDate(m, d, y);
time = new GlobalTime(h, mi, s, zone);
}
public void changeZone(int zone) {
time.setTime(time.getHour(), time.getMinute(), time.getSecond(), zone);
}
// Override toString method
public String toString() {
String s = months[getMonth()] + " " + getDay() + ", " + getYear() + " at " + time.getHour() + ":" + time.getMinute() + ":" + time.getSecond();
int zone = time.getZone();
if(zone < 0) {
s += " UTC" + zone;
} else {
s += " UTC+" + zone;
}
return s;
}
// Override equals method
public boolean equals(TimeStamp obj) {
return (obj.getDay()==getDay() && obj.getMonth()==getMonth() && obj.getYear()==getYear()) && obj.time.equals(this.time);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.