Lab 8: Simulation
Bard College – Computer Science – Object-Oriented Programming

Predator-prey simulations are used to understand how populations of animals interact. In this lab’s agent-based simulation, there will be two types of animals: Prey and Predator, for example, rabbits and wolves. Your task is to program the individual animal behaviors. 

Both types of animals run around randomly and reproduce with some probability. The predators eat nearby prey, removing prey from the population. The predators gain energy from eating the prey, but will die if their energy drops below zero. Animals are born with some amount of energy, and predators expend energy hunting each time-step. The predators are also less likely to reproduce if their energy is low.

The Animal parent class and the World class are provided, in addition to the main sketch. Your task is to implement the Prey and Predator child classes. You should not modify the World or Animal classes. Resist the urge to copy and paste; use inheritance. First, get a simple Predator displayed and moving around, then a Prey—develop the classes iteratively getting one aspect to work before moving on to the next piece of functionality.

Next, run experiments with your simulation. Gather data about how the populations change as you vary your parameters: initial population size, speed, movement strategy, reproduction rate. Describe what you find in a comment at the top of the file.

BONUS:  Try exchanging your classes with a classmate.

BONUS: Make the code use an ArrayList of Animals rather than a statically sized array.

TIPS

  1. Create the two new child classes in new tabs;
  1. Override & specialize the display method;
  1. Override & specialize the step method to have the animal reproduce and die.

Learning Objectives

  • Use inheritance
  • Explore agent-based simulation.
  • Read a multi-class program.
  • Write more classes.

Submission (due November 19th, 2021)

  • Your program should start with a comment that includes your name(s), email(s), date, assignment description & collaboration statement.
  • Bring a hardcopy of your programs (the source code, not the graphics) to your next lab period. 
  • Submit a zip file to Google Classroom (using the correct folder & file names).

Starter Code

World w;

void setup() {
  size(500, 500);
  w = new World();
  for (int i = 0; i < 50; i++) {
    w.addAnimal(new Prey(w));
  }

  for (int i = 0; i < 10; i++) {
    w.addAnimal(new Predator(w));
  }
}

void draw() {
  background(196);
  w.tic();
  drawPop();
}