In C++ language: Design a class named Time. The class contains: Data fields hour
ID: 3691802 • Letter: I
Question
In C++ language:
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.
A constructor that constructs a Time object with a specified elapse time since the middle of the night, Jan 1, 1970, in seconds.
A constructor that constructs a Time object with the specified hour, minutes, and second.
Three get functions for the data fields hour, minute, and second.
A function named setTime(int elapseTime) that sets a new time for the object using the elapsed item.
REQUIRED:
Draw the UML diagram for the class. Implement the class. Write a test program that creates two Time objects, one using a no-arg constructor and the other using Time(555550), and display their hour, minute, and second.
(Hint: The first two constructors will extract hour, minute, and second from the elapse time. For example, if the elapse 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 time(0), as shown in Listing 2.9. ShowCurrentTIme.cpp.)
Explanation / Answer
class timeclass1tester{
public static void main(String[] args)
{TimeClass a=new TimeClass();
TimeClass b=new TimeClass(555550000); //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();
}
}
-----------------------------------
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);
}
}