CSE142 Sample Program handout #9
First Sample log of execution
-----------------------------
This program reads in data for two people
and computes their body mass index (BMI)
and weight status.
Enter next person's information:
height (in inches)? 73
weight (in pounds)? 225
Enter next person's information:
height (in inches)? 71
weight (in pounds)? 220
Person #1 body mass index = 29.681929067367236
overweight
Person #2 body mass index = 30.68042055147788
obese
Second Sample Log of Execution
------------------------------
This program reads in data for two people
and computes their body mass index (BMI)
and weight status.
Enter next person's information:
height (in inches)? 62.5
weight (in pounds)? 130.5
Enter next person's information:
height (in inches)? 58.5
weight (in pounds)? 90
Person #1 body mass index = 23.485824
normal
Person #2 body mass index = 18.487836949375414
underweight
Unstructured version BMI1.java (not a good model to follow)
-----------------------------------------------------------
// Stuart Reges
// 4/18/06
//
// This program finds the body mass index (BMI) for two individuals.
import java.util.*;
public class BMI1 {
public static void main(String[] args) {
System.out.println("This program reads in data for two people");
System.out.println("and computes their body mass index (BMI)");
System.out.println("and weight status.");
System.out.println();
Scanner console = new Scanner(System.in);
System.out.println("Enter next person's information:");
System.out.print("height (in inches)? ");
double height1 = console.nextDouble();
System.out.print("weight (in pounds)? ");
double weight1 = console.nextDouble();
double bmi1 = weight1 / (height1 * height1) * 703;
System.out.println();
System.out.println("Enter next person's information:");
System.out.print("height (in inches)? ");
double height2 = console.nextDouble();
System.out.print("weight (in pounds)? ");
double weight2 = console.nextDouble();
double bmi2 = weight2 / (height2 * height2) * 703;
System.out.println();
System.out.println("Person #1 body mass index = " + bmi1);
if (bmi1 < 18.5) {
System.out.println("underweight");
} else if (bmi1 < 25) {
System.out.println("normal");
} else if (bmi1 < 30) {
System.out.println("overweight");
} else { // bmi1 >= 30
System.out.println("obese");
}
System.out.println("Person #2 body mass index = " + bmi2);
if (bmi2 < 18.5) {
System.out.println("underweight");
} else if (bmi2 < 25) {
System.out.println("normal");
} else if (bmi2 < 30) {
System.out.println("overweight");
} else { // bmi2 >= 30
System.out.println("obese");
}
}
}
Structured version BMI2.java (follow this model)
------------------------------------------------
// Stuart Reges
// 4/18/06
//
// This program finds the body mass index (BMI) for two individuals. This
// variation includes several methods other than method main.
import java.util.*;
public class BMI2 {
public static void main(String[] args) {
giveIntro();
Scanner console = new Scanner(System.in);
double bmi1 = getBMI(console);
double bmi2 = getBMI(console);
reportResults(bmi1, bmi2);
}
// introduces the program to the user
public static void giveIntro() {
System.out.println("This program reads in data for two people");
System.out.println("and computes their body mass index (BMI)");
System.out.println("and weight status.");
System.out.println();
}
// prompts for one person's statistics, returning the BMI
public static double getBMI(Scanner console) {
System.out.println("Enter next person's information:");
System.out.print("height (in inches)? ");
double height = console.nextDouble();
System.out.print("weight (in pounds)? ");
double weight = console.nextDouble();
double bmi = BMIFor(height, weight);
System.out.println();
return bmi;
}
// this method contains the body mass index formula for converting the
// given height (in inches) and weight (in pounds) into a BMI
public static double BMIFor(double height, double weight) {
return weight / (height * height) * 703;
}
// reports the overall bmi values and weight status to the user
public static void reportResults(double bmi1, double bmi2) {
System.out.println("Person #1 body mass index = " + bmi1);
reportStatus(bmi1);
System.out.println("Person #2 body mass index = " + bmi2);
reportStatus(bmi2);
}
// reports the weight status for the given bmi value
public static void reportStatus(double bmi) {
if (bmi < 18.5) {
System.out.println("underweight");
} else if (bmi < 25) {
System.out.println("normal");
} else if (bmi < 30) {
System.out.println("overweight");
} else { // bmi >= 30
System.out.println("obese");
}
}
}