I am having trouble understanding an example program we had to write in Java...
ID: 3664845 • Letter: I
Question
I am having trouble understanding an example program we had to write in Java...
Compute Body Mass Index
BMI = mass / height2
BMI Category cut-offs
Weight Category
BMI Cutoff Value ranges
UnderWeight
< 18.5
Normal weigh
>= 18.5 and < 25
Overweight
>=25 and < 30
Obese
>= 30
Requirements:
Use Input (Scanner) to collect inputs to BMI algorithm.
Use Output (System.out.print*) to display outs on Console.
Algorithm to compute BMI and Display outputs
Read weight in pounds into an int variable intPounds
Read height in feet into an int variable intHeightFeet.
Read remaining height in inches into an int variable intHeightInches.
Compute double dblHeightMeters as (intHeightFeet * 12 + intHeightInches) * .0254
Set a variable type double (dblMass) to the value (intPounds / 2.2)
Set a variable type double (dblBMI) to the value (dblMass / (dblHeightMeters * dblHeightMeters))
Display on the Console, the dblBMI value
Determine the Weight Category type String (strWeightCategory) using the cut-off table above. Perform the needed If/Else logic against the dblBMI value.
Display the Weight Category String on the Console.
If someone can walk me through this that would be great.
Weight Category
BMI Cutoff Value ranges
UnderWeight
< 18.5
Normal weigh
>= 18.5 and < 25
Overweight
>=25 and < 30
Obese
>= 30
Explanation / Answer
import java.util.Scanner;
/**
* @author Srinivas Palli
*
*/
public class BMICaliculator {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
System.out.print("Weight in pounds:");
int intPounds = scanner.nextInt();
System.out.print("height in feet:");
int intHeightFeet = scanner.nextInt();
System.out.print("remaining height in Inches:");
int intHeightInches = scanner.nextInt();
double dblHeightMeters = (intHeightFeet * 12 + intHeightInches) * 0.0254;
double dblMass = (intPounds / 2.2);
double dblBMI = (dblMass / (dblHeightMeters * dblHeightMeters));
System.out.println("dblBMI=" + dblBMI);
String strWeightCategory = "";
if (dblBMI < 18.5) {
strWeightCategory = "UnderWeight";
} else if (dblBMI >= 18.5 && dblBMI < 25) {
strWeightCategory = "Normal Weight";
} else if (dblBMI >= 25 && dblBMI < 30) {
strWeightCategory = "Over Weight";
} else if (dblBMI >= 30) {
strWeightCategory = "Obese";
}
System.out.println("Weight Category :" + strWeightCategory);
} catch (Exception e) {
// TODO: handle exception
} finally {
scanner.close();
}
}
}
OUTPUT:
Weight in pounds:9
height in feet:5
remaining height in Inches:4
dblBMI=1.5480766046759367
Weight Category :UnderWeight
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.