logo Practice-It logo


Related Links:
Author: Marty Stepp (on 2010/12/28)

Suppose you have a pre-existing class Dieter that models a person trying to monitor diet and weight to improve his/her Body Mass Index (BMI) rating. The class has the following data and behavior:

Field/Constructor/Method Description
private String name the dieter's full name, such as "John Smith"
private int height the dieter's height, in inches
private int weight the dieter's weight, in pounds
public Dieter(String name, int h, int w) makes a dieter with given name, height, weight
public String getName() returns the dieter's name
public int getHeight() returns the dieter's height
public int getWeight() returns the dieter's weight
public double getBMI() computes the dieter's Body Mass Index (BMI), which is (weight / height2) * 703
public void gain(int pounds) called when the dieter gains weight
public void lose(int pounds) called when the dieter loses weight

Your task is to modify the class to be Comparable by adding an appropriate compareTo method. Dieters are primarily compared by their BMI ratings; a dieter with a lower BMI is "less than" one with a higher BMI. If two dieters have the same BMI, the tie is broken by height; the shorter person is considered to be "less than" the taller one. If two dieters have the same BMI and height, the tie is broken by name. The dieter whose name comes first in alphabetical order is considered "less than" the one whose name comes later in ABC order. If the two dieters have the same BMI, height, and name, they are considered to be "equal." Your method should not modify any dieter's state. You may assume the parameter passed is not null.

Type your solution here:

This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.