subject

Animals need energy to move. They get energy from eating food while 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 left
Notice there is no setEnergy method. Energy is only changed by eating or moving.
Since 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, you should 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.

ansver
Answers: 2

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 18:00, teamroper35
Which finger presses the h key on the keyboard? index finger on the left hand pinky finger on the right hand index finger on the right hand thumb on the left hand
Answers: 1
image
Computers and Technology, 24.06.2019 05:00, alfarodougoy8lvt
Who is most likely be your target audience if you create a slide presentation that had yellow background and purple text
Answers: 2
image
Computers and Technology, 24.06.2019 16:00, altstattlana
Read these lines from beowulf. often scyld scefing seized mead-benches from enemytroops, from many a clan, he terrified warriors, even thoughfirst he was found a waif, best explains why the author includes this information in theexposition? a. to emphasize that a hero must learn to be fierceb. to remember the famous story of a popular heroc. to express sadness about losing the old heroesd. to see whether people still respect the old heroes
Answers: 1
image
Computers and Technology, 25.06.2019 00:30, kukisbae
How do i take a screenshot on a laptop?
Answers: 2
You know the right answer?
Animals need energy to move. They get energy from eating food while moving consumes energy. Create a...

Questions in other subjects:

Konu
Mathematics, 16.09.2020 02:01
Konu
English, 16.09.2020 02:01
Konu
Mathematics, 16.09.2020 02:01
Konu
Mathematics, 16.09.2020 02:01
Konu
Business, 16.09.2020 02:01
Konu
Mathematics, 16.09.2020 02:01
Konu
Mathematics, 16.09.2020 02:01
Konu
Mathematics, 16.09.2020 02:01
Konu
Mathematics, 16.09.2020 02:01
Konu
Mathematics, 16.09.2020 02:01