// Zorah Fung, CSE 142 // This program computes two people's body mass index (BMI) and // compares them. import java.util.*; // to use the Scanner public class BMICalculator { public static void main(String[] args) { // Note: Our goal is to make main a summary of our program intro(); Scanner console = new Scanner(System.in); double bmi1 = calculateBMI(console); double bmi2 = calculateBMI(console); printResults(bmi1, bmi2); } // Prints out the introduction public static void intro() { // Note: We make this a method so that main is a concise summary. // The details (text) of the intro are not important enough to keep // in main 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 the user for their weight and height and returns the BMI // based on these weight and height public static double calculateBMI(Scanner console) { // Note: This method both reduces redundancy -- we do this for two people -- // and adds structure to our program -- calculating the BMI is a single, important // task in our program 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(height, weight); return bmi; } // Returns the BMI for the given height and weight public static double bmiFor(double height, double weight) { return weight / (height * height) * 703; } // Prints the weight class for the given BMI public static void printStatus(double bmi) { // Note: This method both reduces redundancy -- we do this for two people -- // and adds structure to our program -- determining the weight class is a single, // important task in our program 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"); } } // Prints the weight class pf and difference between the two given BMIs. public static void printResults(double bmi1, double bmi2) { System.out.println("Person 1 BMI " + bmi1); printStatus(bmi1); System.out.println("Person 2 BMI " + bmi2); printStatus(bmi2); System.out.println("Difference = " + Math.abs(bmi1 - bmi2)); } }