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 this array, which is also a count of the
total number of Raptors in the population (do not forget you have this
variable; credit will not be given for later methods that need it if you
do not use it!).
☑ Also create a public
static constant
int MAX_POP, which is 100. We won'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 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 (think about what method this means), 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 static method chance which takes
a percentage. Using random numbers, return true with the
percentage chance given and false otherwise. (So chance(20) will
return true 20% of the time, not a 1 in 20 chance). There are
several ways to do this.
☑ 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, (you wrote a
method!)
- If it hunts successfully, increase the Raptor's health by 20 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 with less than 40 health have a 1/10 chance of success
at reproducing, other live Raptors have a 50/50 chance of success at reproducing
- If
it is successful, create a new Raptor with the same health as the
parent.
☑ Add a static method day() During a day:
- 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 until there are 100 raptors or 0 raptors (they die out).
Every day, report the current number of Raptors.