Static
EvilRobot class with static
EvilRobotHarness
Classwork
Correct answers will only use concepts and constructs we have covered in class.
Create a class Raptor. Each Raptor has a health, which starts at 100.
Add the usual methods.
Give the Raptor class a static variable population which is an array of
Raptors. We will also need, to go with it, a variable to keep
track of the first empty spot in the array, which is also a count of the
total number of Raptors in the population. Also create a constant
int MAX_POP, which is 100. Don't provide accessors or
mutators for any of these.
In a static block, set up the array to be size MAX_POP.
Add a static int preyPopulation, and in the static block, start this at
50. Do provide accessor and mutator for this.
Add a static method addRaptor, which takes a Raptor and adds it to the
population array if there is still room.
add a static method
populationSize that returns how many Raptors are currently in the array.
Every time the constructor for Raptor is called, add that raptor to the
population array. (Don't worry about the case where there isn't
room in the array -- we'll just assume excess Raptor eggs are eaten by the prey species).
Add a (non-static) method hunt.
- The likelihood that the Raptor will hunt successfully is
determined by how close preyPopulation is to MAX_POP. If
preyPopulation is 50% of MAX_POP, there is a 50/50 chance, if it is
25% of MAX_POP, there is a 25% chance, if it is 60% of MAX_POP there
is a 60% chance, etc.
- Generate a random number based on this
chance to determine if the Raptor was successful.
- If it hunts successfully, decrease the preyPopulation by 1 and
increase the Raptor's health by 20.
- Otherwise decrease its health by 20
Add a (non-static) method reproduce.
- The likelihood that the
Raptor will successfully reproduce is determined by how far the Raptor
population size is from MAX_POP. If the population is 50% of
MAX_POP, there is a 50/50 chance, if it is 25% of MAX_POP, there is a
75% chance, if it is 60% of MAX_POP there is a 40% chance, etc.
- Generate a random number based on this chance to determine if
the Raptor was successful.
- It
is is successful, create a new Raptor with the same health as the
parent.
We need to keep track of raptors who have run out of health and died.
[option 1] Add a static method liveRaptors which runs through the
population and counts how many are still alive (health 1 or higher), and
returns this number.
[option 2, EC+25] Add a static method cleanOut which runs through the population looking for
dead (health less than 1) Raptors. Each one it finds, it removes
by moving the last Raptor currently in the array into that spot, and
then decreasing the number of raptors in the population by one. (Don't assume there's only 1 dead Raptor!)
To make it easy to test this, add a static method setPopulation that
allows you to pass in an array of Raptors and a number, and set the
population array and first empty variable accordingly, and a static
method showPopulation which prints a String that shows the live an dead
raptors in the array with L being a live Raptor and D a dead Raptor.
e.g. LLLDLLL for an array with three live raptors, one dead, and three
live, where firstempty is 7.
[EC+20] Make sure your code handles cases like the following with L being a live Raptor and
D a dead Raptor (and the rest of the array full of nulls, not that this
matters). Clearly comment your code to explain how your code works!
LDLLD
LLLLD
LDLLDD
LDLDLDDDDL
DDDDD
D
Add a static method day. During a day:
- the size of the prey population increases by 2* preypop *
(1 - preypop/MAX_POP) (This should always round down to the
next int)
- each Raptor tries to hunt
- each Raptor tries to reproduce
- if doing that EC, clean out any Raptors that died during the day
In a separate class with a main
- first create 10 new raptors
- call the day method every day for a year, but stop if all the
Raptors die out (you will determine this differently depending on
whether you did the extra credit). Every day, report the current number of
Raptors and prey.