Question: Homework 5-1 Textbook Section 10.7 make a class to represent a Food object. Use the description provided below in UML. Food name : String calories

Homework 5-1 Textbook Section 10.7

make a class to represent aFood object. Use the description provided below in UML.

Food

  • name : String
  • calories : int

  • Food(String, int) // The only constructor.Food name and calories must be

// specified

  • setName(String) : void // Sets thenameof the Food
  • getName() : String // Returns thename of theFood
  • setCalories(int) : void // Sets thecalories of theFood
  • getCalories() : int // Returns thecalories of theFood
  • toString() : String @Override //see Note1:
  • equals(Object) : boolean @Override //see Note2:

NOTE1:

toString() method returns a String with the following format:

"Food - name: %10s | calories: %4d"

NOTE2:

Two instances of Food are equal if, and only if, their names are equal and their calories are equal.

Homework 5-2 Textbook Section 10.8

Change thePassenger class fromHomework 4-1, to anabstract class and add the following to it:

Implement theEater Interface.

Passenger // the class becomes anabstract class

  • caloriesConsumed: int // this field is initialized to zero and will always increase
  • caloriesAccumulator: int // this field is initialized to zero and is an accumulator

// that is reset whenmetabolizeAccumulatedCalories()

// (see below)

  • getCaloriesConsumed() : int
  • setCaloriesConsumed(int) : void
  • getCaloriesAccumulator() : int
  • setCaloriesAccumulator(int) : void
  • metabolizeAccumulatedCalories() : double // anabstract method that returns adouble

// representing the amount of weight gained.

// Classes that inherit from Passenger will have to implement this method.

// Weight gain is calculated based on the number of accumulated calories

// represented bycaloriesAccumulator.

// Every Passenger uses a different formula for weight gain.

// In addition to returning the amount of weight gained, this method will update

// the Passenger's weight and zero outcaloriesAccumulator.

Change thePerson class fromHomework 4-2, to anabstract class.

Homework 5-3 Textbook Section 10.9

Modify theInfant class fromHomework 4-3 as follows:

  • Implement theabstract methodmetabolizeAccumulatedCalories(). Please note thatInfants gain one pound for every 1200 accumulated calories.
  • Implement theMover Interface.Infants return"flail arms and legs".

Homework 5-4 Textbook Section 10.10

make a class to represent aTeen object that inherits from (extends) thePerson class. TheTeen class will:

  • Implement theabstract methodmetabolizeAccumulatedCalories(). Please note thatTeens gain one pound for every 5000 accumulated calories.
  • Implement theMover Interface.Teens return "run fast and jump".

Homework 5-5 Textbook Section 10.11

make a class to represent aElder object that inherits from (extends) thePerson class. TheElder class will:

  • Implement theabstract methodmetabolizeAccumulatedCalories(). Please note thatElders gain one pound for every 300 accumulated calories.
  • Implement theMover Interface.Elders return "ouch my back wait for me".

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!