Create a class called BMI with the following instance variables: name, age, weig
ID: 3835597 • Letter: C
Question
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 the following formula. You need to conver the height to meter 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”
Also include a driver class to create couple of the objects and output the bmi and the status for each person.
Explanation / Answer
BMITest.java
public class BMITest {
public static void main(String[] args) {
BMI b1 = new BMI();
b1.setAge(11);
b1.setName("Suresh");
b1.setHeight(1.1);
b1.setWeight(121.1);
System.out.println(b1);
System.out.println(b1.getBMI());
BMI b2 = new BMI();
b2.setAge(21);
b2.setName("Sekhar");
b2.setHeight(11.1);
b2.setWeight(21.1);
System.out.println(b2);
System.out.println(b1.equals(b2));
System.out.println(b2.getBMI());
}
}
BMI.java
public class BMI {
private String name;
private int age;
private double weight, height;
public BMI() {
}
public BMI(String name, int age){
this.name = name;
this.age = age;
}
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 double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public String toString() {
return "BMI [age=" + age + ", height=" + height + ", name=" + name
+ ", weight=" + weight + "]";
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BMI other = (BMI) obj;
if (age != other.age)
return false;
if (Double.doubleToLongBits(height) != Double
.doubleToLongBits(other.height))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(weight) != Double
.doubleToLongBits(other.weight))
return false;
return true;
}
public String getBMI() {
double w = 0.45 * weight;
double h = 0.0254 * height;
double bmi = w * h/ (h * h);
if(bmi < 16){
return "seriously underweight";
}
else if(bmi >= 16 && bmi < 18){
return "underweight";
}
else if(bmi >= 18 && bmi < 24){
return "normal weight";
}
else if(bmi>= 24 && bmi < 29){
return "overweight";
}
else if(bmi>= 29 && bmi < 35){
return "overweight";
}
else {
return "gravely overweight";
}
}
}
Output:
BMI [age=11, height=1.1, name=Suresh, weight=121.1]
gravely overweight
BMI [age=21, height=11.1, name=Sekhar, weight=21.1]
false
overweight
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.