bmi calculator example author: rebecca hasti, [email protected] copyright 2000, all rights reserved
TRANSCRIPT
BMI Calculator Example
Author: Rebecca Hasti, [email protected] copyright 2000, all rights reserved
BMI Calculator
• Problem Statement:– Write a program that computes the body mass
index (BMI) for a person given the person's height and weight. Body mass index is calculated as follows:
BMI = (weight in kilograms) / (height in meters)^2
BMI Calculator
• How do we solve this problem?
• Start with the software life cycle:
• Design
• Analysis
• Coding
• Testing
• Maintenance
BMI Calculator: Analysis
• Program Tasks – Prompt the user for height – Prompt the user for weight – Calculate the BMI – Output the results in a user-friendly fashion
• In the United States, people give height in feet and inches and weight in pounds. Our program will let the user enter information in this way.
• Since body mass index is: (weight in kilograms) / (height in meters)^2
• We will need to convert to metric units
Design Document: BMICalculator
Class Purpose
BMICalculator The main class of the program.
MainWindow The main frame window of the program. The title is set to Body Mass Index Calculator. This class is from javabook2.
InputBox An InputBox object is used to get three values: the user's name, height (in inches), and weight (in pounds). This class is from javabook2.
OutputBox An OutputBox object is used to display a description of the program, the input values, and the computed BMI. This class is from javabook2.
Math The pow method is used in the calculation of the BMI. This class is from java.lang.
BMI Calculator: Design
BMI Calculator: Coding
• Use an incremental approach (Top Down):
• Program skeleton
• Input
• Output
• Compute
BMI Calculator: Coding
• Use an incremental approach (Top Down in this case):
• Program skeleton
• Input
• Output
• Compute
import javabook2.*; class BMICalculator {
public static void main(String args[]) {
} // end method main } // end class BMICalculator
import javabook2.*; class BMICalculator {
public static void main(String args[]) {
// declare the input and output objects
// get the input we are assuming no negative values or zero (0)
// compute the BMI
// describe the program with some prompts
// display the results
} // end method main } // end class BMICalculator
import javabook2.*; class BMICalculator {
public static void main(String args[]) {
// declare the input and output objectsMainWindow mainWindow = new MainWindow("Body Mass Index Calculator"); InputBox inputBox = new InputBox(mainWindow); OutputBox outputBox = new OutputBox(mainWindow); mainWindow.show(); outputBox.show(); // we are assuming no negative values or zero (0) // get the input
// compute the BMI
// describe the program
// display the results
} // end method main } // end class BMICalculator
import javabook2.*; class BMICalculator {
public static void main(String args[]) {
// declare the input and output objectsMainWindow mainWindow = new MainWindow("Body Mass Index Calculator"); InputBox inputBox = new InputBox(mainWindow); OutputBox outputBox = new OutputBox(mainWindow); mainWindow.show(); outputBox.show(); // we are assuming no negative values or zero (0) // get the inputname = InputBox.getString("Enter your name:"); weightInLbs = inputBox.getInteger("Enter your weight (in pounds):"); heightInInches = inputBox.getInteger("Enter your height (in inches):"); // compute the BMI
// describe the program
// display the results
} // end method main } // end class BMICalculator
import javabook2.*; class BMICalculator {
public static void main(String args[]) {
int weightInLbs, heightInInches;String name; // name of person double weightInKg, heightInM, bodyMassIndex; // declare the input and output objectsMainWindow mainWindow = new MainWindow("Body Mass Index Calculator"); InputBox inputBox = new InputBox(mainWindow); OutputBox outputBox = new OutputBox(mainWindow); mainWindow.show(); outputBox.show(); // we are assuming no negative values or zero (0) // get the inputname = InputBox.getString("Enter your name:"); weightInLbs = inputBox.getInteger("Enter your weight (in pounds):"); heightInInches = inputBox.getInteger("Enter your height (in inches):"); // compute the BMI
// describe the program
// display the results
} // end method main } // end class BMICalculator
import javabook2.*; class BMICalculator {
public static void main(String args[]) {
int weightInLbs, heightInInches;String name; // name of person double weightInKg, heightInM, bodyMassIndex; // declare the input and output objectsMainWindow mainWindow = new MainWindow("Body Mass Index Calculator"); InputBox inputBox = new InputBox(mainWindow); OutputBox outputBox = new OutputBox(mainWindow); mainWindow.show(); outputBox.show(); // we are assuming no negative values or zero (0) // get the inputname = 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
// display the results
} // end method main } // end class BMICalculator
import javabook2.*; class BMICalculator {
public static void main(String args[]) { final double METERS_PER_INCH = 0.0254; final double KG_PER_POUND = 0.454; int weightInLbs, heightInInches;String name; // name of person double weightInKg, heightInM, bodyMassIndex; // declare the input and output objectsMainWindow mainWindow = new MainWindow("Body Mass Index Calculator"); InputBox inputBox = new InputBox(mainWindow); OutputBox outputBox = new OutputBox(mainWindow); mainWindow.show(); outputBox.show(); // we are assuming no negative values or zero (0) // get the inputname = 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
// display the results
} // end method main } // end class BMICalculator
import javabook2.*; class BMICalculator {
public static void main(String args[]) { final double METERS_PER_INCH = 0.0254; final double KG_PER_POUND = 0.454; int weightInLbs, heightInInches;String name; // name of person double weightInKg, heightInM, bodyMassIndex; // declare the input and output objectsMainWindow mainWindow = new MainWindow("Body Mass Index Calculator"); InputBox inputBox = new InputBox(mainWindow); OutputBox outputBox = new OutputBox(mainWindow); mainWindow.show(); outputBox.show(); // we are assuming no negative values or zero (0) // get the inputname = 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
} // end method main } // end class BMICalculator
import javabook2.*; class BMICalculator {
public static void main(String args[]) { final double METERS_PER_INCH = 0.0254; final double KG_PER_POUND = 0.454; int weightInLbs, heightInInches;String name; // name of person double weightInKg, heightInM, bodyMassIndex; // declare the input and output objectsMainWindow mainWindow = new MainWindow("Body Mass Index Calculator"); InputBox inputBox = new InputBox(mainWindow); OutputBox outputBox = new OutputBox(mainWindow); mainWindow.show(); outputBox.show(); // we are assuming no negative values or zero (0) // get the inputname = 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