e Time class) Design a class named Time. The class contains: 7.10 (The e private
ID: 3598200 • Letter: E
Question
e Time class) Design a class named Time. The class contains: 7.10 (The e private data fields hour, minute, and second that represent a time. A constructor that constructs a Time object that initializes hour, minute, and second using the current time. The get methods for the data fields hour, minute, and second, respectively using the elapsed time in seconds. For example, if the elapsed time is 555550 A method named setTime (elapseTime) that sets a new time for the object seconds, the hour is 10, the minute is 19, and the second is 12. Draw the UML diagram for the class, and then implement the class. Write a test program that creates a Time object and displays its hour, minute, and second Your program then prompts the user to enter an elapsed time, sets its elapsed time in the Time object, and displays its hour, minute, and second. Here is a sample run: Current time is 12:41:6 Enter the elapsed time: 55550505 Ener The hour:minute:second for the elapsed time is 22:41:45 (Hint: The initializer will extract the hour, minute, and second from the elapsed time. The current elapsed time can be obtained using time.time), as shown in Listing 2.7, ShowCurrentTime.py.)Explanation / Answer
import time class Time: def __init__(self): self.setTime(int(time.time())) def getHour(self): return self.__hour def getMinute(self): return self.__minute def getSecond(self): return self.__second def setTime(self, elapsedTime): self.__second = elapsedTime % 60 totMins = elapsedTime // 60 self.__minute = totMins % 60 totHours = (totMins // 60)-5 self.__hour = (totHours % 24) def main(): curTime = Time() print("Current time is " + str(curTime.getHour()) + ":" + str(curTime.getMinute()) + ":" + str(curTime.getSecond())) elapseTime = eval(input("Enter the elapsed time: ")) curTime.setTime(elapseTime) print("The hour:minute:second for elapsed time is " + str(curTime.getHour()) + ":" + str(curTime.getMinute()) + ":" + str(curTime.getSecond())) main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.