In: Computer Science
In this activity, we are going to implement the previous algorithm in Java. Just in case, here is problem again: This program calculates and displays a person's body mass index (BMI). The BMI is calculated as the weight times 703 divided by the height squared where the weight is measured in pounds and the height in inches. The program should display a message indicating whether the person has optimal weight (BMI between 18.5 and 25), is underweight (BMI is less than 18.5), or overweight (BMI is greater than 25).
Important considerations:
1. There are several ways to design and implement this algorithm; however, for the purpose of this exercise and for an automatic grading, we are going to use the algorithm developed in the previous exercise.
2. Because of the softchalk limitation about the uses of double quotation marks, and for uniformity, the String objects have been created for you.
3. For the calculation, you must use the Math library for the exponentiation.
4. The curly braces for the if-else statement blocks are located in separate lines for uniformity.
Given the following skeleton, please add the missing instructions
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
double weight;
double height;
double bmi;
String stWeight = "Enter your weight (pounds) ";
String stHeight = "Enter your height (inches) ";
String stBMI = "Your BMI is ";
String stOver = "You are considered Overweight";
String stOptimal = "You have optimal weight";
String stUnder = "You are considered Underweight";
Fill in: ALL OF THE BLANKS HAVE TO BE FILLED IN!!
//Input prompt the user for weight and height in that order
System.out.println(stWeight); |
System.out.println(stHeight); |
//Calculate BMI
// Display results of calculation and the message in different lines
//Overweight
//Optimal Weight
//Underweight
}
CODE FOR THE FOLLOWING PROGRAM:-
import java.util.Scanner; // Import the Scanner class
import java.lang.Math; //Import the math class
public class Main
{
public static void main (String[]args)
{
// TODO Auto-generated method stub
Scanner scan = new Scanner (System.in);
double weight;
double height;
double bmi;
String stWeight = "Enter your weight (pounds) ";
String stHeight = "Enter your height (inches) ";
String stBMI = "Your BMI is ";
String stOver = "You are considered Overweight";
String stOptimal = "You have optimal weight";
String stUnder = "You are considered Underweight";
//Input prompt the user for weight and height in that order
System.out.println(stWeight);
weight=scan.nextDouble();
System.out.println(stHeight);
height=scan.nextDouble();
//Calculate BMI Formula: 703 x weight (lbs) / [height (in)]2
bmi = (703 * weight) / Math.pow(height, 2);
// Display results of calculation and the message in different lines
//Underweight
if (bmi <= 18.5)
{
System.out.println (stUnder);
}
//Optimal Weight
else if (bmi > 18.5 && bmi < 25)
{
System.out.println (stOptimal);
}
//Overweight
else if (bmi >= 25)
{
System.out.println (stUnder);
}
}
}
SCREENSHOT OF THE PROGRAM AND SAMPLE OUTPUT:-
SAMPLE OUTPUT:-
HAPPY LEARNING