Lab 1: Numeric Operations

Goals

After the lab, you should know how to

  1. move/rename files in Linux
  2. solve simple arithmetic problems in Python

Objective: Practice Using Linux

Setting Up for Lab 1

  1. Create a directory called lab1 in your labs directory. Your programs and the output for this lab will all be saved in the lab1 directory.
  2. Copy the file area.py from the /home/courses/cs111/handouts/lab1/ into your lab1 directory.
  3. To rename files or move them from one directory to another, you can use the mv command. Move the area.py file into the file lab1.8.py using the command in the lab1 directory:
    mv area.py lab1.8.py
  4. List the contents of your directory. You should have only one file (called lab1.8.py) in the directory.

Objective: Programming in Python

We'll practice writing several Python programs, each in their own text file. Name the files lab1.1.py through lab1.8.py.

Your programs will be graded on correctness, style, and how well you tested them. Make sure you adhere to the good development and testing practices we discussed in class, use good variable names

After you've developed a correct solution to each program, close the IDLE "shell" and reopen it by running the program again (using F5), demonstrate that the program works using several good test cases, and save the output to a file named lab1.x.out, where x is the problem number.

  1. (15 pts) This problem has several parts. You will create a program in the first step and modify it for each subsequent step. Note how the progression of steps for this problem adheres to the good development practices we dicussed in class.
    1. Create three variables (i, j, and result) to calculate and display result = i² + 3j - 5 for the case where i=7 and j=2. Your code will not look exactly like this formula. Display the result and verify that it is correct.
    2. Change the name of the variable i to i21. Be sure to change the name everywhere the variable is used. Execute it to show it works.
    3. Now edit the file again and change the name to 21i. What error message do you get? Record in comments the error message. Revert your program back to the correct variable name.
    4. Modify your program so that the program prompts the user for values of i and j.

      You only need to save the output from executing the final version of your program.

  2. (10 pts) How do you measure, measure a year? The musical Rent asks how you would measure a year and suggests love as the metric. Since we don't have a primitive data type that represents love, we'll measure a year in minutes. Write a program that computes the number of minutes in a year and plugs it into part of the chorus of Seasons Of Love. Note: do not recompute the number of minutes each time it is used in the chorus. Compute the minutes once and save it in a variable that you use in the print statements.

    Output should look similar to

    525600 minutes, 525600 moments so dear.
    525600 minutes - how do you measure, measure a year? 
    
  3. (10 pts) Create a program that computes your birth year, given your age and the current year. Output should look similar to:
    This program determines your birth year
    given your age and current year
    
    Enter your age: 20
    Enter the current year: 2006
    
    You were either born in 1986 or 1985
    
  4. (10 pts) Create five variables, i, j, x, y, and result, and set i to 9, j to 2, x to 9.0 and y to 2.0. Now write a series of six pairs of assignment and print statements in one program as follows:
    1. Set result to i/j; display the result nicely
    2. Set result to j/i; display the result nicely
    3. Set result to x/y; display the result
    4. Set result to y/x; display the result
    5. Set result to i/y; display the result
    6. Set result to float(i)/j; Display the result nicely

    7. We will talk about this last one on Wednesday.
  5. (15 pts) MLB Hall of Famers. Rickey Henderson was voted into the Major League Baseball Hall of Fame last year. At one time Henderson was the career leader in three major offensive categories: runs, stolen bases, and walks. (Barry Bonds passed him in walks.)

    Henderson stole 1406 bases and was caught 335 times. Lou Brock is second with 938 stolen bases and 307 caught stealing.

    Calculate and display Henderson's and Brock's successful base stealing percentages. (Hint: Divide the number of bases successfully stolen by the number of attempts.) Then, display the difference in their percentages.

  6. (15 pts) Create a program that converts a given Fahrenheit temperature to Celsius. The formula to convert is C=5/9(F-32). Again, note that you cannot use this formula exactly in your program.

    Output should look similar to:

    This program prints the Celsius temperature
    given a Fahrenheit temperature.
    
    Enter a Fahrenheit temperature: 70
    70 degrees F is 21.111111111111111 degrees C
    

    Note: The output is not very pretty yet. We'll learn how to format the output more nicely soon.

    Note: When you demonstrate this program, especially think about what are good test cases for this program. What answers do you know?

  7. (15 pts) Write a program that demonstrates the importance of operator precedence. Your program will get three integers from a user and assign them to the variables a, b, and c. Then, print the result of a Python expression that has no parentheses, using those three numbers. Then, show a different result from the same expression with one added pair of parentheses.

    Example output (with fake numbers--I'm not showing an equation):

    Enter a: 12
    Enter b: 34
    Enter c: 5
    The result of <print out your equation here> is 51.
    The result of <print out your equation with parentheses here> is 15.
    
  8. (10 pts) Modify the area.py program to compute the area of a triangle. (How do you compute the area of a triangle? How could you figure that out if you don't know it?)

Finishing up: What to turn in for this lab

  1. Copy your lab1 directory into the turnin directory. (Review the UNIX handout if you don't remember how to do that.)
  2. Cleanup: jEdit and IDLE may make backup files. The backup files have an "~" appended to the name of your file. Delete any "~" files from your lab directory. (What is the UNIX command that you can use to delete all of them at once?)
  3. You will print out your lab and turn that in by Friday. Don't forget to write out the Honor Pledge and sign your lab. Also, staple all pages together and put your printed name at the top of the page.

    Create the printable file of your lab using the printLab.sh command.
    Recall that the command you use is
    printLab.sh <labdirpath>
    and that you should probably print from the labs directory. This command should create the file lab1.ps

    To view the file you created, use the command gv lab1.ps &

    Print the file using the lpr command introduced in the first lab.
    The command has the form
    lpr -P<printername> <filetoprint.ps>

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 1:20 p.m. on Friday.

Ask well before the deadline if you need help turning in your assignment!

Grading (100 pts)