Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Animals need energy to move. They get energy from eating food. Moving consumes energy. Create a class Animal with a constructor that takes no parameters

Animals need energy to move. They get energy from eating food. Moving consumes energy.

Create a class Animal with a constructor that takes no parameters and has an instance variable:

private int energy;

When an animal is "born", it has one unit of energy.

It has the methods:

public void eat(int amountToEat) - which increases the amount of energy the animal has by amountToEat.

public void move(int amountToMove) - which decreases the energy the animal has by amountToMove.

public int getEnergy() - which returns the amount of energy the animal has left.

Notice there is no setEnergy method. Energy is only changed by eating or moving.

It isn't realistic for animals to be able to gather infinite amounts of energy, to have negative energy, or to eat or move a negative amount.

Create a subclass BetterAnimal which has a cap on the amount of energy an animal can have. The constructor takes a parameter that specifies a maximum for energy. You will need to save this in another instance variable.

Override the eat and move methods.

In the eat method: If the amount the BetterAnimal eats would set its energy above the max, the energy level is only increased to the max. Also, energy is only changed if the amount > 0.

In the move method: Energy changes only if amount > 0. The energy can never be less than 0. If an BetterAnimal has an energy of 2 and tries to move 5, its energy will be 0.

Provide Javadoc for both classes.

AnimalTester.java

public class AnimalTester { public static void main(String [] args) { Animal animal = new BetterAnimal(25); System.out.println("just born : " + animal.getEnergy()); System.out.println("Expected: 1"); animal.move(10); System.out.println("move too much: " + animal.getEnergy()); System.out.println("Expected: 0"); animal.eat(15); System.out.println("eat: " + animal.getEnergy()); System.out.println("Expected: 15"); animal.eat(-5); System.out.println("eat negative: " + animal.getEnergy()); System.out.println("Expected: 15"); animal.move(-5); System.out.println("move negative: " + animal.getEnergy()); System.out.println("Expected: 15"); animal.move(5); System.out.println("move: " + animal.getEnergy()); System.out.println("Expected: 10"); animal.move(20); System.out.println("move to much: " + animal.getEnergy()); System.out.println("Expected: 0"); animal.eat(50); System.out.println("eat above max: " + animal.getEnergy()); System.out.println("Expected: 25"); animal = new BetterAnimal(10); System.out.println("just born : " + animal.getEnergy()); System.out.println("Expected: 1"); animal.eat(5); System.out.println("eat: " + animal.getEnergy()); System.out.println("Expected: 6"); animal.eat(4); System.out.println("eat to max: " + animal.getEnergy()); System.out.println("Expected: 10"); animal.eat(5); System.out.println("eat past max: " + animal.getEnergy()); System.out.println("Expected: 10"); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic 4 Ole Database And Controls Superbible

Authors: Michael Hatmaker, C. Woody Butler, Ibrahim Malluf, Bill Potter

1st Edition

1571690077, 978-1571690074

More Books

Students also viewed these Databases questions