// Helene Martin, CSE 142 // This program computes two people's body mass index. // to see bad versions of this program and why they are bad, // read "procedural design heuristics" on page 291 import java.util.*; // for Scanner public class BMICalculator { public static void main(String[] args) { Scanner console = new Scanner(System.in); introduction(); double bmi1 = calculateBMI(console); double bmi2 = calculateBMI(console); reportResults(bmi1, bmi2); System.out.println("Difference = " + round(Math.abs(bmi1 - bmi2), 2)); } // Prints an introduction for the program. public static void introduction() { System.out.println("This program reads in data for two people and"); System.out.println("computes their body mass index (BMI)"); System.out.println(); } // Prompts for height and weight then calculates and returns // body mass index. public static double calculateBMI(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(weight, height); return bmi; } // Calculates and returns body mass index for specified weight and height public static double bmiFor(double weight, double height) { return weight / Math.pow(height, 2) * 703; } // Calculates and returns the bmi class based on a // chart published by the Centers for Disease Control and Prevention public static String calculateWeightClass(double bmi) { if (bmi < 18.5) { return "underweight"; } else if (bmi < 25) { return "normal"; } else if (bmi < 30) { return "overweight"; } else { return "obese"; } } // Reports the bmi and weight class for two people on the console public static void reportResults(double bmi1, double bmi2) { System.out.println("Person 1 BMI = " + bmi1); System.out.println(calculateWeightClass(bmi1)); System.out.println("Person 2 BMI = " + bmi2); System.out.println(calculateWeightClass(bmi2)); } // Rounds a value to a given number of digits. public static double round(double value, int digits) { return Math.round(value * Math.pow(10, digits)) / Math.pow(10, digits); } }