In: Computer Science
Using class, write a program that:
Answer:
Here is the java code as per your requirement
Raw code:
filename: BMI.java
//BMI Class file
public class BMI{
//method to calculate bmi
public void calculateBmi(double height,double weight){
//declaring variable
double bmi;
//bmi formula
bmi=703*weight/(height*height);
//calculating bmi based on formula
if(bmi<18.5){
System.out.println("You are underweight");
}
else if(bmi>18.5){
System.out.println("You have a normal weight");
}
else if(bmi>24.9){
System.out.println("Your weight is considered overweight");
}
else if(bmi>30){
System.out.println("your weight is considered as obese");
}
}
}
Editor:
Filename: Main.java
// importing Scanner class
import java.util.Scanner;
//main
class Main {
//main method
public static void main(String[] args) {
//Scanner object
Scanner sc = new Scanner(System.in);
//declaring variables
int weight,height;
//asking user for height and weight
System.out.print("Enter height(in): ");
height=sc.nextInt();
System.out.print("Enter weight(lbs): ");
weight=sc.nextInt();
//creating BMI object
BMI b = new BMI();
//calling the method of bmi class
b.calculateBmi(height, weight);
}
}
Editor:
output:
Hope this helps you! If you still have any doubts or queries please feel free to comment in the comment section.
"Please refer to the screenshot of the code to understand the indentation of the code".
Thank you! Do upvote.