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

Following the instructions in the problem statement, design and implement a Java

ID: 3696226 • Letter: F

Question

Following the instructions in the problem statement, design and implement a Java program for programming exercise 10.1, page 399 (name it Time.java).

Time:

public class Time
{
private int hour;
private int minute;
private int second;

public Time()
{
this(System.currentTimeMillis());
}

public Time(long elapseTime)
{
long totalSeconds = elapseTime / 1000L;

this.second = (int)(totalSeconds % 60L);

long totalMinutes = totalSeconds / 60L;

this.minute = (int)(totalMinutes % 60L);

int totalHours = (int)(totalMinutes / 60L);

this.hour = (totalHours % 24);
}

public String toString() {
return this.hour + ":" + this.minute + ":" + this.second + " GMT";
}

public int getHour() {
return this.hour;
}

public int getMinute() {
return this.minute;
}

public int getSecond() {
return this.second;
}
}

Next, develop a test program in a separate file (call it TestTime.java) to create couple time object and display their time as specified in the problem statement. Document your code, and organize and space the outputs properly. Use escape characters and formatting objects when applicable.

Explanation / Answer

Test class to test the time class is given below with proper comments

import java.io.*; //test class to test Time Class
public class TimeTest
{
    public static void main(String args[]) //main method
    {
        Time t1 = new Time(); //time object
        System.out.println(t1.getHour());
      
        System.out.println(t1.getMinute());
        System.out.println(t1.getSecond());
        System.out.println(t1.toString());

        Time t2 = new Time(); //time object
        System.out.println(t2.getHour());
      
        System.out.println(t2.getMinute());
        System.out.println(t2.getSecond());
        System.out.println(t2.toString());
      
    }
  
}