Question

In: Computer Science

uppose you have a pre-existing class Dieter that models a person trying to monitor diet and...

uppose 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

Make Dieter objects comparable to each other using the Comparable interface.

You are just writing a compreTo method for the class and not any other method.

Write the compareTo method for the class Dieter using the following criteria.

  • 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  

Solutions

Expert Solution

// Dieter.java

public class Dieter implements Comparable<Dieter> {

   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
  
   // constructor that creates a dieter with given name, height, weight
   public Dieter(String n, int h, int w)
   {
       name = n;
       height = h;
       weight = w;
   }
  
   // returns the dieter's name
   public String getName()
   {
       return name;
   }
  
   // returns the dieter's height
   public int getHeight()
   {
       return weight;
   }
  
   // returns the dieter's weight
   public int getWeight()
   {
       return weight;
   }
  
   // computes the dieter's Body Mass Index (BMI), which is (weight / height2) • 703
   public double getBMI()
   {
       return (((double)weight)/Math.pow(height,2))*703;
   }
  
   // method called when the dieter gains weight
   public void gain(int pounds)
   {
       weight += pounds;
   }
  
   // method called when the dieter loses weight
   public void lose(int pounds)
   {
       weight -= pounds;
       if(weight < 0)
           weight = 0;
   }

  
   /*
   * method to compare 2 Dieter and returns
   * < 0, if this Dieter < other
   * > 0, if this Dieter > other
   * = 0, if this Dieter = other
   */
   @Override
   public int compareTo(Dieter other) {
      
       // compare Dieter based on BMI
       if(getBMI() < other.getBMI())
           return -1;
       else if(getBMI() > other.getBMI())
           return 1;
       else
       {
           // BMI are equal
           // compare Dieter based on height
           // shorter person is considered to be "less than" the taller one
           if(height < other.height)
               return -1;
           else if(height > other.height)
               return 1;
           else // same height, compare the Dieters based on name
               // The dieter whose name comes first in alphabetical order is considered
               // "less than" the one whose name comes later in ABC order.
               return name.compareTo(other.name);
       }
   }
}

//end of Dieter.java


Related Solutions

Using Classes This problem relates to the pre-loaded class Person. Using the Person class, write a...
Using Classes This problem relates to the pre-loaded class Person. Using the Person class, write a function print_friend_info(person) which accepts a single argument, of type Person, and: prints out their name prints out their age if the person has any friends, prints 'Friends with {name}' Write a function create_fry() which returns a Person instance representing Fry. Fry is 25 and his full name is 'Philip J. Fry' Write a function make_friends(person_one, person_two) which sets each argument as the friend of...
You are the operations manager at your firm. Due to a pre-existing contract, you have the...
You are the operations manager at your firm. Due to a pre-existing contract, you have the opportunity to acquire 200 barrels of oil and 3000 pounds of copper for a total of $25,000. The current market price of oil is $90 per barrel and for copper is $3.50 per pound. You are not sure that you need all the oil and copper, so you are wondering whether you should take this opportunity. How much is the value of the opportunity?...
Many existing video surveillance systems come with pre-packaged analytics that are which one below? Monitor all...
Many existing video surveillance systems come with pre-packaged analytics that are which one below? Monitor all the stairwells Monitor all the exits/entrances Look for anomalies Search facial imagery for known criminals
You are maintaining a collection for fwooper , and are trying to adjust their diet to...
You are maintaining a collection for fwooper , and are trying to adjust their diet to keep them from gaining or losing too much weight. You know that fwoopers in the wild have a mean mass of 125 grams but other than that you don’t know much else. The data you collect initially about the fwooper mass is in the excel data table. fwooper mass (g) 1 143.9 2 141.9 3 122.9 4 106.8 5 120.3 6 163.3 7 126...
How have you contributed to classroom participation, either in class pre-pandemic or electronically post-pandemic?
How have you contributed to classroom participation, either in class pre-pandemic or electronically post-pandemic?
Without any changes in their diet a person has been loosing weight and their feces have...
Without any changes in their diet a person has been loosing weight and their feces have become fatty. The medical diagnosis is "a condition limiting the production of bile". Why do you think inadequate bile leads to the symptoms of weight loss and fatty feces? Your answer must discuss the role of bile in fat digestion.
1- Write a class called MedicalStaff that is a Person (the class that you wrote in...
1- Write a class called MedicalStaff that is a Person (the class that you wrote in last lab). A MedicalStaff has specialty (i.e. Orthopedic, cardiology, etc.). 2- Then write two classes: Doctor class has office visit fee. Nurse class has title (i.e. RN, NP, etc.) Both classes inherit from MedicalStaff. Be sure these are all complete classes, including toString method. 3- Write a tester to test these classes and their methods, by creating an array or ArrayList of Person and...
1.You are trying to determine what the best combination of diet and exercise to provide the...
1.You are trying to determine what the best combination of diet and exercise to provide the most weight loss. You decided to try three different diets (Atkins, Paleo, McDonald's only), along with no dieting, and two different forms of exercise (Yoga, high cardio), along with no exercise. You try each combination possible, and track weight loss for each. How many independent variables do you have? 2.You are trying to determine what the best combination of diet and exercise provides the...
Write a Java class called Person. The class should have the following fields: A field for...
Write a Java class called Person. The class should have the following fields: A field for the person’s name. A field for the person’s SSN. A field for the person’s taxable income. A (Boolean) field for the person’s marital status. The Person class should have a getter and setter for each field. The Person class should have a constructor that takes no parameters and sets the fields to the following values: The name field should be set to “unknown”. The...
In this homework you will implement a Library class that uses your Book and Person class...
In this homework you will implement a Library class that uses your Book and Person class from homework 2, with slight modifications. The Library class will keep track of people with membership and the books that they have checked out. Book.java You will need to modify your Book.java from homework 2 in the following ways: field: dueDate (private)             A String containing the date book is due.  Dates are given in the format "DD MM YYYY", such as "01 02 2017"...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT