Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Body mass index is a measure of whether someone\'s weight is appropriate for the

ID: 3624629 • Letter: B

Question

Body mass index is a measure of whether someone's weight is appropriate for their height. A body - mass - index value between 19 and 25 is considered to be in the normal range. Here's the formula for bmi = 704 x weightInPounds/heightInInches^2 calculating body mass index: a) Implement a program that prompts the user for height and weight values and displays the associated body mass index. Perform input validation by making sure that the user enters positive decimal numbers for height and weight. See the sample session for details. In particular, note the format for the echo - printed height and weight values and for the generated body - mass - index value. For this program, there's no need for multiple classes or even multiple methods. Just put all your code in a main method and put the main method in a BodyMassIndex class. Sample session: Enter height in inches: hi Invalid inches value. Must be a decimal number. Re - enter height in inches: 0 Invalid inches value. Must be positive. Re - enter height in inches: 69.25 Enter weight in pounds: dog Invalid pounds value. Must be a decimal number. Re - enter weight in pounds: - 3 Invalid pounds value. Must be positive. Re - enter weight in pounds: 150.5 height = 69.25 weight = 150.5 pounds body mass index = 22.1

Explanation / Answer

public static void main(String[] args)
{
// keyboard input
Scanner kb = new Scanner(System.in);

int height=0, weight=0;

// input validation
System.out.print("Enter height in inches: ");
while(true)
{
try
{
height = kb.nextDouble();

if(height < 0)
{
System.out.println("Invalid inches value. Must be positive.");
Syste.out.print("Re-enter height in inches: ");
}
else
break;
}
catch(NoSuchElementException ex)
{
System.out.println("Invalid inches value. Must be a decimal number.");
System.out.print("Re-enter height in inches: ");
}
}

System.out.print("Enter weight in pounds: ");
while(true)
{
try
{
height = kb.nextDouble();

if(weight < 0)
{
System.out.println("Invalid pounds value. Must be positive.");
Syste.out.print("Re-enter weight in pounds: ");
}
else
break;
}
catch(NoSuchElementException ex)
{
System.out.println("Invalid pounds value. Must be a decimal number.");
System.out.print("Re-enter weight in pounds: ");
}
}

System.out.println(" height = "+height+""");
System.out.println("weight = "+weight+" pounds");

// calculations
double bmi = 704*weight/height/height;

// output
System.out.printf("body mass index = %.1f ", bmi);
}