Laboratory 5: Optional/Practice

March 1, 2006

Bring your Java, Java, Java text to the laboratory.

You may use the textbook, your notes, your listings from previous labs, and Java documentation online.

Objectives

The objectives of this laboratory are
  1. to evaluate and strengthen your comprehension of Java fundamentals.

Problem statement: The Body-Mass-Index (BMI) Calculator

Source: Center For DiseaseControl BMI Calculator

Design and implement a class named BMICalculator. This class will have three instance variables, all of type double. One for a person's height, one for a person's weight, and one for a person's bmi. The class should have a constructor method that allows the values for the height and weight to be initialized when an instance is constructed.

The class should have two accessor methods. The calculateBMI() method will calculate a person's BMI using the following formula:

       BMI = (weight/(height * height) * 703)
where weight and height are given in pounds and inches. This method takes no parameters. After calculating and storing the BMI value, this method should return the BMI value.

The second method, rateBMI(), takes no parameters and returns a String that rates the bmi according to the following table:

BMI Rating
Less than 18.5 Underweight
Between 18.5 and less than 25 Normal
Between 25 and less than 30 Overweight
30 and higher Obese

This method uses the stored BMI value (that was calculated in the calculateBMI() method) to determine the rating. It uses and multiway if/else statement to decide which string to return. Before writing this method, draw a flowchart of the if/else statement and have it checked by the TA or instructor.

HINT: The only boolean operator you need for this problem is the less than symbol (<). The trick is to organize the if/else clauses in the same order as shown in the table: If the BMI is less than 18.5, the person is underweight, else if the BMI is less than 25, the person is normal, and so on.

Define a second class named RateMyBMI that contains a main() method that will be used to test the correctness of your implementation of the BMICalculator class. As in last week's lab, the main() method will use a KeyboardReader object to prompt the user and to read keyboard input from the user.

The main() program prompt the user to input his or her height in inches and weight in pounds. It should then create an BMICalculator object using these values to initialize it. It can then use the BMICalculator methods to calculate and report the user's actual BMI value and report the BMI rating.

Design your algorithm so that it produces exactly output shown here, including all the line breaks and punctuation. The user's input is shown in bold face:


This program will calculate your Body Mass Index.
In pounds and inches, BMI = (Wgt/(Hgt * Hgt) * 703)
Input your height in inches > 74.5
Input your weight in pounds > 165
Your BMI is: 20.899058600963922
Your BMI suggests your weight is normal

UML Diagram

Before beginning to program, draw a complete UML diagram for this problem, showing both the BMICalculator and RateMyBMI classes. Have your diagrams checked by the TA or instructor before you start programming. These will be handed in and graded as part of the lab.

Pointers/Suggestions

Hand In.

Before handing in your programs, be sure to document your source code.

Have your work checked by the laboratory instructor or TA. Print out and hand in copies of your BMICalculator.java and RateMyBMI.java files.

You're done. Great work!