CPSC 120 Postlab 2: Finding Your Pace
Due before class on Monday, September 14, 2009
For runners, race distances are often given in kilometers -- 5K, 10K, etc.
A fast runner might run a 5K race in 20 minutes or so; the winning time is
usually closer to 15 minutes, and most of the population comes in between
20 and 35 minutes. It's interesting to think of what these times translate
to in miles per hour (MPH). In this assignment you will write a program that
relates the user's race time to their
speed in MPH.
Your program should ask the user for the following input:
- The distance of the race, in kilometers
- The minutes part of their finish time
- The seconds part of their finish time
Your program should print the user's pace in MPH, then ask the user what
pace he or she is trying to achieve. After reading in this information,
the program should print what time the user would need to run a race of
the given distance in to achieve this pace.
A sample run of your program for a user who ran a 5K in 27 minutes and 15
seconds might look like this (items in bold are
entered by the user):
Enter the number of kilometers run: 5
Enter the minutes part of your time: 27
Enter the seconds part of your time: 15
Your pace is 6.8807339449541285 MPH.
How fast do you want to go, in MPH? 10
To go that fast, you would have to run your distance in 18.75 minutes.
Planning Your Program
Be sure to put your name, the file name, and a description of the program
in the header.
- First declare your variables and constants. You will probably
to hold the distance in kilometers, the distance in miles, the minutes part
of the time, the seconds part of the time, the total seconds, the time
in hours, the pace in MPH, the desired pace, and the time required for the
desired pace. Phew! Think about what type each of these should be.
Note that there are 1.6 kilometers in a mile and 3600 seconds in an hour
(these should be constants!!).
- Next, do the calculations to get the distance in miles and the time in
hours. It's easy to convert the distance to miles -- just use the conversion
factor above. But be sure to use it the right way! Note that 5 kilometers
is about 3.1 miles; this should help you see if your answers make sense.
- Converting the time to hours will take a little more thought.
First convert the minutes part to seconds, then add the result to the
seconds part entered by the user. This gives you the total time in seconds,
which you can convert to hours using the conversion factor above.
- Once you have the distance in miles and the time in hours, it's simple
to get the pace in MPH -- just divide.
- When the user enters their desired pace, it's also easy
to calculate the necessary time in hours; just solve the
formula (pace = miles/hours) for hours, since you know both the pace and
the miles. Of course, you'll need to convert the result to minutes to
print it out.
To submit your code cp a tgz file containing your java file
to the directory: