Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(The Time.java class) Implement a class named Time. The class contains: Data fie

ID: 3762342 • Letter: #

Question

(The Time.java class) Implement a class named Time. The class contains:

Data fields hour, minute, and second that represents a time.

A no-arg constructor that creates a Time object for the current local time.

A constructor that constructs a Time object with a specified elapse time since midnight, January 1, 1970 in milliseconds. (The data fields value will represent this time.)

A constructor that creates a Time object with the specified hour, minute and second.

Three get methods for the data fields hour, minute, and second, respectively.

A method named setTime(long elapseTime) that sets a new time for the object using the elapsed time.

Write a test program that creates two Time objects (using new Time() and new Time(555550000)) and display their hour, minute, and second.

Note 1: The pseudocode to initialize times with the current time is:

public Time(){

Calendar cal = Calendar.getInstance();

hour = cal.get(Calendar.HOUR_OF_DAY);

minute = cal.get(Calendar.MINUTE);

second = cal.get(Calendar.SECOND);

}

Note 2: The pseudocode to compute times from the elapsed time in milliseconds in the second constructor is:

totalSeconds is the elapseTime / 1000

time_seconds is the reminder after you divide totalSeconds to 60

totalMinutes is the totalSeconds / 60

time_minutes is the reminder after you divide totalMinutes to 60

totalhours is the totalMinutes / 60

time_hour is the reminder after you divide totalhours to 24

The setTime method does exactly the same thing as the constructor above. It is a modifier method – it changes the data fields of an object that was already created.

Explanation / Answer

class Time {double hour; double minute; double second; public Time(long elapseTime){ //setTime(elapseTime); second = (elapseTime / 1000); minute = (elapseTime / 1000 / 60); hour = (elapseTime / 1000 / 60 / 60); } Time(int hour, int minute, int second){ /*hour = ((this.hour >= 0 && this.hour < 24) ? this.hour : 0); minute = ((this.minute >= 0 && this.minute < 6) ? this.minute : 0); second = ((this.second >= 0 && this.second < 24) ? this.second : 0);*/ this.hour = hour; this.minute = minute; this.second = second; } public void setTime(long elapseTime){ second = (int) (elapseTime / 1000) % 60 ; minute = (int) ((elapseTime / (1000*60)) % 60); hour = (int) ((elapseTime / (1000*60*60)) % 24); } public long getHour() { return hour; } public long getMinute() { return minute; } public long getSecond() { return second; } public String toString(){ return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond()); } } public class TestTimeClass{ public static void main(String [] args){ Time t1 = new Time(); System.out.println(t1); //System.out.println(System.currentTimeMillis()); Time t2 = new Time(1382832000); System.out.println(t2); t2.setTime(555550000); System.out.println(t2); } }