Static
Static
EvilRobot class with static
EvilRobotHarness
Classwork
Create a class Raptor. Each Raptor has a health, which starts at 50.
Add the usual methods.
Give the Raptor class a static variable population which is an array of
Raptors (no accessor or mutator). Also, 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 public
static 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 method
populationSize that returns how many Raptors are currently in the array.
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.
Every time a new Raptor is created, add that raptor to the
population array. (Don't worry about what happens to Raptors when there isn't
room in the array -- we'll just assume excess Raptor eggs are eaten by
something).
Add a (non-static) method hunt in Raptor
- If a raptor's health is <= 0, it is dead and cannot hunt
- Live Raptors
have a 50/50 chance of success at hunting
- or [EC+5] The likelihood that
a live Raptor will hunt successfully is
proportional to how close preyPopulation currently 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 to determine if the Raptor was successful
based on this chance. (hint: you need to generate random
numbers in some range, and then check whether the number generated
is in the success part of the range or the fail part of the range.
If we are thinking of the success as "25%" or "60%" or "99%"
what is the natural range to use?)
- If it hunts successfully, decrease the preyPopulation by 1 and
increase the Raptor's health by 20 (don't worry about the prey population going negative) otherwise decrease its health by 20
Add a (non-static) method reproduce.
- If a raptor's health is <= 0, it is dead and cannot reproduce
-
Live Raptors have a 50/50 chance of success at reproducing
- or [EC+5] The likelihood that
a live
Raptor will successfully reproduce is inversely proportional to how
close the Raptor
population size is to 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.
- If
it is successful, create a new Raptor with the same health as the
parent.
Add a static method day() During a day:
- the size of the prey population increases by preypop *
(1 - preypop/MAX_POP) (This should always round down to the
next int)
- each Raptor in the population tries to hunt
- each Raptor tries to reproduce
In a separate class with a main
- first create 10 new raptors
- call the day method every day for a year. Every day, report the current number of
Raptors and prey.
[EC+30]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 count of raptors in the population by one.
(Don't assume there's only 1 dead Raptor!) Call cleanOut at the
end of each day.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 solution works!LDLLDLLLLDLDLLDDLDLDLDDDDLDDDDDD