To Home Page

IT 212 -- Lab 2

Details:

  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:
    +------------------------------------+
    |             Clock                  |
    +------------------------------------+
    | + hr : int                         |
    | + min : int                        |
    | + sec : int                        |
    +------------------------------------+
    | + __init__(self : Clock, hr : int, |
    |    min : int, sec : int)           |
    | + __str__(self : Clock) : str      |
    | + tick(self : Clock )              |
    +------------------------------------+
    
  2. The  __init__ method should initialize the values of the instance variables. Here is the beginning of __init__:
    def __init__(self, hr, min, sec):
        self.hr = 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)
    
    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 == 24.
    
  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 Clock class using assertEqual statements.
  6. If you have time, add yr, mon, and day instance variables, modify the tick method to accomodate them.

    Traditional Tests:
    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.  
    
    Unit Tests:
    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( )