BMI class: create FindBMI() method to return BMI value according to height and w
ID: 3809482 • Letter: B
Question
BMI class:
create FindBMI() method to return BMI value according to height and weight.
BMI.java
public class BMI
{
private double height, weight;
BMI(double height, double weight)
{
this.height = height;
this.weight = weight;
}
BMI()
{
height = 0.0;
weight = 0.0;
}
void setHeight(double updateHeight)
{
height = updateHeight;
}
void setWeight(double updateWeight)
{
weight = updateWeight;
}
double getHeight()
{
return height;
}
double getWeight()
{
return weight;
}
// 1. create FindBMI() method to return BMI value according to height and weight:
public String toString()
{
return "Height " + height + " Weight: " + weight;
}
}
Explanation / Answer
Hi,
Below is the code in Java for BMI using accessor, getter and setter methods-
Code below-
import java.util.Scanner;
public class BMI
{
private static double height, weight;
BMI(double height, double weight)
{
this.height = height;
this.weight = weight;
}
BMI()
{
height = 0.0;
weight = 0.0;
}
void setHeight(double updateHeight)
{
height = updateHeight;
}
void setWeight(double updateWeight)
{
weight = updateWeight;
}
double getHeight()
{
return height;
}
double getWeight()
{
return weight;
}
public double FindBMI(double w,double h)
{
double bmi;
bmi=((w*703)/(h*h));
return bmi;
}
public String toString()
{
return "Height " + height + " Weight: " + weight;
}
public static void main(String[] args){
double bmi_value;
Scanner in = new Scanner(System.in);
BMI ppp=new BMI();
System.out.print("Enter your weight in pounds: ");
weight = in.nextInt();
ppp.setWeight(weight);
System.out.print("Enter your height: ");
height = in.nextInt();
ppp.setHeight(height);
bmi_value=ppp.FindBMI(weight,height);
System.out.printf ("Your Body Mass Index (BMI) is %f ", bmi_value);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.