Q:

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

0

(The Time class) 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 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.)

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*********************************************************************************
* (The Time class) 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 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.)                                                         *
*********************************************************************************/
public class Exercise_10_01 {
	/** Main method */
	public static void main(String[] args) {
		// Create two Time objects
		Time time1 = new Time();
		Time time2 = new Time(555550000);

		// Display Time objects hour, minute, and 
		// second in the format hour:minute:second 
		System.out.println(time1.getHour() + ":" + time1.getMinute() + 
			":" + time1. getSecond());
		System.out.println(time2.getHour() + ":" + time2.getMinute() + 
			":" + time2. getSecond());
	}
}

Time.java 

                //UML diagram
/************************************************
*                      Time                     *
*-----------------------------------------------*
* -hour: long                                   *
* -minute: long                                 *
* -second: long                                 *
* Time()                                        *
* Time(elapseTime: long)                        *
* Time(hour: long, minute: long, second: long)  *
* getHour(): long                               *
* getMinute(): long                             *
* getSecond(): long                             *
* setTime(elapseTime: long)                     *
************************************************/

// Implement Time class
public class Time {
	// Data fields
	private long hour;
	private long minute;
	private long second;

	// Creates a Time object for the current time
	Time() {
		this(0);	
	}

	// Constructs a Time object with a specified elapsed 
	// time since midnight, January 1, 1970, in milliseconds.
	Time(long elapseTime) {
		setTime(elapseTime);
	}

	// Constructs a Time object with the specified hour, minute, and second
	Time(long hour, long minute, long second) {
		this.hour = hour;
		this.minute = minute;
		this.second = second;
	}

	// Return hour
	public long getHour() {
		return hour;
	}

	// Return minute
	public long getMinute() {
		return minute;
	}

	// Return second
	public long getSecond() {
		return second;
	}

	// Sets a new time for the object using the elapsed time
	public void setTime(long elapseTime) {
		long totalMilliseconds = System.currentTimeMillis();
		long totalSeconds = totalMilliseconds / 1000;
		second = totalSeconds % 60;
		long totalMinutes = totalSeconds / 60;
		minute = totalMinutes % 60;
		long totalHours = totalMinutes / 60;
		hour = totalHours % 24;

		if (elapseTime > 0) {
			totalSeconds = elapseTime / 1000;
			second = totalSeconds % 60;
			totalMinutes = totalSeconds / 60;
			minute = totalMinutes % 60;
			totalHours = totalMinutes / 60;
			hour = totalHours % 24;
		}
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now