Can someone help me with this? Create a class called BMI with the following inst
ID: 3582147 • Letter: C
Question
Can someone help me with this?
Create a class called BMI with the following instance variables: name, age, weight (in pounds), and height. Create a constructor for your class so that you can create objects of type
BMI. Include all the methods such as accessors, mutators, toString, equals. Also include a method called getBMI which calculates the BMI for the person and returns the BMI. Use thefollowing formula. You need to convert the height to
inches and the weigh to kilogram. 1 kg =
.45 pounds
1 meter = .0254 inches
bmi = (weight in kg * heigh(in meter)) / (height in meter)* (height in meter)
Also include a method called getStatus to return a String representing the person’s status
bmi < 16 returns “seriously underweight
bmi < 18 returns
“underweight”
bmi < 24 “returns
normal weight”
bmi < 29 returns “overweight”
bmi < 35 returns “seriously overweight”
Anything else returns “gravely overweight”
Methods bmi and getStatus do not receive any parameters
Also include a driver class to create couple of the objects and output the bmi and the status for each person.
Explanation / Answer
Hello, I went through your question and have given the solution below. As you have asked for the driver class, a driver class is a simple class which has a main function where you can create an object of any class within the package. Now your second requirement to get the status and bmi of a person without an argument doesn't makes sense. You need to pass either the object of the BMI Class or the height and weight of the person as arguments. I have made a function taking height and weight as arguments and then calulating the BMI for it.
//Class BMI
public class BMI {
private String name;
private int age;
private int height;
private int weight;
// constructor for the Class BMI, Taking weight in pounds and height in inches
public BMI(String name,int age,int height,int weight){
this.name=name;
this.age=age;
this.height=height;
this.weight=weight;
}
// Method to calculate BMI Index
public float getBMI(int height, int weight){
float weightInKg=(weight*100)/45;
float heightInMeter=(height*254)/100;
float bmiIndex=(weightInKg)/(heightInMeter*heightInMeter);
return bmiIndex;
}
//Method to get status
public String getStatus(float bmiIndex){
String status="";
if(bmiIndex<16){
status="seriously underweight";
return status;
}
if(bmiIndex>=16 && bmiIndex<18 ){
status="underweight";
return status;
}
if(bmiIndex>=18 && bmiIndex<24){
status="normal weight";
return status;
}
if(bmiIndex>=24 && bmiIndex<29){
status="overweight";
return status;
}
if(bmiIndex>=29 && bmiIndex<35){
status="seriously overweight";
return status;
}
status="gravely overweight";
return status;
}
// getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
Thank you!!