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
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.
Provided is the source code for the SimpleTime
and SimpleDate SimpleDate
classes. Subclass the Date and include a Time instance. Remember to include 8 constructors. Field ordering should match the Time and Date classes except that Date has priority (i.e., a single digit constructor represents the year only, while the four digit constructor represents the month, day, year and hour, etc.).
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
public void setMinute(int m) { minute = ( m >= 0 && m
public void setSecond(int s) { second = ( s >= 0 && s
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
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
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
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(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
