Valeri173

Danov Homework Ex

Oct 20th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1. import java.util.*;  // for Scanner
  2.  
  3. public class BMICalculator {
  4.     public static void main(String[] args) {
  5.         Scanner console = new Scanner(System.in);
  6.         printIntroduction();
  7.  
  8.         double bmi1 = getBMI(console);
  9.         double bmi2 = getBMI(console);
  10.        
  11.         String status1 = getStatus(bmi1);
  12.         String status2 = getStatus(bmi2);
  13.  
  14.         reportResults(1, bmi1, status1);
  15.         reportResults(2, bmi2, status2);
  16.        
  17.         System.out.println("Difference = " + round(Math.abs(bmi1 - bmi2), 2));
  18.     }
  19.    
  20.     // Prints an introduction for the program.
  21.     public static void printIntroduction() {
  22.         System.out.println("This program reads in data for two people and");
  23.         System.out.println("computes their body mass index (BMI)");
  24.         System.out.println();  
  25.     }
  26.    
  27.     // Prompts for height and weight; calculates and returns
  28.     // body mass index.
  29.     public static double getBMI(Scanner console) {
  30.         System.out.println("Enter next person's information:");
  31.         System.out.print("height (in inches)? ");
  32.         double height = console.nextDouble();
  33.        
  34.         System.out.print("weight (in pounds)? ");      
  35.         double weight = console.nextDouble();
  36.         System.out.println();      
  37.            
  38.         return bmiFor(weight, height);
  39.     }
  40.    
  41.     // Calculates and returns body mass index based on weight and height
  42.     public static double bmiFor(double weight, double height) {
  43.         return weight / Math.pow(height, 2) * 703;
  44.     }
  45.        
  46.     // Calculates and returns the bmi class based on a
  47.     // chart published by the Centers for Disease Control and Prevention
  48.     public static String getStatus(double bmi) {
  49.         if (bmi < 18.5) {
  50.             return "underweight";
  51.         } else if (bmi < 25) {
  52.             return "normal";
  53.         } else if (bmi < 30) {
  54.             return "overweight";
  55.         } else {
  56.             return "obese";
  57.         }  
  58.     }
  59.    
  60.     // Prints bmi and bmi status for a person.
  61.     public static void reportResults(int person, double bmi, String status) {
  62.         System.out.println("Person " + person + " BMI = " + round(bmi, 2));
  63.         System.out.println(status);
  64.     }
Advertisement
Add Comment
Please, Sign In to add comment