// CSE 142, Autumn 2009, Marty Stepp // This program computes two people's body mass index (BMI) and // compares them. The code uses parameters, returns, and Scanner. // // This is the complete version of the program. import java.util.*; // so that I can use Scanner public class BMI { public static void main(String[] args) { System.out.println("This program reads in data for two people and"); System.out.println("computes their body mass index (BMI)"); System.out.println(); Scanner console = new Scanner(System.in); double bmi1 = person(console); double bmi2 = person(console); // report overall results report(1, bmi1); report(2, bmi2); System.out.println("Difference = " + Math.abs(bmi1 - bmi2)); } // prints a welcome message explaining the program public static void introduction() { System.out.println("This program reads data for two people and"); System.out.println("computes their body mass index (BMI)."); System.out.println(); } // Reads information for one person, then computes and returns their BMI. public static double person(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(); System.out.println(); double bodyMass = bmi(height, weight); return bodyMass; } // Computes and returns a person's BMI for the given height and weight. public static double bmi(double height, double weight) { double result = weight / (height * height) * 703; return result; } // Outputs information about a person's BMI and weight status. public static void report(int number, double bmi) { System.out.println("Person " + number + " BMI = " + 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"); } } }