In: Computer Science
PYTHON ASSIGNMENT
Problem:
(1) The __init__ method should initialize the values of the instance variables. Here is the beginning of __init__:
def __init__(self, the_hr, the_min, the_sec): self.hr = the_hr # Also initialize min and sec.
(2) Include a __str__ method that returns the current state of the clock object as a string. You can use the string format method format like this:
return "{0:02d}:{1:02d}:{2:02d}".format(self.hr, self.min, self.sec)
(3) When the tick method is executed, add one to sec. If the resulting value of sec is 60, set it back to 0 and update min and hr if necessary. Here is the beginning of the tick method:
def tick( ): self.sec += 1 if self.sec == 60: self.sec = 0 self.min += 1 # Also check if self.min == 60 and # and if self.hr == 0.
(4) Write testing code in test1.py to test your class. Use this import statement at the top:
from clock import Clock
(5) Also write a unit test file (test2.py file) for all of the methods in your Clockclass using assert statements.
(6) If you have time, add yr, mon, and day instance variables, modify the tickmethod to accomodate them.
I need the answer to 5 and 6
Clock.py:
Raw_code:
class Clock:
def __init__(self, the_hr, the_min, the_sec):
self.hr = the_hr
self.min = the_min
self.sec = the_sec
def __str__(self):
return "{0:02d}:{1:02d}:{2:02d}".format(self.hr, self.min,
self.sec)
def tick(self):
self.sec += 1
if (self.sec == 60):
self.min += 1
self.sec = 0
if (self.min == 60):
self.hr += 1
self.min = 0
return self.__str__()
test1.py:
Raw_code:
from clock import Clock
#creating 3 Clock objects and calling tick method and printing
clock object
c1 = Clock(1, 20, 22)
c1.tick()
print(c1)
c2 = Clock(1, 30, 59)
c2.tick()
print(c2)
c3 = Clock(1, 59, 59)
c3.tick()
print(c3)
test2.py:
Raw_code:
from clock import Clock
# creating 3 class Object and using assert statement
c1 = Clock(1, 20, 22)
c1.tick()
# assert, comparing string with return type of __str__ method of
clock object
assert "01:20:23" == c1.__str__()
# comparing string with return type of tick() method of clock
object,
# tick method changes data variables and returns the resultant
string
assert "01:20:24" == c1.tick()
c2 = Clock(1, 30, 59)
c2.tick()
assert "01:31:00" == c2.__str__()
assert "01:31:01" == c2.tick()
c3 = Clock(1, 59, 59)
c3.tick()
assert "02:00:00" == c3.__str__()
# failing assert statement for testing
assert "02:00:00" == c3.tick()
#here the return value tick() method is "02:00:01"
Clock class after adding year, month, day:
clock2.py:
Raw_code:
class Clock:
#intializing required data variables
def __init__(self, yr, mon, day, the_hr, the_min, the_sec):
self.yr = yr
self.mon = mon
self.day = day
self.hr = the_hr
self.min = the_min
self.sec = the_sec
def __str__(self):
return "{0:4d}-{1:02d}-{2:02d};
{3:02d}:{4:02d}:{5:02d}".format(self.yr, self.mon, self.day,
self.hr, self.min, self.sec)
def tick(self):
self.sec += 1
if (self.sec == 60):
self.min += 1
self.sec = 0
if (self.min == 60):
self.hr += 1
self.min = 0
self.sec = 0
# if hours == 24 incrementing day
if (self.hr == 24):
self.day += 1
self.hr = 0
self.min = 0
self.sec = 0
# incrementing month as per condition required for month
# if it is eight month
if (self.mon == 8):
#since, day is incremented in above if statement
# so if the eigth month days is 32, month is incremented
if (self.day == 32):
self.mon += 1
self.day = 1
self.hr = 0
self.min = 0
self.sec = 0
# if the month is second month, checking leap year condition
elif (self.mon == 2):
if (self.yr % 4) == 0:
if (self.yr % 100) == 0:
if (self.yr % 400) == 0:
if(self.day == 30):
self.mon += 1
self.day = 1
self.hr = 0
self.min = 0
self.sec = 0
else:
if(self.day == 29):
self.mon += 1
self.day = 1
self.hr = 0
self.min = 0
self.sec = 0
else:
if(self.day == 30):
self.mon += 1
self.day = 1
self.hr = 0
self.min = 0
self.sec = 0
else:
if(self.day == 29):
self.mon += 1
self.day = 1
self.hr = 0
self.min = 0
self.sec = 0
# if the month is divisible by 2 and days are 31(last day - 30),
incrementing month
elif (self.mon % 2 == 0):
if (self.day == 31):
self.mon += 1
self.day = 1
self.hr = 0
self.min = 0
self.sec = 0
# if the month is not divisible by 2 and days are 32(last day -31),
incrementing month
else:
if (self.day == 32):
self.mon += 1
self.day = 1
self.hr = 0
self.min = 0
self.sec = 0
# if month is 12th month , incrementing year
if (self.mon == 12):
self.yr += 1
self.mon = 1
self.day = 1
self.hr = 0
self.min = 0
self.sec = 0
test3.py:
Raw_code:
from clock2 import Clock
# normal month with 30 day
c1 = Clock(2000, 4, 30, 23, 59, 59)
c1.tick()
print(c1, end = "\n\n")
# normal month with 31 day
c2 = Clock(2000, 1, 31, 23, 59, 59)
c2.tick()
print(c2, end = "\n\n")
# 2 month in leap year
c3 = Clock(2000, 2, 28, 23, 59, 59)
c3.tick()
print(c3, end = "\n\n")
# 2 month in leap year
c4 = Clock(2000, 2, 29, 23, 59, 59)
c4.tick()
print(c4, end = "\n\n")
# 2 month of not leap years
c5 = Clock(2006, 2, 28, 23, 59, 59)
c5.tick()
print(c5, end = "\n\n")
# 12 month
c6 = Clock(2010, 12, 31, 23, 59, 59)
c6.tick()
print(c6, end = "\n\n")