CS5nm, Fall 2008

ex05: writing and testing simple math functions

 


Goals for this lab

By the time you have completed this lab, you should be able to

Step by Step Instructions

Step 0: Open IDLE to the usual starting point

See lab01, step 3, for instructions on bringing up IDLE.

Now, arrange your IDLE windows to the "usual starting point". By this, we mean

This particular arrangement is only a suggestion—you can actually arrange the windows any way you like. But, if you are new to programming, keeping things organized like this may help you remember what to do, because this is the way Prof. Conrad organizes things during lecture.

It will also be the "usual starting point" we refer to in the exercises, so it will be helpful to develop this habit.

Step 1: Testing some partially finished code

The program at the link below contains three function definitions, and some test cases. From the comments you can see that there there are functions for three problems, along with test cases for those functions:

http://www.cs.ucsb.edu/~pconrad/cs5nm/08F/ex/ex05/ex05PhillConrad.py

Some of these functions are correct, and some are incorrect. To find out which, we can run the program and look at the test cases.

Copy and paste this program from the web into the untitled window, and run the program. When you save the program, save it with a name like this (substituting in your own name, and following the pattern.)

Once you save this and run it, you should get output similar to the following:

IDLE 1.2.2      
>>> ================================ RESTART ================================
>>>
Test perimRect test 1 passed.
Test perimRect test 2 passed.
Test perimRect test 3 passed.
Test FtoC test 1 failed: expected 100 but I got 194.222222222
Test FtoC test 2 failed: expected 0 but I got 14.2222222222
Test FtoC test 3 failed: expected 20 but I got 50.2222222222
Test areaRect test 1 failed: expected 6 but I got -1
Test areaRect test 2 failed: expected 10 but I got -1
Test areaRect test 3 failed: expected 9 but I got -1
>>>

The output above indicates that the tests for the perimeter of a rectangle function pass, but the ones for other other two fail. Your next job in this exercise is to fix the two broken functions. We'll do that in the next step.

Step 2: Fix the code

Step 2a: Change the comment at the top

At the top of the file is a comment that looks like this:

# ex5PhillConrad.py     Exercise 5 for CS5nm, Fall 2008, Phill Conrad, UCSB
# define several functions, and test them

Change this comment to have the correct name of your file, your name, and today's date.

Note: From now on, you should always do that in this course whenever you make changes to a file and submit it. This is an important part of documenting your program, and is one of the things you'll be graded on, so try to remember this. (For a while, I'll remind you, but after a few exercises, I'll expect you to have learned to do this without a reminder.)

Step 2b: Fix the body of the function FtoC

The function definition for FtoC has an error in it. Use the internet to look up the correct formula, and change the return statement so that the function works correctly.

Step 2c: Understanding "stubs", and fixing areaOfRectangle

The function body for areaOfRectangle is currently just:

    return -1  # stub

This is a special kind of function body. We call it a stub.

Some facts about stubs:

Using a stub allows us to "shake the ladder"—that is, to see if our safety systems are going to keep us safe. In this case, the "safety system" we are referring to is our set of "test cases".

Your job is to change this stub to a proper function body. To do that you'll replace the -1 with some expression involving length and width that actually computes the area of a rectangle (and remove the # stub comment.)

Do that now, so that the test cases pass.

Step 3: Adding a function definition

Now, add a function definition of your own, along with some test cases for it.

The name of your function should be areaOfTriangle. The parameters to the function will be base and height. The formula is "one half of the base times the height" (you'll need to convert that into Python notation).

In addition to writing the function, write at least three test cases. For now, you can assume for now that the numbers passed in for base and height will be positive numbers, so you don't have to worry about "weird" things, like if the numbers passed in are negative or zero.

You should come up with your own test cases.

You might like to start by writing a stub, and making sure the test cases fail. You can then fill in the body with a correct definition of the function so that the test cases pass.

When all your test cases pass, you are finished, and ready to submit.

Step 4: Submitting on GauchoSpace

Step4a: Making a clean run

Now, to make a transcript of your work, save all your work, and quit out of IDLE.

Go back into IDLE, and open up your saved file.

Run the file, so that we see the tests passing in your Python Shell Window.

Step4b: Saving this to a transcript file

Then, make a transcript file (just like you did in lab01), like this:

While you are in the IDLE window, not the window with your function defintions:

Step 4c: Submit your work on GauchoSpace.

Submit the files ex5YourName.py and ex5.transcript.txt on GauchoSpace as your submission for Exercise ex05.

Then you are finished with this exercise!


Evaluation and Grading (100 pts)

Due Date: 10/15/2008 (5pm)

Ex05 is due next Wednesday at 5pm. You should try to complete as much of them as you can during today's lab. If you cannot complete them, you can work on them during the week. If you have questions, come to office hours for help:

You can also ask questions during next week's lab, but ideally you should be finished or "almost finished" with both Ex05 when you come to lab next week.


Copyright 2008, Phillip T. Conrad, CS Dept, UC Santa Barbara. Permission to copy for non-commercial, non-profit, educational purposes granted, provided appropriate credit is given; all other rights reserved.