echo $title ?>
Goals
After the lab, you should be proficient at
- using functions within your program
- refactoring your code to use functions
- writing data to files
- reading numeric data from files
Objective: Review
Review the slides for today's lab.
Objective: Set Up
- Set up the help client:
For the most recent version of the help client, run
java -jar /csdept/local/courses/cs111-01/shared/labhelp.jar
Or, run
runHelpClient.sh &
- Copy
/csdept/local/courses/cs111-01/handouts/lab7
and all of its contents, recursively, into yourcs111
directory. This way, you don't have to create thelab7
directory first. Otherwise, you could first create thelab7
directory and then copy all of the contents of the/csdept/local/courses/cs111-01/handouts/lab7
directory (i.e., add a * to thecp
command) into yourlab7
directory.
Objective: Programming in Python
We'll practice writing several Python programs, each in their own text file.
Your programs will be graded on correctness, style, efficiency, and how well you tested them. Make sure you adhere to the good development and testing practices we discussed in class. Your code should be readable and your output should be useful and well-formatted.
After you've developed a correct solution to each program, restart
IDLE or close and reopen the IDLE "shell" by running the program again
(using F5), demonstrate that the program works using
several
- (15) Open lab7.1.py, which you copied at the beginning of lab. Read through the functions in the file and their documentation. Then, modify the program so that it implements what is described in the comments, e.g., draws and moves bugs, using functions defined within the program.
- (20) Several weeks ago, we
wrote
speedingticket.py
, which calculated a fine for speeding. You copied this program when you copied thelab7
directory earlier. Refactor the code by defining a function that takes as parameters the speed limit and the clocked speed and returns the computed fine. (What should the calculated fine be if the person wasn't speeding?) The code that calls the function willprint
an appropriate message based on the returned fine. Write an appropriate comment for the function you defined.Test the function using a test function, as we discussed in class, to make sure that the function returns appropriate values.
Then, put the driver part of the program (i.e., the part that gets input from the user, calls the function, and displays the output) into a
main
function.Refactoring is a common practice in coding. You write some code, test it, and then reorganize it so that it's better organized and easier to expand/maintain.
Have the instructor or a student assistant review your code before moving on to discuss appropriate organization and commenting.
- (20) Copy the Caesar Cipher program from the last lab where you read from
a file and modify it so that it has at least three new functions. (If your
Caesar Cipher program didn't work correctly or you want to see my version of
the program, you can start with this one.)
One function will handle the encoding. This function takes as parameters a string and a key and returns the encoded string. Modify your program to use this function. Test the encoding function to ensure that it is working correctly.
Put the driver into a
main
function. Then, test the program to make sure the program is still working correctly.Then, modify your program to have a function called
translateLetter
that takes as parameters a letter and a key and returns the translated the letter. Call thetranslateLetter
function in the encoding function you just wrote.Finally, write comments that compare the readability of the program before and after you added the functions.
- (15) Copy the previous program and modify it so that it writes the encoded
message to a file. The program should take as input the encoded message output
file's name. You may want to assume that the output files always get written
to the
data
directory so that you don't clutter yourlab7
directory with files.The idea is that now you can encode files that you can give to others to decode (and vice versa).
Note: I broke up a larger problem (extending the Caesar cipher program) into multiple, smaller problems, each of which is easier to tackle than tackling the whole problem all at once. This is an important problem-solving skill to develop. You may feel like breaking the problem into smaller problems slows you down, but it usually doesn't because you spend less time debugging the smaller pieces than if you had tackled it all at once. You'll get lots of practice solving problems in this class--including on the next problem!
- (30)
Break this problem into pieces (what are those pieces?) and
tackle them one at a time. Use top-down design, as appropriate. Create functions that solve each piece.
The system of scoring an Olympic gymnast is based on two separate panels of scores. The A panel judges the requirements, difficulty, and connections of a routine. The scoring starts at zero and then adds points accordingly. The B panel judges the execution of a routine, and the scoring starts at 10 with points deducted accordingly for execution and for any applicable violations such as stepping out of bounds or being over the time limit.
For each panel of judges, the lowest and highest scores are dropped (to prevent judges from biasing the results) and then averaged. The two panels are then added together for the final score. A very good score will range in the 15s and 16s.
Your task: Read in a file containing the gymnastics scores. (The file name can be a constant in your program.) The first line of the file is the average difficulty score. The remaining six lines are the judges' execution scores. (You cannot assume that the execution scores are sorted.) Calculate the average score, as described above. Display the judges' scores and the final score.
Example input file:
5.7 8.3 9.1 8.0 8.9 8.8 8.5
Example output:
Gymnastics Scores for data/scores.dat -------------------------------------------------------- Judges Execution Scores: 8.0 8.3 8.5 8.8 8.9 9.1 Average Execution Score: 8.625 Average Difficulty Score: 5.7 The Final Score: 14.325
Your output does not need to display the scores in sorted order. However, the output should be formatted as above.
Example files are
data/nastia.dat
anddata/shawn.dat
Finishing up: What to turn in for this lab
- Copy
your
lab7
directory into yourturnin
directory. - Before printing, remove
the
graphics.pyc
file from yourlabs/lab7
directory, if it exists, and move thegraphics.py
file out of thelab7
directory so that you don't print this large file. include("printing.html"); ?>
Labs are due at the beginning of Friday's class. You should hand
in the printed copy at the beginning of class, and the electronic
version should be in the turnin
directory before 2:20
p.m. on Friday.
Ask well before the deadline if you need help turning in your assignment!
Grading (100 pts)
- Python programs: 100 pts; see above for breakdown