A local health club wants to computerize the Body Mass Index (BMI) calculation f
ID: 3624183 • Letter: A
Question
A local health club wants to computerize the Body Mass Index (BMI)calculation for its members. The BMI calculator uses height and weight to
calculate the BMI. The BMI formula is: BMI = ( Weight in Pounds / (
Height in inches ) x ( Height in inches ) ) x 703. The BMI calculator
should have default & nondefault constructors, 1 pair of
accessors/mutators, a toString() or display() method, and equals(),
compareTo() methods as well as any additional methods to do the necessary
calculation. You can order the compareTo() method by BMI. Write an
application class to test your BMI class.
So far I have done everything right except for the compareTo method and incorporating it into my toString method. Find my BMI class and the BMIApp class below. Help me edit it accordingly.
import java.lang.Math;
import java.text.*;
public class BMI
{
private double height;
private double weight;
public static final double DEFAULT_HEIGHT = 50.00;
public static final double DEFAULT_WEIGHT = 100.00;
//default
public BMI()
{
setWeight(DEFAULT_WEIGHT);
setHeight(DEFAULT_HEIGHT);
}
//non-default
public BMI(double newHeight, double newWeight)
{
setWeight(newWeight);
setHeight(newHeight);
}
public double getHeight()//accessor
{
return height;
}
public void setHeight(double newHeight)//mutator
{
if (newHeight > DEFAULT_HEIGHT)
height=newHeight;
else
height = DEFAULT_HEIGHT;
}
public double getWeight()//accessor
{
return weight;
}
public void setWeight(double newWeight)//mutator
{
if (newWeight> DEFAULT_WEIGHT)
weight=newWeight;
else
weight = DEFAULT_WEIGHT;
}
public double calcBMI()//calculates the BMI of the person based on the height and weight using the formula below
{
double BMI = (double)(weight/Math.pow(height,2)) * 703.0;
return BMI;
}
public String toString()
{
return "With your height: "+ height+ " inches(in) and your weight: "+weight+ " pounds(lb) your BMI is: " + calcBMI();
}
public boolean equals(BMI that)//checks if two BMIs are the same and gives a true/false result
{
if (this.getWeight() == that.getWeight() && this.getHeight() == that.getHeight())
return true;
else
return false;
}
public int compareTo(BMI that)
{
if(this.getWeight() == that.getWeight() && this.getHeight() == that.getHeight())
return 0; //returns zero to show that there is no difference
else if(this.getWeight() > that.getWeight() && this.getHeight() < that.getHeight())
return 1; //positive if the first BMI is greater than the other BMI
else
return -1;//returns negative if the second BMI is less than the other BMI
}
public void compareMessage(BMI that)
{
if (this.calcBMI() == that.calcBMI())
System.out.println("Person 1's BMI is equal to person 2's"+ " ");
else
System.out.println("Person 1's BMI is not equal to person 2's"+ " ");
//this is where the problem starts
if(this.compareTo(BMI that) = 0)
System.out.println("They are of equal BMI" +" ");
else if(compareTo(BMI that) = 1)
System.out.println("The positive number: "+1+ " shows that the former person has a greater BMI"+ " ");
else
System.out.println("The negative number: " + -1+ " shows that the former person has a smaller BMI" + " ");
}
}
This is my test class below
import java.lang.Math;
import java.util.Scanner;
public class BMIApp
{
public static void main(String []args)
{
Scanner input = new Scanner(System.in);
double height;//instantiating the private variables from the BMI class that would be used here
double weight;
BMI person1 = new BMI();
person1.setHeight(75.0);
person1.setWeight(198.0);
double b = person1.calcBMI();
System.out.println(person1.toString()+ " ");
BMI person2 = new BMI();
person2.setHeight(58.0);
person2.setWeight(145.0);
double b = person2.calcBMI();
System.out.println(person2.toString()+ " ");
person1.compareMessage(person2);
BMI person3 = new BMI();
System.out.println("Enter your height: ");
height = input.nextDouble();
System.out.println("Enter your weight: ");
weight = input.nextDouble();
person3.setHeight(height);
person3.setWeight(weight);
double b = person3.calcBMI();
System.out.println(person3.toString()+ " ");
BMI person4 = new BMI();
System.out.println("Enter your height: ");
height = input.nextDouble();
System.out.println("Enter weight: ");
weight = input.nextDouble();
person4.setHeight(height);
person4.setWeight(weight);
double b =person4.calcBMI();
System.out.println(person4.toString()+ " ");
person3.compareMessage(person4);
}
}
Explanation / Answer
Cast to a boxed type and use compareTo. Otherwise you should let your BMI class implement Comparable public void compareMessage(BMI that) { int compareVal = ((Double)this.calcBMI()).compareTo(that.calcBMI()); if (compareVal == 0) System.out.println("Person 1's BMI is equal to person 2's"+ " "); else if (compareVal < 0) System.out.println("Person 1's BMI is smaller than person 2's"+ " "); else System.out.println("Person 2's BMI is greater than person 1: "); }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.