Question: PYTHON BASIC CODING Goal: Implement and test a Clock class. This class will be defined in the file clock.py. The hr instance variable records time

PYTHON BASIC CODING

Goal: Implement and test a Clock class. This class will be defined in the file clock.py. The hr instance variable records time on a 24 hour clock: 0 is midnight, 6 is 6am, 12 is noon and 23 is 11pm.

Deliverables: Source code for clock.py.

Source code for test1.py.

Source code for test2.py.

Problem:

(1) Create a UML diagram for a clock with instance variables hr, min, and sec with corresponding accessor variables. (You don't need to submit the UML diagram.) Here is the Clock UML diagram:

(2) 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. 

(3) 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) 

(4) 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. 

(5) Write testing code in test1.py to test your class. Use this import statement at the top:

from clock import Clock 

(6) Also write a unit test file (test2.py file) for all of the methods in your Clockclass using assert statements.

(7) If you have time, add yr, mon, and day instance variables, modify the tickmethod to accomodate them.

----------------

Example Traditional Test:

from clock import Clock c1 = Clock(4, 7, 3) print(c1) print(c1.hr, c1.min, c1.sec) c1.tick( ) print(c1) c2 = Clock(4, 7, 59) c2.tick print(c2) # Also create clocks c3 and c4 set to # 04:59:59 and 23:59:59, respectively. # Call the tick method for each of these # clocks and print them to make sure that # tick works correctly for all cases. 

Example Unit Test:

from clock import Clock import unittest class MyUnitTestClass(unittest.TestCase): def test_1(self): c1 = Clock(4, 7, 3) self.assertEqual(str(c1), "04:07:03") self.assertEqual(c1.hr, 4) self.assertEqual(c1.min, 7) self.assertEqual(c1.sec, 3) c1.tick( ) self.assertEqual(str(c1), "04:07:04") def test_2(self): c2 = Clock(4, 7, 59) self.assertEqual(str(c2), "04:07:59") c1.tick( ) self.assertEqual(str(c2), "04:08:00") # Also write test_3 and test_4 # methods that test clocks c3 and c4 # that start at 04:59:59 and # 23:59:59, respectively. if __name__ == '__main__': unittest.main( )
Clock + hr int min: int + sec int (the_hr int, the_min: int, the_sec int) +str tick) ( ) : str Clock + hr int min: int + sec int (the_hr int, the_min: int, the_sec int) +str tick) ( ) : str

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!