Implement a program that prompts the user for height and weight values and displ
ID: 3627799 • Letter: I
Question
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 Output:
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
please rate - thanks
import java.util.*;
public class BMI
{
public static void main(String argv[])
{
Scanner input = new Scanner(System.in);
String inputString;
double inches,pounds;
double bmi;
System.out.print("enter height (in inches) ");
inputString = input.nextLine();
try{Double.parseDouble(inputString);
} catch(NumberFormatException nfe)
{System.out.println("Invalid inches value. Must be a decimal number.");
System.out.print("Re-enter height in inches: ");
inputString = input.nextLine();
}
inches=Double.parseDouble(inputString);
if(inches<=0)
{System.out.println("Invalid inches value. Must be positive.");
System.out.print("Re-enter height in inches:");
inputString = input.nextLine();
inches=Double.parseDouble(inputString);
}
System.out.print("enter weight(in pounds) ");
inputString = input.nextLine();
try{Double.parseDouble(inputString);
} catch(NumberFormatException nfe)
{System.out.println("Invalid pounds value. Must be a decimal number.");
System.out.print("Re-enter weight in pounds: ");
inputString = input.nextLine();
}
pounds=Double.parseDouble(inputString);
if(pounds<=0)
{System.out.println("Invalid pounds value. Must be positive.");
System.out.print("Re-enter weight in pounds:");
inputString = input.nextLine();
pounds=Double.parseDouble(inputString);
}
System.out.println("Height = "+inches+""");
System.out.println("Weight = "+pounds+" pounds");
System.out.printf("Body Mass Index = %.1f ",(pounds*703.)/(inches*inches));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.