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
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.