Design a class named Clock. The class contains private data fields for startTime
ID: 641659 • Letter: D
Question
Design a class named Clock. The class contains private data fields for startTime and stopTime, a no argument constructor that initializes the startTime to the current time, a method named start() that resets the startTime to the given time, a stop() method that sets the endTime to the given time and a getElapsedTime() method that returns the elapsed time in seconds. Create a TestClock class to construct a Clock instance and return the elapsed time. Command line arguments should be used to send the start and end times. You should use the java.time classes. Here is sample run: java TestClock 11:45:12 11:48:13 Elapsed time in seconds is: 181
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.