Design a class named Time. The class contains: Data fields hour, minute, and sec
ID: 3690987 • Letter: D
Question
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. Must Be Written In C++ Only Need Help Constructing the Class Portion of it
Explanation / Answer
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; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.