BMI Calculator Example Author Rebecca Hasti hastics wisc
BMI Calculator Example Author: Rebecca Hasti, hasti@cs. wisc. edu 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. Main. Window The main frame window of the program. The title is set to Body Mass Index Calculator. This class is from javabook 2. Input. Box An Input. Box object is used to get three values: the user's name, height (in inches), and weight (in pounds). This class is from javabook 2. Output. Box An Output. Box object is used to display a description of the program, the input values, and the computed BMI. This class is from javabook 2. 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 javabook 2. *; class BMICalculator { public static void main(String args[]) { } // end method main } // end class BMICalculator
import javabook 2. *; 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 javabook 2. *; class BMICalculator { public static void main(String args[]) { // declare the input and output objects Main. Window main. Window = new Main. Window("Body Mass Index Calculator"); Input. Box input. Box = new Input. Box(main. Window); Output. Box output. Box = new Output. Box(main. Window); main. Window. show(); output. Box. 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 javabook 2. *; class BMICalculator { public static void main(String args[]) { // declare the input and output objects Main. Window main. Window = new Main. Window("Body Mass Index Calculator"); Input. Box input. Box = new Input. Box(main. Window); Output. Box output. Box = new Output. Box(main. Window); main. Window. show(); output. Box. show(); // we are assuming no negative values or zero (0) // get the input name = Input. Box. get. String("Enter your name: "); weight. In. Lbs = input. Box. get. Integer("Enter your weight (in pounds): "); height. Inches = input. Box. get. Integer("Enter your height (in inches): "); // compute the BMI // describe the program // display the results } // end method main } // end class BMICalculator
import javabook 2. *; class BMICalculator { public static void main(String args[]) { int weight. In. Lbs, height. Inches; String name; // name of person double weight. In. Kg, height. In. M, body. Mass. Index; // declare the input and output objects Main. Window main. Window = new Main. Window("Body Mass Index Calculator"); Input. Box input. Box = new Input. Box(main. Window); Output. Box output. Box = new Output. Box(main. Window); main. Window. show(); output. Box. show(); // we are assuming no negative values or zero (0) // get the input name = Input. Box. get. String("Enter your name: "); weight. In. Lbs = input. Box. get. Integer("Enter your weight (in pounds): "); height. Inches = input. Box. get. Integer("Enter your height (in inches): "); // compute the BMI // describe the program // display the results } // end method main } // end class BMICalculator
import javabook 2. *; class BMICalculator { public static void main(String args[]) { int weight. In. Lbs, height. Inches; String name; // name of person double weight. In. Kg, height. In. M, body. Mass. Index; // declare the input and output objects Main. Window main. Window = new Main. Window("Body Mass Index Calculator"); Input. Box input. Box = new Input. Box(main. Window); Output. Box output. Box = new Output. Box(main. Window); main. Window. show(); output. Box. show(); // we are assuming no negative values or zero (0) // get the input name = Input. Box. get. String("Enter your name: "); weight. In. Lbs = input. Box. get. Integer("Enter your weight (in pounds): "); height. Inches = input. Box. get. Integer("Enter your height (in inches): "); // compute the BMI weight. In. Kg = weight. In. Lbs * KG_PER_POUND; height. In. M = height. Inches * METERS_PER_INCH; body. Mass. Index = weight. In. Kg / Math. pow(height. In. M, 2); // describe the program // display the results } // end method main } // end class BMICalculator
import javabook 2. *; class BMICalculator { public static void main(String args[]) { final double METERS_PER_INCH = 0. 0254; final double KG_PER_POUND = 0. 454; int weight. In. Lbs, height. Inches; String name; // name of person double weight. In. Kg, height. In. M, body. Mass. Index; // declare the input and output objects Main. Window main. Window = new Main. Window("Body Mass Index Calculator"); Input. Box input. Box = new Input. Box(main. Window); Output. Box output. Box = new Output. Box(main. Window); main. Window. show(); output. Box. show(); // we are assuming no negative values or zero (0) // get the input name = Input. Box. get. String("Enter your name: "); weight. In. Lbs = input. Box. get. Integer("Enter your weight (in pounds): "); height. Inches = input. Box. get. Integer("Enter your height (in inches): "); // compute the BMI weight. In. Kg = weight. In. Lbs * KG_PER_POUND; height. In. M = height. Inches * METERS_PER_INCH; body. Mass. Index = weight. In. Kg / Math. pow(height. In. M, 2); // describe the program // display the results } // end method main } // end class BMICalculator
import javabook 2. *; class BMICalculator { public static void main(String args[]) { final double METERS_PER_INCH = 0. 0254; final double KG_PER_POUND = 0. 454; int weight. In. Lbs, height. Inches; String name; // name of person double weight. In. Kg, height. In. M, body. Mass. Index; // declare the input and output objects Main. Window main. Window = new Main. Window("Body Mass Index Calculator"); Input. Box input. Box = new Input. Box(main. Window); Output. Box output. Box = new Output. Box(main. Window); main. Window. show(); output. Box. show(); // we are assuming no negative values or zero (0) // get the input name = Input. Box. get. String("Enter your name: "); weight. In. Lbs = input. Box. get. Integer("Enter your weight (in pounds): "); height. Inches = input. Box. get. Integer("Enter your height (in inches): "); // compute the BMI weight. In. Kg = weight. In. Lbs * KG_PER_POUND; height. In. M = height. Inches * METERS_PER_INCH; body. Mass. Index = weight. In. Kg / Math. pow(height. In. M, 2); // describe the program output. Box. print. Line("Welcome to the Body Mass Index (BMI) Calculator!"); output. Box. skip. Line(1); output. Box. print. Line("The BMI is used to calculate the risk of"); output. Box. print. Line("weight-related health problems. "); output. Box. print. Line("A BMI of 20 to 25 is considered normal. "); output. Box. skip. Line(1); output. Box. print. Line("This program will compute your BMI given"); output. Box. print. Line("your weight (in pounds) and height (in inches). "); output. Box. skip. Line(2); // display the results } // end method main } // end class BMICalculator
import javabook 2. *; class BMICalculator { public static void main(String args[]) { final double METERS_PER_INCH = 0. 0254; final double KG_PER_POUND = 0. 454; int weight. In. Lbs, height. Inches; String name; // name of person double weight. In. Kg, height. In. M, body. Mass. Index; // declare the input and output objects Main. Window main. Window = new Main. Window("Body Mass Index Calculator"); Input. Box input. Box = new Input. Box(main. Window); Output. Box output. Box = new Output. Box(main. Window); main. Window. show(); output. Box. show(); // we are assuming no negative values or zero (0) // get the input name = Input. Box. get. String("Enter your name: "); weight. In. Lbs = input. Box. get. Integer("Enter your weight (in pounds): "); height. Inches = input. Box. get. Integer("Enter your height (in inches): "); // compute the BMI weight. In. Kg = weight. In. Lbs * KG_PER_POUND; height. In. M = height. Inches * METERS_PER_INCH; body. Mass. Index = weight. In. Kg / Math. pow(height. In. M, 2); // describe the program output. Box. print. Line("Welcome to the Body Mass Index (BMI) Calculator!"); output. Box. skip. Line(1); output. Box. print. Line("The BMI is used to calculate the risk of"); output. Box. print. Line("weight-related health problems. "); output. Box. print. Line("A BMI of 20 to 25 is considered normal. "); output. Box. skip. Line(1); output. Box. print. Line("This program will compute your BMI given"); output. Box. print. Line("your weight (in pounds) and height (in inches). "); output. Box. skip. Line(2); // display the results output. Box. print. Line("For: " + name); output. Box. print. Line("Weight: " + weight. In. Lbs + " lbs" + " (" + weight. In. Kg + " kg)"); output. Box. print. Line("Height: " + height. Inches + " in" + " (" + height. In. M + " m)"); output. Box. print. Line("Body Mass Index = " + body. Mass. Index); } // end method main } // end class BMICalculator
- Slides: 17