Step 2: Input

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


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

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[]) {

        int    weightInLbs,     // weight of person (in pounds)
               heightInInches;  // height of person (in inches)
        String name;            // name of person

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

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

        // echo print the input values

        outputBox.printLine("Name is " + name);
        outputBox.printLine("Weight is " + weightInLbs + " pounds");
        outputBox.printLine("Height is " + heightInInches + " inches");

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

Go to Step 3: Output