Please use Java for the following!!!! Implement a class named Time. The class co
ID: 3688027 • Letter: P
Question
Please use Java for the following!!!!
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 elapsed time since midnight, January 1, 1970 in milliseconds. (The data field values 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
ANy queries please comment
import java.util.Calendar;
public class Time
{
private int hour;
private int minute;
private int second;
public static void main (String args[])
{
Time time1 = new Time();
Time time2 = new Time();
time2.setTime(555550000L);
System.out.println("First Time: "+time1.hour+":"+time1.minute+":"time1.second);
System.out.println("Second Time: "+time2.hour+":"+time2.minute+":"time2.second);
}
public Time()
{
Calendar calen= Calendar.getInstance();
hour=calen.get(Calendar.HOUR_OF_DAY);
minute=calen.get(Calendar.MINUTE);
second=calen.get(Calendar.SECOND);
}
public void setTime(long elapseTime)
{
Calendar c=Calendar.getInstance();
c.setTimeInMillis(elapseTime);
hour=c.get(Calendar.HOUR_OF_DAY);
minute=c.get(Calendar.MINUTE);
second=c.get(Calendar.SECOND);
}
}