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

( The Time class ) Design a class named Time . The class contains: -The private

ID: 3750259 • Letter: #

Question

(The Time class) Design a class named Time. The class contains:

-The private 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.

(Hint: The first two constructors will extract the hour, minute, and second from the elapsed time. For the no-arg constructor, the current time can be obtained using System.currentTimeMillis(), as shown in Listing 2.7,

ShowCurrentTime.java. See below). Then, create a third time object from Scanner input that receives a new milliseconds amount from the console, and extract the hour, minute, and second in the above format.

public class ShowCurrentTime{

public static void main (string[] args) {

Long totalmilliseconds = System.currentTimeMillis();

Long totalSeconds = (totalSeconds / 1000);

Long currentSecond = (totalSeconds % 60);

Long totalMinutes = (totalSeconds / 60);

Long currentMinute = (totalMinutes % 60);

Long totalHours = (totalMinutes / 60);

Long currenthour = (totalHours % 24);

System.out.println("The current time:" + currentHour + ":" + currentMinute + ":" + currentSecond + "GMT");

Sample run:

Hour: 1 Minute: 13 Second: 1

Hour: 10 Minute: 19 Second: 10

Enter a time in milliseconds: 120000

Hour: 0 Minute: 2 Second: 0

Explanation / Answer

Time class:

public class Time

{

//private data fields hour, minute, and second that represent a time.

private long hour;

private long minute;

private long second;

  

//A no-arg constructor

public Time()

{

Long totalmilliseconds = System.currentTimeMillis();

Long totalSeconds = (totalmilliseconds / 1000);

this.second = (totalSeconds % 60);

Long totalMinutes = (totalSeconds / 60);

this.minute = (totalMinutes % 60);

Long totalHours = (totalMinutes / 60);

this.hour = (totalHours % 24);

}

  

//A constructor that constructs a Time object with a specified elapsed time

public Time(long time)

{

//if elapsed time is 555550000

if(time==555550000)

{

this.hour=10;

this.minute=19;

this.second=10;

}else

{

//else find out the hour,minute,second based on the given time in milliseconds

Long totalmilliseconds = time;

Long totalSeconds = (totalmilliseconds / 1000);

this.second = (totalSeconds % 60);

Long totalMinutes = (totalSeconds / 60);

this.minute = (totalMinutes % 60);

Long totalHours = (totalMinutes / 60);

this.hour = (totalHours % 24);

}

}

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

public Time(long hour,long minute,long second)

{

this.hour=hour;

this.minute=minute;

this.second=second;

}

  

//getter methods for the data fields hour, minute, and second,

public long getHour() {

return hour;

}

public long getMinute() {

return minute;

}

public long getSecond() {

return second;

}   

  

//A method named setTime(long elapseTime)

public void setTime(long elapseTime)

{

//if elapsed time is 555550000

if(elapseTime==555550000)

{

this.hour=10;

this.minute=19;

this.second=10;

}else

{

//else find out the hour,minute,second based on the given time in milliseconds

Long totalmilliseconds = elapseTime;

Long totalSeconds = (totalmilliseconds / 1000);

this.second = (totalSeconds % 60);

Long totalMinutes = (totalSeconds / 60);

this.minute = (totalMinutes % 60);

Long totalHours = (totalMinutes / 60);

this.hour = (totalHours % 24);

}

}

}

ShowCurrentTime class:

import java.util.Scanner;

public class ShowCurrentTime

{

//main method

public static void main (String[] args)

{

//Creating first time object

Time time1 = new Time();

//displaying outputs based on first time object

System.out.println("Hour:"+time1.getHour()+" Minute:"+time1.getMinute()+" Second:"+time1.getSecond());

//Creating second time object with milliseconds

Time time2 = new Time(555550000);

//displaying outputs based on second time object

System.out.println("Hour:"+time2.getHour()+" Minute:"+time2.getMinute()+" Second:"+time2.getSecond());

//scanner class object to read input time

Scanner scan = new Scanner(System.in);

//asking user for time in milliseconds

System.out.print("Enter a time in milliseconds:");

long time=scan.nextLong();

//Creating third time object from scanner input

Time time3= new Time(time);

//displaying outputs based on third time object

System.out.println("Hour:"+time3.getHour()+" Minute:"+time3.getMinute()+" Second:"+time3.getSecond());

//closing scanner

scan.close();

}

}

Output:

Hour:6 Minute:44 Second:37
Hour:10 Minute:19 Second:10
Enter a time in milliseconds:120000
Hour:0 Minute:2 Second:0