Create a class called BMI with the following instance variables: name, age, weig
ID: 3838852 • 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
//BMI class
public class BMI{
private String Name;
private double weight ;
private double height ;
public BMI()
{
Name = "";
weight = 0.0;
height = 0.0;
}
public BMI(String s,double w, double h)
{
Name = s;
weight = w;
height = h;
}
//mutator functions
public void setName(String s)
{
Name =s;
}
public void setWeight(double w)
{
weight =w;
}
public void setHeight(double h)
{
height =h;
}
//accessor functions
public String getName()
{
return Name;
}
public double getWeight()
{
return weight ;
}
public double getHeight()
{
return height ;
}
@Override
public String toString()
{
String tmp;
tmp = "Name: '" + this.Name + "', Height: '" + this.height + "', Weight: '" + this.weight;
return tmp;
}
public double calBMI()
{
double bmi;
bmi = (weight*0.45 *height * 0.0254)/(height*0.0254 * height*0.0254);
return bmi;
}
public String getStatus()
{
double bmi = calBMI();
System.out.printf("BMI = %f ",bmi);
String tmp = "";
if(bmi < 16 && bmi <= 18)
tmp = "seriously underweight";
if(bmi > 16 && bmi <= 18 )
tmp = "underweight";
if(bmi > 18 && bmi <=24)
tmp = "normal weight";
if(bmi > 24 && bmi <= 29 )
tmp = "overweight";
if(bmi > 29 && bmi <= 35 )
tmp = "seriously overweight";
if(bmi > 35 )
tmp = "gravely overweight";
return tmp;
}
public boolean equals(BMI obj)
{
if (obj.calBMI() == this.calBMI())
{
return true;
}
else
return false;
}
}
---------------------------------------------------------
//driver class
public class Driver
{
public static void main(String []args)
{
BMI bm1 = new BMI("John",70.78,58);
BMI bm2 = new BMI("Smith",89.78,60);
System.out.printf("Bm1 = %s BMI= %f ",bm1.toString(),bm1.calBMI());
System.out.printf("Bm2 = %s BMI= %f ",bm2.toString(),bm2.calBMI());
System.out.printf("Status of bm1= %s ",bm1.getStatus());
System.out.printf("Status of bm2 = %s ",bm2.getStatus());
if(bm1.equals(bm2))
System.out.println("BMI of two person is equal ");
else
System.out.println("BMI of two person is not equal ");
}
}
--------------------------------------------------------------
//output
java -Xmx128M -Xms16M Driver BMI
Bm1 = Name: 'John', Height: '58.0', Weight: '70.78 BMI= 21.620282
Bm2 = Name: 'Smith', Height: '60.0', Weight: '89.78 BMI= 26.509843
BMI = 21.620282
Status of bm1= normal weight
BMI = 26.509843
Status of bm2 = overweight
BMI of two person is not equal
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.