|
First Summer 2006 |
Today's lab will be about the material in Chapter 8 in the Python text.
The lab material and the lab assignment will be posted here shortly.
This is your lab assignment for today, Wednesday May 24, 2006:Write a program to solve the Clock problem.
- Define a class of objects called Clock.
- An object of type Clock stores time (hours and minutes) in military time format, in two instance variables of type int.
- Objects of type Clock have two instance methods:
(a) report which is simply returning the time, and
(b) tick which advances the clock by one minute.- The constructor for class Clock takes one argument, a String that represents the time the clock is originally set to.
Write a test program too, that illustrates how your objects are working.
Turn in your programs in OnCourse (under A201-7055) by the end of the day.
class Clock(object):
def __init__(self, time):
self.hour = int(time[0:2])
self.minute = int(time[3:])
def tick(self):
self.minute = (self.minute + 1) % 60
if self.minute == 0:
self.hour = (self.hour + 1) % 24
self.report()
def report(self):
print ("00" + str(self.hour))[-2:] + ":" + ("00" + str(self.minute))[-2:]
clock = Clock("23:58")
clock.tick()
clock.tick()
clock.tick()
clock.tick()
clock.tick()This problem should be discussed in lab today.
Here are some more problems we could develop:
http://www.cs.indiana.edu/classes/a201-dger/spr2005/notes/007.html
I particularly like the Tigger and Robot problems. I will post what we do in class today, and I will come to lab for the first half an hour or so.