CS 8, Spring 2017

Programming Project 1

Due: Sunday, April 23, 11:59pm
Worth: 100 points

    Before you begin:
  1. Read all of Miller/Ranum chapters 1 and 2.
  2. Obtain copies of these four files: Either download them from here, or take copies from ~mikec/cs8/p1/ at CSIL, and save them in whatever folder you choose to work on this project (presumably ~/cs8/p1/).
  3. Edit the comments at the beginnings of these files to include your name (and your partner's name if you are working with another student) and the date each program is written. This is IMPORTANT, and it applies to all future programs too!
Note: Parts of this project are based on exercises in the text. To the extent that instructions here differ from those in the text, the instructions on these web pages take precedence.
  1. (25 points: some calculations) Add the following four functions to sphere.py (three of them are taken from Exercises 2.1, 2.2 and 2.3 on page 81 of the text, and the other is a natural addition):
    1. circumference(r) - to return the circumference of a circle with radius r. [2πr]
    2. area(r) - to return the area of a circle with radius r. [πr2]
    3. surface(r) - to return the surface area of a sphere with radius r. [4πr2]
    4. volume(r) - to return the volume of a sphere with radius r. [4πr3/3]
    Notes:
    • The function headers must exactly match the ones above, or the testing code will not work.
    • Use math.pi (after importing the math module) for the value of π in all of these calculations.
    • We suggest you test them one at a time in the Python shell. When they are all done, you can run the module. Here are two sample runs to which you can compare your results:
      -bash-4.2$ python3 sphere.py 
      enter radius: 2.5
      circumference: 15.7
      circle area: 19.6
      sphere volume: 65.4
      sphere surface area: 78.5
      -bash-4.2$ python3 sphere.py 
      enter radius: 10
      circumference: 62.8
      circle area: 314.2
      sphere volume: 4188.8
      sphere surface area: 1256.6
  2. (25 points: slightly more complicated calculations) Add two functions to screensize.py to calculate the height and width of a display screen, given the diagonal screen size (D) and the aspect ratio (W:H), using these formulas:
    1. height(D, W, H) - where D, W and H are described above.
    2. width(W, H, screenHeight) - where W and H as above, and screenHeight is the result of the height function.
    When done, your results should match the following sample runs from our solution.
    -bash-4.2$ python3 screensize.py 
    enter D, W, H (separated by commas): 51, 4, 3
    width = 40.8, height = 30.6
    -bash-4.2$ python3 screensize.py 
    enter D, W, H (separated by commas): 64, 16, 9
    width = 55.8, height = 31.4
  3. (25 points: accumulator pattern) Write the function specified in Exercise 2.7 on page 81 of the text to approximate π, and add it to mypi.py. You must use the equation described in that exercise, and the function header must exactly match the following:
    mypi(n) - where n is the number of terms to include.
    • If n is equal to 1, then just include the first term in the equation: (1). If n is equal to 4, include the first four terms exactly as they are shown in the exercise description on page 81. And so on as n increases.
    • When done, your results should match the following sample runs from our solution.
      -bash-4.2$ python3 mypi.py 
      enter number of terms: 10
      estimate of pi is 3.1415905109
      -bash-4.2$ python3 mypi.py
      enter number of terms: 1 
      estimate of pi is 3.4641016151
      -bash-4.2$ python3 mypi.py
      enter number of terms: 25
      estimate of pi is 3.1415926536
  4. (25 points: another calculation, and selection) Write a function to calculate a person's body mass index (BMI), and a function to categorize this index, and add them both to bmi.py.
    1. bmi(inches, pounds) - returns the index using the formula below.
    2. category(index) - returns the string category of the index.
    At left is the formula for calculating BMI, given a person's weight in kilograms and height in meters.
  5. Go to CSIL (in person unless you can manage this step remotely without any assistance from us). Open a terminal window, cd to the same directory as your source code files, then type the following (careful - turning in to uppercase P-one, P1, at class account cs8c):
    turnin P1@cs8c sphere.py screensize.py mypi.py bmi.py
    If you are working with a partner, be sure that both partners names are in the comments at the top of all source files.

Updated 4/11/2017, by C. Michael Costanzo