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

Design a class named Time, the class contains: - the data fields hour,minute, an

ID: 3532531 • Letter: D

Question

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 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 get methods for the data fields hour minute and second respectively.

-A method names setTime(long elapsedTime) 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 the second is 10

Explanation / Answer

import java.util.Calendar;


public class Time {
private int hour;
private int minute;
private int second;
public Time() {
Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR);
minute = c.get(Calendar.MINUTE);
second = c.get(Calendar.SECOND);
}
public Time(int hour, int minute, int second) {
super();
this.hour = hour;
this.minute = minute;
this.second = second;
}
public int getHour() {
return hour;
}
public int getMinute() {
return minute;
}
public int getSecond() {
return second;
}
private void setTime(long elapsedTime) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(elapsedTime);
hour = c.get(Calendar.HOUR);
minute = c.get(Calendar.MINUTE);
second = c.get(Calendar.SECOND);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote