C SC 160 Project 1: The Road to Health
15 points
Deadline: before lab Thursday October 2
This is an individual assignment
Your Assignment
Your program will request several pieces of information from the user and then
produce a color-coded heart rate bar chart for a 60-minute workout. Here is a summary.
- I will provide you folder Project1, which contains LabFrame.class and Graphic2.java. Copy it
into your workspace.
- In jGRASP, load Graphic2.java then change all Graphic2 references to
HeartRate, then save the program as HeartRate.java. Compile and run it to make
sure it works (click to move a rectangle around). You will extensively modify this class to
build your solution.
- What your program will do (short version):Request the user's name,
birth date, and resting heart rate. Calculate training zone minimum and maximum heart
rates based on the formulas given below, then produce a color 6-segment horizontal
bar chart to represent ideal heart rates to maintain over a 60 minute workout.
This can all be done by modifying and extending main() and the paint()
method provided. If you have the skills to define your own methods (which
I do not yet expect), feel free to define additional methods. Detailed descriptions
are given below.
- The basic 15 point solution is described. In addition to this there is also a simpler 12 point
solution and also a 17 point (2 bonus point) solution. Both are detailed below.
- Send me your resulting HeartRate.java program.
- This assignment requires you to use some programming constructs that you used in CSC 120 and 150
but have not yet been covered in CSC 160. IF statements in particular. I know you can handle it.
The main purpose of this assignment is to give you experience using a number of different classes from
the Java API. Most of these you have already seen in the textbook Chapter 2 examples. Others are introduced as needed. You need to use the Java API web site
to get information on the classes and methods you will be using. Its URL is:
http://math.otterbein.edu/home/java/j2sdk1.5/docs/api/
The Calculations
The effectiveness of an aerobic training program can be measured by
heart rate during training using the Karvonen formula. According to the Karvonen formula, a person's aerobic workout should
be designed to maintain a heart rate within a Training Zone.
- The person's Resting Heart Rate is input by the user
- The person's Age is calculated from the input birth date (details below)
- The Maximum Heart Rate is calculated by: 220 - Age
- The Heart Rate Reserve is calculated by: Maximum
Heart Rate - Resting Heart Rate
- The Training Zone low end is calculated by:
(Heart Rate Reserve * 0.50) + Resting Heart Rate
- The Training Zone high end is calculated by:
(Heart Rate Reserve * 0.85) + Resting Heart Rate
Example: a 48 year old person with a Resting Heart Rate of 60 BPM
(beats per minute).
Maximum Heart Rate = 220 - 48 = 172
Heart Rate Reserve = 172 - 60 = 112
Training Zone low end = (112 * 0.50) + 60 = 116
Training Zone high end = (112 * 0.85) + 60 = 155
Everything is rounded to the nearest whole number. Hint: Math.round()
What Your Program Will Do (Details)
Program execution has three major phases: get the input, perform the
calculations, and draw the results.
Phase 1: get the input
- Use JOptionPane to get the user's name. The dialog box will be
centered on the screen.
- Use JOptionPane again to get the user's birth date. It needs to be entered in the
format MM/DD/YYYY
- Use JOptionPane a third time to get the user's resting heart rate.
- All three of these are stored in String variables.
- Be sure to include your name as a comment. Place it above the class
HeartRate line.
Phase 2: perform the calculations
- The resting heart rate input simply needs to be converted from a String to an int.
Do this conversion using the Integer.parseInt() method (a class method for the Integer class).
- The birth date needs to be split into its three components. It will be stored
in a String, so these can be separated using a StringTokenizer
object. The tokenizer constructor should be given both the string and a second
string containing the delimiter ("/", in this case). Use its nextToken()
method to extract the month, day and year components one at a time.
Then convert each component to an int using Integer.parseInt() as described in the previous item.
- Next, you need to calculate the user's age. To do this, you first need today's date. Use the
GregorianCalendar class described in the textbook. Its
no-parameter constructor GregorianCalendar() will similarly create an object containing the current date and time.
Once you have this, you can extract the day, month and year using the get() method. It requires
you to specify which field you want as an argument. The fields are defined as constant identifiers (names are
in all upper-case) inherited from Calendar. Note that get() is also inherited
from Calendar. At this point, you have the current month, day and year, and the
birth month, day and year, all as int variables. Use that information to calculate the
person's age in years.
- Use the integer age and resting heart rate to calculate the various heart rates described in the Karvonen
formulas. For debugging purposes, I suggest you output the results using System.out.println(), so you
can view them in jGRASP's output window at the bottom of the screen. If you do this, be sure to remove or
comment it out before submitting the program.
- Determine the target heart rates for a 60 minute workout, in six 10-minute
segments. The
target heart rate for the first segment should be at the lower end of the training zone. The second
segment should be at the midpoint between the lower and upper ends of the
training zone. The third and fourth segments should be at the upper end of
the training zone. The fifth segment should be same as the second and the sixth the
same as the first.
Phase 3: draw the results
- Graph the results on the Graphics object g you are already
given in the paint() method. Feel free to adjust the size of the
frame (currently set to 300 by 300 in main's call to setSize()).
Graph the 60 minute workout as a horizontal bar chart (left edge is the base).
Each 10-minute segment will be a rectangle, and the target heart rate determines
the width of the rectangle. The six rectangles will be of equal height (you
choose the height; each represents 10 minutes) and will be adjacent to each
other with time going from top to bottom. The color of the rectangle will
be determined by the target heart rate level. Since there are 3 different
levels (low end, midpoint, high end), use three different colors of your choice.
It is OK that the two adjacent high end segments are the same color. Each
segment should be labeled with the target heart rate it represents - you can
put the label to the left of or inside the rectangle.
- Beneath the chart, put the label "Workout chart for name with birth
date date". Output the birth date as input by the user (the String variable).
Simpler 12 Point Solution
For maximum 12 points, have the user input age instead of birth date. This changes Phase 1 requirement 2, eliminates
Phase 2 requirement 2, vastly simplifies Phase 2 requirement 3 (just convert the String to an int), and changes Phase 3 requirement 2.
Optional 2 Point Bonus
Phase 3 requirement 2 has you output the birth date using the same String it was entered into.
For up to 2 points extra credit, format the birth date
as e.g. 14 April 1983. This can be accomplished
by creating a SimpleDateFormat object then calling its format()
method. One slight trick is needed, because SimpleDateFormat works
with Date objects but one of these can only be created using the
current date. GregorianCalender comes to the rescue again! You can
use the three int variables with the birth month, day and year, as
arguments to a GregorianCalendar constructor to create an object
for that date. Then call its getTime() method to obtain a corresponding
Date object. This can then be used with SimpleDateFormat
to get the desired format.
Some Hints
The program you are starting with has three methods: main(), paint()
and mousePressed(). If you try to do the whole thing in paint(),
you will run into problems because it is called repeatedly by other methods that
you don't see. Here's what I suggest.
- The first phase, get the input using three input dialogs, must be done in
main(), just above the f.setVisible(true); statement.
This means input will occur before the frame is displayed and the input dialogs
will be displayed at screen center. You
need three String variables to hold the input values. Define all
three as class variables (e.g. static String whatever;) at the
beginning of the class, just above paint(). This is the only way
to make them accessible inside paint() -- they cannot be passed to
it as arguments because the paint() parameter list cannot be changed.
- The second phase, do conversions and calculations, should be done in paint().
- The third phase to draw the chart must be done in paint(), after
the calculations. See the on-line documentation of the Graphics class
for more methods.
Point Allocations
Points | Item |
3 | Phase 1 to get the inputs and store them in class variables. This also includes
putting your name into the program as a comment! |
6 |
Phase 2 to perform the calculations. This includes correct formulas and
use of descriptive variable names. This drops to 3 points if the 12 point
solution is submitted. |
6 | Phase 3 to draw the results. Partial credit depends on how many elements are drawn. |
2 |
Extra Credit. Formatting the birth date as described
in phase 3. Optional. Not available with the 12 point solution. |
To Turn In
When you are finished, email me your HeartRate.java file.
[ CSC 160 schedule
| C
SC 160 | Peter
Sanderson | Math Sciences server
| Math Sciences home page
| Otterbein ]
Last updated:
Peter Sanderson (PSanderson@otterbein.edu)