// Helene Martin, CSE 142 // This structured program computes one person's body mass index and // displays their weight status. // goal: demonstrate good decomposition and use of returns. // See chapter 4 case study for more details. // In particular, the "procedural design heuristics" section is excellent (p283) import java.util.*; // for Scanner public class BMICalculator { public static void main(String[] args) { Scanner console = new Scanner(System.in); printIntroduction(); double bmi1 = getBMI(console); double bmi2 = getBMI(console); String status1 = getStatus(bmi1); String status2 = getStatus(bmi2); reportResults(1, bmi1, status1); reportResults(2, bmi2, status2); System.out.println("Difference = " + round(Math.abs(bmi1 - bmi2), 2)); } // Prints an introduction for the program. public static void printIntroduction() { 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; calculates and returns // body mass index. 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(); System.out.println(); return bmiFor(weight, height); } // Calculates and returns body mass index based on 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 getStatus(double bmi) { if (bmi < 18.5) { return "underweight"; } else if (bmi < 25) { return "normal"; } else if (bmi < 30) { return "overweight"; } else { return "obese"; } } // Prints bmi and bmi status for a person. public static void reportResults(int person, double bmi, String status) { System.out.println("Person " + person + " BMI = " + round(bmi, 2)); System.out.println(status); } // 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); } }