Modify the Time class of Fig. 7.13 to include a tick method that increments the time stored in a Time object by one second. The Time object should always remain in a consistent state. Write a driver program that tests the tick method. Be sure to test the following cases:
a) Incrementing into the next minute.
b) Incrementing into the next hour.
c) Incrementing into the next day (i.e., 23:59:59 to 0:00:00).
```
# Class Time with tick method.
class Time:
"""Class Time with default constructor"""
def __init__( self, hour = 0, minute = 0, second = 0 ):
"""Time constructor initializes each data member to zero"""
self.setTime( hour, minute, second )
def setTime( self, hour, minute, second ):
"""Set values of hour, minute, and second"""
self.setHour( hour )
self.setMinute( minute )
self.setSecond( second )
def setHour( self, hour ):
"""Set hour value"""
if 0 <= hour < 24:
self.__hour = hour
else:
raise ValueError, "Invalid hour value: %d" % hour
def setMinute( self, minute ):
"""Set minute value"""
if 0 <= minute < 60:
self.__minute = minute
else:
raise ValueError, "Invalid minute value: %d" % minute
def setSecond( self, second ):
"""Set second value"""
if 0 <= second < 60:
self.__second = second
else:
raise ValueError, "Invalid second value: %d" % second
def getHour( self ):
"""Get hour value"""
return self.__hour
def getMinute( self ):
"""Get minute value"""
return self.__minute
def getSecond( self ):
"""Get second value"""
return self.__second
def printMilitary( self ):
"""Prints Time object in military format"""
print "%.2d:%.2d:%.2d" % \
( self.__hour, self.__minute, self.__second ),
def printStandard( self ):
"""Prints Time object in standard format"""
standardTime = ""
if self.__hour == 0 or self.__hour == 12:
standardTime += "12:"
else:
standardTime += "%d:" % ( self.__hour % 12 )
standardTime += "%.2d:%.2d" % ( self.__minute, self.__second )
if self.__hour < 12:
standardTime += " AM"
else:
standardTime += " PM"
print standardTime,
def tick( self ):
"""Increments Time object by one second"""
self.__second = ( self.__second + 1 ) % 60
# if next minute
if self.__second == 0:
self.__minute = ( self.__minute + 1 ) % 60
# if next hour
if self.__minute == 0:
self.__hour = ( self.__hour + 1 ) % 24
# Exercise 7.5: ex07_05.py
# Driver for class Time with method tick.
from Time4 import Time
timeA = Time() # all default
timeB = Time( 2, 13, 59 ) # 2:13:59
timeC = Time( 21, 59, 59 ) # 21:59:59
timeD = Time( 23, 59, 59 ) # 23:59:59
print "Original times:"
print "timeA:"
timeA.printStandard()
print "\n"
print "timeB:"
timeB.printStandard()
print "\n"
print "timeC:"
timeC.printStandard()
print "\n"
print "timeD:"
timeD.printStandard()
print "\n"
# increment each time by one second
timeA.tick()
timeB.tick()
timeC.tick()
timeD.tick()
# print new times
print "timeA after one second:"
timeA.printStandard()
print "\n"
print "timeB after one second:"
timeB.printStandard()
print "\n"
print "timeC after one second:"
timeC.printStandard()
print "\n"
print "timeD after one second:"
timeD.printStandard()
print "\n"
```
Original times:
timeA:
12:00:00 AM
timeB:
2:13:59 AM
timeC:
9:59:59 PM
timeD:
11:59:59 PM
timeA after one second:
12:00:01 AM
timeB after one second:
2:14:00 AM
timeC after one second:
10:00:00 PM
timeD after one second:
12:00:00 AM
You might also like to view...
If you use the Paste Special option to link an Excel chart into a Word document, changes made in the Excel workbook will also be updated and shown in the chart contained in the Word document.
a. true b. false
By passing different arguments to the same method, that method can produce different behaviors.
Answer the following statement true (T) or false (F)