Lab 2: Simple Methods, Scanner Class (input!), While Loops

Due: 10pm Thursday February 8

Purpose: In this assignment, you will learn how to get input from users with the scanner class, print output with slightly more sophistication, use while loops with boolean logic, and use methods to eliminate unnecessary redundancy. When you finish this lab, you will have the basic skills for input and output to your program, and thus a start on being able to more easily debug your programming errors.


You will write a program, HeartRates.java, that takes as input a series of name and age pairs, prints out heart rates for exercising, and continues until either the user types "q" or 4 iterations have been executed. Below is a sample output from an execution of the program:

Computing heart rates for exercising (press q to exit)
-----------------------------------------------
Name: John
Enter age: 20
John for your age: 20
Your aerobic zone is: 150
Your fat Burning zone is: 130

Computing heart rates for exercising (press q to exit)
-----------------------------------------------
Name: Jack
Enter age: aaa
Error! Check age again.

Computing heart rates for exercising (press q to exit)
-----------------------------------------------
Name: Joe
Enter age: 21
Joe for your age: 21
Your aerobic zone is: 149
Your fat Burning zone is: 129

Program terminating. Bye!

In oder to compute the aerobic and fat burning zone you will use the formulas:

aerobic zone = MAX_HEART_RATE - age) * .75

fat burning zone = MAX_HEART_RATE - age) * .65

where MAX_HEART_RATE is 220 and aerobic zone is and fat burning zone are integer numbers.

The calculation of each zone should be enclosed in a method, which will be called at the appropriate place in your program. The method signatures are the following:

int calcAerobicZone(int age)

int calcFatBurningZone(int age)

You should also try to minimize the number of println() method calls in your source code. In order to do that you can use another method.

Note that only "q" should terminate the execution, all other strings are considered valid names. Your program should also contain explicit checks whether the age inserted by the user is indeed a valid number. If not, the program should print an appropriate message informing the user and should not proceed to the calculation of the two zones. If the user does not terminate the iterative process by inserting "q", the program loop should terminate after 4 tries, regardless of whether some of them are successful or not.

Tip 1: You need to put the following statement at the beginning of your class in order to tell the Java compiler where to find the Scanner class.

import java.util.*;

Tip 2: To compare two strings, you should use:

String h = "hello"
String s = "hello"
String t = "later";
h.equals(s) --> resolves to true
h.equals(t) --> resolves to false


Submission and Grading:

Always include your names, slip days, and a comment at the top of your file. Here is a template for HeartRates.java that includes some skeleton code for the methods described above.

Submit your version of HeartRates.java using the turnin program . If you need help goto turnin program help.

Your program should be internally correct (sound logic) and externally correct (following java style guideline).

If you are submitting without a partner, please include your initial partner and the email correspondence with the instructor or with the TA in your header.