Project 2: registerPredator() and registerPrey()

Oct. 07, 2009

Posted by PERRY, JO under Information
0 Comments | Permalink

The Animal class declares two abstract methods: registerPredator() and registerPrey(). Here is why.

Each Animal in the simulation ecosystem must be associated with a concrete Predator or Prey. But when you create an instance of Predator, no instance of Prey may yet exist. At some time, you have to decide which species (which instance) is is going to be the prey for the instance of Predator you just created. So suppose you create coyote as an instance of Predator and later create mouse as an instance of Prey. Then this statement determines that mouse is prey for the coyote:

coyote.registerPrey(mouse);

Similarly, you want to do this for mouse:

mouse.registerPredator(coyote);

registerPredator() registerPrey() are setter methods with the prefix "register" instead of "set."



Post a Comment:
Comments are closed for this entry.