# ex08PhillConrad.py Define some functions involving pi, sin, cos etc. # P. Conrad for CS5nm, UCSB, Fall 2008 # 10/14/2008 # @@@ Something is missing here.. something you need in order to use # math.pi and math.fabs (remove this comment after you put it in) # First, we define functions that run our test cases # Note that \ character at the end of a line means that the line continues # on the next line # check_expect checks whether a function produces the expected result # consumes: # test: a string describing the test # check: the value we are testing (typically result of a function call) # expect: the value we are expecting (typically a literal value) # produces: # nothing (there is no return value) # side effect: # prints a message indicating whether the test passed or failed def check_expect(test,check,expect): if (check == expect): print "Test " + test + " passed." else: print "Test " + test + " failed: expected " + str(expect) + \ " but I got " + str(check) # check_expect_within_tolerance # checks whether a function produces the expected result # but allows some degree of inaccuracy (a "tolerance") # in cases where calculating the exact result may not be feasible # (e.g. quantities involving pi, square roots, etc.) # consumes: # test: a string describing the test # check: the value we are testing (typically result of a function call) # expect: the value we are expecting (typically a literal value) # toleranace: the largest inaccuracy that is permitted # produces: # nothing (there is no return value) # side effect: # prints a message indicating whether the test passed or failed def check_expect_within_tolerance(test,check,expect,tolerance): if (math.fabs(check-expect) <= tolerance): print "Test " + test + " passed (with tolerance " + \ str(tolerance) + ")" else: print "Test " + test + " failed: expected " + str(expect) + \ " but I got " + str(check) + " (tolerance of " + \ str(tolerance) + " exceeded)" # Next define a function for the perimeter of a rectangle # consumes: radius of the circle # produces: area of the circle def areaOfCircle(radius): return math.pi * radius ** 2 # Next, here are some test cases for areaOfCircle. # These test cases are likely to fail because we have not allowed # for the fact that the estimate of pi used in the calculation is # less accurate than the one that Python uses. check_expect("areaOfCircle test 1", areaOfCircle(1), 3.14159) check_expect("areaOfCircle test 2", areaOfCircle(2), 12.56636) # Here, by contrast are some test cases for areaOfCircle that will pass: check_expect_within_tolerance("areaOfCircle test 3", areaOfCircle(1), 3.14159, 0.0001) check_expect_within_tolerance("areaOfCircle test 4", areaOfCircle(2), 12.56636, 0.0001) # Next, there are two functions that you must finish. # In each case, one test case has been written for you. # Add at least two additional test cases. # It is better to write the test cases BEFORE you write the functions. # Use the internet to look up the correct formulas, and use a calculator # to compute the correct values. Choose an appropriate tolerance # degreesToRadians converts degrees to radians # consumes: degrees, the number of degrees in some angle # produces: radians, the number of radians in an equivalent angle def degreesToRadians(degrees): return -1; # a stub @@@ REPLACE THIS WITH THE CORRECT RESULT check_expect_within_tolerance("degreesToRadians test 1", degreesToRadians(180), 3.14159, 0.0001) # 180 degrees is "pi" radians # @@@ ADD TWO MORE TEST CASES HERE, THEN REMOVE THIS COMMENT # distanceTravelled figures out how far you travelled, given rate and time # consumes: # rate (the speed at which you are travelling, e.g. in mph) # time (the amount of time you have been travelling at that speed, e.g. in hours) # produces: # the distance travelled # Note that the units must be consistent between the rate, time and distance, # e.g. if you use hours, you must always use hours, if you use seconds, you must # always use seconds, and if you use meters or miles, you must the same units # throughout def distanceTravelled(rate, time): return -1 # stub check_expect_within_tolerance("distanceTravelled test case 1", distanceTravelled(50, 2), 100, 0.000001) # @@@ ADD TWO MORE TEST CASES HERE, THEN REMOVE THIS COMMENT # Finally, add a function here to compute gas mileage. # Add a comment before the function that indicates what the function # does, and what it consumes and what it produces. # What a function "consumes" is a description of all of its parameters # What a function "produces" is a description of what it returns. # @@@ ADD COMMENT ABOUT YOUR GAS MILEAGE FUNCTION HERE # @@@ ADD FUNCTION DEFINITION FOR GAS MILEAGE FUNCTION HERE # @@@ ADD TEST CASES FOR GAS MILEAGE FUNCTION HERE ( at least two)