// CSE 142, Summer 2008 (Marty Stepp) // This program computes the body mass index (BMI) of two people // and compares the two to find the difference in BMI. // // This version also prints information about the person's weight range using if/else, // and it rounds the numbers to 3 digits after the decimal point. import java.util.*; public class BMI2 { public static void main(String[] args) { Scanner console = new Scanner(System.in); double bmi1 = person(console); double bmi2 = person(console); results(bmi1, bmi2); } // Prompts for the height/weight of a single person, // and returns that person's body mass index. 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(); // compute the bmi double bmi = weight / (height * height) * 703; 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"); } System.out.println(); return bmi; } // Outputs the overall results of each person's BMI and difference. // Uses System.out.printf to round to the nearest thousandth. public static void results(double bmi1, double bmi2) { System.out.printf("Person #1 body mass index = %.3f\n", bmi1); System.out.printf("Person #2 body mass index = %.3f\n", bmi2); System.out.printf("Difference = %.3f\n", Math.abs(bmi1 - bmi2)); } }