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

Computer Science & Information Technology

You might also like to view...

As you insert and delete rows and columns, Excel adjusts _____ cell references in formulas to keep them accurate.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

The ________ contains the central electronic components of the computer

A) arithmetic/logic unit B) peripheral unit C) input unit D) motherboard

Computer Science & Information Technology

(Table of Powers Application) Write an application that displays a table of numbers from 1 to an upper limit, along with ethe square and cube each number. The user should specify the upper limit, and the results should be displayed in a JTextArea as in Fig. 8.23.


```
a) Copying the template to your working directory. Copy the directory C:Examples Tutorial08ExercisesTableOfPowers to your C:SimplyJava directory.
b) Opening the template file. Open the TableOfPowers.java file in your text editor.
c) Handling the keyPressed event for the Upper limit: JTextField. Add code to the limitJTextFieldKeyPressed event handler (which begins in line 97) to clear outputJTextArea.
d) Adding code to the Calculate JButton event handler. Add the code specified in steps e through j to the calculateJButtonActionPerformed event handler, which immediately follows the limitJTextFieldKeyPressed event handler.
e) Declaring a variable to store the loop counter. In the calculateJButtonAction- Performed event handler, declare int variable counter and initialize its value to 1.
f) Clearing outputJTextArea. Add a statement to calculateJButtonActionPer- formed that uses JTextArea method setText to clear any previous output in outputJTextAr

Computer Science & Information Technology

Consider following text: AACABABACBA and pattern: CBAAB What will be the bad character?

a. C (in text) b. A (in text) c. A (in pattern) d. C (in pattern)

Computer Science & Information Technology