IN PYTHON PLEASE Implement a class Time. A Time object has the instance variable
ID: 3840508 • Letter: I
Question
IN PYTHON PLEASE
Implement a class Time. A Time object has the instance variables of hour and minute. It also has the methods resetTime (sets the Time to midnight), addMinute (adds a minute to the current time), and printTime (prints the current time).
Note: set the current time in your constructor to 9:00 pm.
The output should be (in military time):
The current time is: 21:00
The reset time is: 00:00
The time in 3 minutes is: 00:03
Remember: you need to account for changing the hour if minute is added to 59
Explanation / Answer
class Time:
def __init__(self,time_text):
data = time_text.split(" ")
time = data[0].split(":")
self.hour = 0
if(data[1]=="PM" or data[1]=="pm"):
self.hour = (int(time[0])+12)%24
else :
self.hour = int(time[0])
self.minute = int(time[1])
def addMinute(self,m):
self.hour = self.hour + (m/60)
self.minute = self.minute + (m%60)
def __str__(self):
return ""+str(self.hour)+":"+str(self.minute)+" "
time1 = Time("9:10 pm")
time2 = Time("9:10 am")
time3 = Time("12:10 pm")
print "time1: "+str(time1)
print "time2: "+str(time2)
print "time3: "+str(time3)