// CSE 142, Spring 2010, Marty Stepp // This program computes two people's body mass index (BMI) and // compares them. The code uses Scanner for input, and parameters/returns. // It is also useful as an example of procedural design decisions. import java.util.*; // so that I can use Scanner public class BMI { public static void main(String[] args) { System.out.println("This program reads ... (etc.)"); Scanner console = new Scanner(System.in); double bmi1 = processPerson(console); double bmi2 = processPerson(console); double difference = Math.abs(bmi1 - bmi2); System.out.printf("Difference = %.2f\n", difference); } // Reads and computes the body mass data for one person. // Returns the person's body mass index (BMI). public static double processPerson(Scanner console) { double height = doHeight(console); double weight = doWeight(console); double bmi = doBMI(weight, height); doWeightClass(bmi); return bmi; } // Reads and returns a person's height. public static double doHeight(Scanner console) { System.out.println("Enter next person's information:"); System.out.print("height (in inches)? "); double height = console.nextDouble(); return height; } // Reads and returns a person's weight. public static double doWeight(Scanner console) { System.out.print("weight (in pounds)? "); double weight = console.nextDouble(); return weight; } // Computes and returns a person's body mass index (BMI) based // on the given weight and height. public static double doBMI(double weight, double height) { double bmi = weight * 703 / height / height; System.out.printf("Person #1 BMI = %.2f\n", bmi); return bmi; } // Outputs the person's general weight class as specified by the US CDC // based on the given body mass index. public static void doWeightClass(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 { System.out.println("obese"); } } }