(The Time class) Design a class named Time. The class contains: The data fields
ID: 3789568 • Letter: #
Question
(The Time class) Design a class named Time. The class contains: The data fields hour, minute, and second that represent a time. A no-arg constructor that creates a Time object for the current time. (The values of the data fields will represent the current time.). A constructor that constructs a Time object with a specified elapsed time since midnight, January 1, 1970, in milliseconds. (The values of the data fields will represent this time.). A constructor that constructs a Time object with the specified hour, minute, and second. Three getter 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. For example, if the elapsed time is 555550000 milliseconds, the hour is 10 the minute is 19, and the second is 10. Draw the UML diagram for the class and then implement the class. Write a test program that creates two Time objects (using new Time () and new Time (555550000)) and displays their hour, minute, and second in the format hour: minute: second.Explanation / Answer
import java.util.*;
//class definition for Time
class Time
{
//instance variables for time
int hour;
int minute;
int second;
//default constructor
public Time(){
this(System.currentTimeMillis());
}
//constructor with arguments
public Time(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);
}
public Time(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
//toSTring method of time objects
public String toString() {
return this.hour + ":" + this.minute + ":" + this.second + " GMT";
}
//getters for instance variables
public int getHour() {
return this.hour;
}
public int getMinute() {
return this.minute;
}
public int getSecond() {
return this.second;
}
//setTime() method to set elapsed time
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);
}
}
public class HelloWorld{
public static void main(String []args){
// Test the default constructor
Time time = new Time();
System.out.println("time = " + time);
// Test with a supplied value
Time time2 = new Time(555550000L);
System.out.println("time2 = " + time2);
// Test with a supplied value using setTime
Time time3 = new Time();
time3.setTime(555550000L);
System.out.println("time3 = " + time3);
// Test with a supplied value of hour,min,second
Time time4 = new Time(10,19,10);
System.out.println("time4 = " + time4);
}
}
Output:
time = 5:8:14 GMT
time2 = 10:19:10 GMT
time3 = 10:19:10 GMT
time4 = 10:19:10 GMT
Please post differnent questions if you have different requiests to make
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.