def add_time1(time1, time2): """ Returns the sum of time1 and time2 as a new Time object DO NOT ALTER time1 or time2, even though they are mutable Examples: The sum of 12hr 13min and 13hr 12min is 25hr 25min The sum of 1hr 59min and 3hr 2min is 4hr 1min Parameter time1: the starting time Precondition: time1 is a Time object Parameter time2: the time to add Precondition: time2 is a Time object """ a = clock.Time(time1.hours, time1.minutes) b = clock.Time(time2.hours, time2.minutes) sum_hours = a.hours + b.hours sum_minutes = a.minutes + b.minutes if sum_minutes > 60: sum_minutes = sum_minutes - 60 sum_hours = sum_hours + 1 return clock.Time(sum_hours,sum_minutes)
def test_add_time1(): """ Test procedure for the function add_time1() """ print('Testing add_time1()') # TEST 1: Sum 12hr 13min and 13hr 12min time1 = clock.Time(12, 13) time2 = clock.Time(13, 12) result = funcs.add_time1(time1, time2) introcs.assert_equals(clock.Time, type(result)) introcs.assert_equals(25, result.hours) introcs.assert_equals(25, result.minutes) # Verify no objects were modified introcs.assert_equals(12, time1.hours) introcs.assert_equals(13, time1.minutes) introcs.assert_equals(13, time2.hours) introcs.assert_equals(12, time2.minutes) # TEST 2: Sum 1hr 59min and 1hr 2min time1 = clock.Time(1, 59) time2 = clock.Time(3, 2) result = funcs.add_time1(time1, time2) introcs.assert_equals(clock.Time, type(result)) introcs.assert_equals(5, result.hours) introcs.assert_equals(1, result.minutes) # Verify no objects were modified introcs.assert_equals(1, time1.hours) introcs.assert_equals(59, time1.minutes) introcs.assert_equals(3, time2.hours) introcs.assert_equals(2, time2.minutes) # TEST 3: Sum 1hr 15min and 0hr 0min time1 = clock.Time(1, 15) time2 = clock.Time(0, 0) result = funcs.add_time1(time1, time2) introcs.assert_equals(clock.Time, type(result)) introcs.assert_equals(1, result.hours) introcs.assert_equals(15, result.minutes) # Verify no objects were modified introcs.assert_equals(1, time1.hours) introcs.assert_equals(15, time1.minutes) introcs.assert_equals(0, time2.hours) introcs.assert_equals(0, time2.minutes)
def test_add_time2(): """ Test procedure for the function add_time2() """ print('Testing add_time2()') # TEST 1: Sum 12hr 13min and 13hr 12min time1 = clock.Time(12, 13) time2 = clock.Time(13, 12) # Verify that nothing is returned result = funcs.add_time2(time1, time2) introcs.assert_equals(None, result) # Verify time1 is changed, but time2 is not introcs.assert_equals(25, time1.hours) introcs.assert_equals(25, time1.minutes) introcs.assert_equals(13, time2.hours) introcs.assert_equals(12, time2.minutes) # TEST 2: Sum 1hr 59min and 1hr 2min time1 = clock.Time(1, 59) time2 = clock.Time(3, 2) # Verify that nothing is returned result = funcs.add_time2(time1, time2) introcs.assert_equals(None, result) # Verify time1 is changed, but time2 is not introcs.assert_equals(5, time1.hours) introcs.assert_equals(1, time1.minutes) introcs.assert_equals(3, time2.hours) introcs.assert_equals(2, time2.minutes) # TEST 3: Sum 1hr 15min and 0hr 0min time1 = clock.Time(1, 15) time2 = clock.Time(0, 0) # Verify that nothing is returned result = funcs.add_time2(time1, time2) introcs.assert_equals(None, result) # Verify that objects are correct introcs.assert_equals(1, time1.hours) introcs.assert_equals(15, time1.minutes) introcs.assert_equals(0, time2.hours) introcs.assert_equals(0, time2.minutes)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Mon Apr 27 14:45:11 2020 @author: devinpowers """ import clock A = clock.Time(12, 25, 30) print(A) print(repr(A)) print(str(A)) print() B = clock.Time(2, 25, 3) print(B) print(repr(B)) print(str(B)) print() C = clock.Time(2, 25) print(C) print(repr(C)) print(str(C)) print()
import clock help(clock.Time) A = clock.Time() A print(A) A = clock.Time(10, 40, 30) print(A)
import clock help(clock) help(clock.Time) A = clock.Time(1, 2, 43) print(A)
import clock A = clock.Time() A.from_str('12:45:30') print(A.__str__()) print(A.__repr__())
import clock #creating an object of Time #by passing three params #it will invoke the __init__ print "calling __init__ function member\n" A = clock.Time(23, 23, 12) #printing the time by #calling the __repr__ print "calling __repr__ function member" print repr(A) #setting new time by #calling the from_str #by passing one param print "\ncalling from_str function member" A.from_str("12:23:45") #finally calling the __str__ #it will invoke inplicitly #on object print "\ncalling __str__ function member" print A #hence all four defined in clock.py #getting called