Hello I need coding help with the below program. Design a class named Time . The
ID: 3630162 • Letter: H
Question
Hello I need coding help with the below program.
Design a class named Time. The class contains:
*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, Jan 1, 1970, in milliseconds. (The value of the data fields will represent this time.)
*A contructor that constructs a Time object with the specified hour, minute, and second.
*Three get methods for the data fields hour, minute, and second, repsectively.
*A method named setTime(long elapse Time) that sets a new time for the object using the elapsed time.
(Hint: the first two constructors will extract hour, minute, and second from the elapsed time. For example, if the elapsed time is 555550 seconds, the hour is 10, the minute is 19, and the second is 9. For the no-arg constructor, the current time can be obtained using System.currentTimeMills().
Explanation / Answer
please rate - thanks
import java.util.*;
class TimeClass
{
private int hour,min,sec;
public TimeClass()
{long totalMilliseconds = System.currentTimeMillis();
// Obtain the total seconds since the midnight, Jan 1, 1970
long totalSeconds = totalMilliseconds / 1000;
// Compute the current second in the minute in the hour
sec = (int)(totalSeconds % 60);
// Obtain the total minutes
long totalMinutes = totalSeconds / 60;
// Compute the current minute in the hour
min = (int)(totalMinutes % 60);
// Obtain the total hours
long totalHours = totalMinutes / 60;
// Compute the current hour
hour = (int)(totalHours % 24);
}
public TimeClass(long milli)
{ // Obtain the total seconds since the midnight, Jan 1, 1970
long totalSeconds = milli/ 1000;
// Compute the current second in the minute in the hour
sec = (int)(totalSeconds % 60);
// Obtain the total minutes
long totalMinutes = totalSeconds / 60;
// Compute the current minute in the hour
min = (int)(totalMinutes % 60);
// Obtain the total hours
long totalHours = totalMinutes / 60;
// Compute the current hour
hour = (int)(totalHours % 24);
}
public int getHour()
{
return hour;
}
public int getMinute()
{
return min;
}
public int getSecond()
{
return sec;
}
public void setTime(long elapseTime)
{
// Compute the current second in the minute in the hour
sec = (int)(elapseTime % 60);
// Obtain the total minutes
long totalMinutes = elapseTime / 60;
// Compute the current minute in the hour
min = (int)(totalMinutes % 60);
// Obtain the total hours
long totalHours = totalMinutes / 60;
// Compute the current hour
hour = (int)(totalHours % 24);
}
}
----------------------------------------
class timeclass1tester{
public static void main(String[] args)
{TimeClass a=new TimeClass();
TimeClass b=new TimeClass(555550 ); //time in milliseconds
System.out.println("object a");
System.out.println("Hour "+a.getHour());
System.out.println("Minute "+a.getMinute());
System.out.println("Seconds "+a.getSecond());
System.out.println();
System.out.println("object b");
System.out.println("Hour "+b.getHour());
System.out.println("Minute "+b.getMinute());
System.out.println("Seconds "+b.getSecond());
System.out.println();
a.setTime(555550); //time in seconds
System.out.println("object a after set");
System.out.println("Hour "+a.getHour());
System.out.println("Minute "+a.getMinute());
System.out.println("Seconds "+a.getSecond());
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.