Step 4: Compute BMI

Code added (or changed) from previous step is in red.


/*******************************************************************************
Program:	Body Mass Index (BMI) Calculator (Step 4)

Author: 	Rebecca Hasti, hasti@cs.wisc.edu
		copyright 2000, all rights reserved
Course:		CS 302 Summer 2000

Compiler:	CodeWarrior (JDK 1.2)
Platform:	Windows NT 4.0
*******************************************************************************/

import javabook2.*;

/**
 * This program calculates the body mass index (BMI) for a person
 * given his/her height and weight.  A BMI of 20 to 25 is considered
 * "normal".  
 *
 * Body mass index is calculated as follows:
 *   BMI = (weight in kilograms)/(height in meters)^2
 *
 * Input:       person's name (String)
 *              height in inches (int)
 *              weight in pounds (int)
 *
 * Compute:     body mass index (double)
 *
 * BUGS: none known
 **/
class BMICalculator {

    public static void main(String args[]) {

        final double METERS_PER_INCH = 0.0254;
        final double KG_PER_POUND = 0.454;

        int    weightInLbs,     // weight of person (in pounds)
               heightInInches;  // height of person (in inches)
        String name;            // name of person
        double weightInKg,      // weight of person (in kilograms)
               heightInM,       // height of person (in meters)
               bodyMassIndex;   // computed body mass index

        // declare and create the input and output objects

        MainWindow mainWindow = new MainWindow("Body Mass Index Calculator");
        InputBox inputBox = new InputBox(mainWindow);
        OutputBox outputBox = new OutputBox(mainWindow);
        mainWindow.show();
        outputBox.show(); 
       
        // get input
        // Note: we are assuming the user enters appropriate values for height
        //       and weight, i.e. no negative values or zero (0)

        name = inputBox.getString("Enter your name:");
        weightInLbs = inputBox.getInteger("Enter your weight (in pounds):");
        heightInInches = inputBox.getInteger("Enter your height (in inches):");

        // compute the BMI

        weightInKg = weightInLbs * KG_PER_POUND;
        heightInM = heightInInches * METERS_PER_INCH;
        bodyMassIndex = weightInKg / Math.pow(heightInM, 2);

        // describe the program

        outputBox.printLine("Welcome to the Body Mass Index (BMI) Calculator!");
        outputBox.skipLine(1);
        outputBox.printLine("The BMI is used to calculate the risk of");
        outputBox.printLine("weight-related health problems.");
        outputBox.printLine("A BMI of 20 to 25 is considered normal.");
        outputBox.skipLine(1);
        outputBox.printLine("This program will compute your BMI given");
        outputBox.printLine("your weight (in pounds) and height (in inches).");
        outputBox.skipLine(2);
          
        // display the results

        outputBox.printLine("For: " + name);
        outputBox.printLine("Weight: " + weightInLbs + " lbs" +
                            " (" + weightInKg + " kg)");
        outputBox.printLine("Height: " + heightInInches + " in" +
                            " (" + heightInM + " m)");
        outputBox.printLine("Body Mass Index = " + bodyMassIndex);

     } // end method main
} // end class BMICalculator

Here is the complete program. Try it for yourself in CodeWarrior!