a. Java Program for the body mass index (BMI) is commonly used by health and nutrition professionals to estimate human body fat in populations.
It is computed by taking the individual's weight (mass) in kilograms and dividing it by the square of their height in meters.
Write a Java program that reads from the keyboard the wight and the height in kilograms and meters and prints on the screen the BMI of the individual plus using this table gives a verdict:
BMI category
less than 18.5 underweight
18.5 to 24.9 normal weight
25.0 to 29.9 overweight
30.0 or more obese
b. Extend the program at a. to
1. read the name of the person from the keyboard
2. give the choice of the wight to be input either in kilograms or in pounds
3. give the choice of the height to be given either in meters or in feet
Perform the conversions from lb to Kg or from feet to meters and using the table above give the verdict.
Code Solution:
import java.util.Scanner;
public class BMI {
public static void main(String[] args) {
// TODO Auto-generated method stub
final double KILOGRAMS_PER_POUND = 0.453;
//final double METERS_PER_INCH = 0.026;
double weight = 0.0;
double height = 0.0;
double bmi = 0.0;
String name;
String weightType = null;
String heightType =null;
System.out.println("Enter your Name: ");
Scanner readname = new Scanner(System.in);
name = readname.nextLine();
System.out.println("Press 1 or 2 to choose Weight Type: ");
System.out.println("1.KILOGRAMS");
System.out.println("2.POUNDS");
Scanner readweightType = new Scanner(System.in);
weightType = readweightType.next().trim();
if(weightType.equalsIgnoreCase("1"))
System.out.println("Enter Weight in Kilograms:");
else if(weightType.equalsIgnoreCase("2"))
System.out.println("Enter Weight in Pounds:");
else
{
System.out.println("Invalid WeightType Entry");
System.exit(0);
}
Scanner getWeight = new Scanner(System.in);
weight = getWeight.nextDouble();
if(weightType.equalsIgnoreCase("2"))
{
weight = weight* KILOGRAMS_PER_POUND ;
//convert pounds to KG
}
System.out.println("Press 1 or 2 to choose Height Type: ");
System.out.println("1.METERS");
System.out.println("2.FEET");
Scanner readHeightType = new Scanner(System.in);
heightType = readHeightType.next().trim();
if(heightType.equalsIgnoreCase("1"))
System.out.println("Enter Height in Meters:");
else if(heightType.equalsIgnoreCase("2"))
System.out.println("Enter Weight in Feet:");
else
{
System.out.println("Invalid HeightType Entry");
System.exit(0);
}
Scanner readHeight = new Scanner(System.in);
height = readHeight.nextDouble();
if(heightType.equalsIgnoreCase("2"))
{
height = height/3.2808;
}
bmi = Math.abs((weight)/(height * height));
System.out.print("\n \t Your Name is " +name);
System.out.printf("\n \t The BMI is %f", bmi);
if(bmi < 18.5)
System.out.print(" belongs to UnderWeight");
else if(bmi >= 18.5 && bmi < 24.9)
System.out.print(" belongs to NormalWeight");
else if(bmi >= 25.0 && bmi <= 29.9)
System.out.print(" belongs to OverWeight");
else if(bmi >= 30.0 )
System.out.print(" belongs to Obese");
}
}