CS21 Lab 1: First programs

Due by 11:59pm Tuesday, January 24, 2012

This lab assignment will require you to write three programs in python. First, run update21. This will create the cs21/labs/01 directory and copy over three starting point files for your programs. The program handin21 will only submit files in this directory. Next, move into your cs21/labs/01 directory and begin working on the python programs for this lab. The pwd command helps you verify that you are in the correct sub-directory.

$ cd cs21/labs/01
$ pwd
/home/your_user_name/cs21/labs/01

Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page. In particular, your program should have an introductor message that explains its purpose.

Note: in sample output for each program, blue text indicates user entry (i.e., this text was typed by you when you ran the program). Black text is printed by the program. Be sure to test your programs and compare the results with the sample output below.

1. Converting calories to joules

A joule is a unit of energy where 1 calorie is equal to 4.184 joules. Edit the file convertCalories.py in the current lab directory and create a main program that works as follows:

$ python convertCalories.py
This program converts calories to joules.

Enter calorie amount: 500
500 calories is 2092.0 joules

Here is another sample run of this program:

$ python convertCalories.py
This program converts calories to joules.

Enter calorie amount: 25
25 calories is 104.6 joules

2. Calculating Basal Metabolic Rate

Your Basal Metabolic Rate (BMR) is the amount of energy your body needs to function. We can estimate BMR based on height in inches, weight in pounds, age in years, and gender. Given your BMR we can then estimate the number of calories needed per day to maintain your weight.

For women: BMR = 655 + (4.35 * weight) + (4.7 * height) - (4.7 * age)
For men: BMR = 66 + (6.23 * weight) + (12.7 * height) - (6.8 * age)

Assuming that you are moderately active then multiply your BMR by 55% and add this to your BMR to get the appropriate number of calories to maintain your current weight.

For example, a program to calculate female BMRs should work as follows:

$ python bmr.py
This program calculates Basal Metabolic Rate (BMR) for women.

Enter height in inches: 65
Enter weight in pounds: 140
Enter age in years: 20

BMR: 1475.5
Calories needed to maintain weight:  2287.025

While a program to calculate male BMRs should work as follows:

$ python bmr.py
This program calculates Basal Metabolic Rate (BMR) for men.

Enter height in inches: 71
Enter weight in pounds: 165
Enter age in years: 18

BMR: 1873.25
Calories needed to maintain weight: 2903.5375

Edit the file bmr.py in the current lab directory and create a main program that calculates BMR and calories needed to maintain weight for either men or women, your choice. You don't need to do both, but your program introductory message should indicate which formula you use.

3. VO2 max Table

VO2 max is the maximum capacity of an individual's body to transport and use oxygen during incremental exercise, which reflects the physical fitness of the individual (higher is better). One way to calculate it is the Cooper test formula, which is VO2max = (d12 - 505) / 45. In that formula, d12 is the distance (in meters) you can run/walk in 12 minutes.

Write a program, called vo2.py, to generate a table of VO2 max values, using 100 meter increments, given minimum and maximum d12 values entered by the user.

Here are 2 sample runs:

 
$ python vo2.py 
 
This program displays a table of VO2 max values, calculated using
the Cooper test formula, given minimum and maximum d12 values (how
far you can run in 12 minutes). Please enter the min and max d12 values.

Enter minimum d12 value (in meters): 800
Enter maximum d12 value (in meters): 1600

d12		VO2
800 		6.55555555556
900 		8.77777777778
1000 		11.0
1100 		13.2222222222
1200 		15.4444444444
1300 		17.6666666667
1400 		19.8888888889
1500 		22.1111111111
1600 		24.3333333333

$ python vo2.py  
This program displays a table of VO2 max values, calculated using
the Cooper test formula, given minimum and maximum d12 values (how
far you can run in 12 minutes). Please enter the min and max d12 values.

Enter minimum d12 value (in meters): 1050 
Enter maximum d12 value (in meters): 1250 

d12		VO2
1050 		12.1111111111
1150 		14.3333333333
1250 		16.5555555556

To get the values to line up in columns you can print out one or more tab characters ("\t") between printing the values on each line. For example, try these print statements in python and see what happens:

print 3, "\t", 4
print 3, "\t\t", 4
Submit

Once you are satisfied with your programs, hand them in by typing handin21 at the unix prompt.

Note: handin21 will not be ready for lab 1 until Friday

You may run handin21 as many times as you like, and only the most recent submission will be recorded. This is useful if you realize, after handing in some programs, that you'd like to make a few more changes to them.

Remember: for this lab, handin21 will only submit files from your cs21/labs/01 directory. If you create your programs in a different directory, use the unix mv or cp commands to move or copy them into the cs21/labs/01 directory. For example:

 cp  myprog.py  ~/cs21/labs/01/myprog.py