ITI 1121. Introduction to Computer Science II
Laboratory 3

Summer 2009

Objectives

. Creating a hierarchy of classes
. Further understanding of inheritance
. Further understanding of polymorphism

Biological Game of Life

This laboratory brings together two applications. One of them is the Game
of Life; invented by the Cambridge mathematician John Conway. The game
consists of a two dimensional grid, where each cell is occupied or not.
Based on simple rules, a cell can live, die or multiply. Since this
laboratory is about inheritance, we are introducing a new concept to the
game: let the content of a cell be an Organism, where an Organism can be
either a Plant or an Animal. The animals are further divided into
herbivores and carnivores. All the organisms have an update method that
determines, based on information about its neighborhood, if this Organism
will survive to the next generation. The particular survival rules depend
on the particular Organism.
The class Simulation is the heart of this application. A run consists of
fixed number of discrete generations. At the start of the simulation, a two
dimensional grid is created and initialized with a population of organisms.
At each generation, the simulation gathers, for each Organism, the list of
its neighbors; a cell has a maximum of 8 neighbors. The method update of
the Organism receives the list of neighbors and this information is used to
determine if the Organism remains alive or dies. New organisms come to life
at each generation, and the dead organisms are removed from the grid. The
simulation ends when the maximum number of generations has been reached.
You will be implementing the classes that determine behavior of the
simulation; the class Organism and its descendants.

1 Organism

Implement an abstract class, called Organism, to represent the
characteristics that are common to all the organisms. Namely,
1. An Organism is alive or not; all the organisms are alive at birth;
1. It has an instance method, boolean isAlive(), that returns true if
this Organism is alive, and false otherwise;
1. It declares an abstract (instance) method, void update(Organism[]
neighbors), that will be implemented by the subclasses of Organisms.

2 Plant

Create a subclass of Organism to represent the characteristics of all
plants. This will be a concrete class called Plant.
1. Give an implementation for the method void update(Organism[]
neighbors),
o A Plant dies when surrounded by 5 or more neighboring organisms;

o A Plant dies if it is surrounded by 2 or more Herbivores.
If none of the above rules apply, a Plant can live forever.
1. Implement a method public String toString() to return the String "P"
(quotes not included).

3 Animal

Create an abstract subclass of Organism to represent the characteristics of
all animals, call this class Animal.
1. All animals have an age. At the birth of the Animal, its age is 0. At
every generation (each call to the method update) the age will be
incremented by 1;
1. Create an access method that returns the age of this Animal.

4 Herbivore

A Herbivore is an Animal that eats plants (i.e. is a subclass of Animal).
1. Implement the method public void update(Organism[] neighbours), such
that,
o A Herbivore dies unless it is surrounded by at least one Plant;
o A Herbivore cannot live more than 20 generations.
1. Implement a method public String toString() to return the String "H"
(quotes not included).

5 Carnivore

A Carnivore is an Animal that eats other animals.
1. A Carnivore must eat regularly. If a Carnivore is not surrounded by at
least one Animal for a given generation, this counts as one fasting
generation;
1. Implement the method public void update(Organism[] neighbours), such
that,
o A Carnivore dies after four consecutive generations of fasting;
o A Carnivore cannot live more than 15 generations.
1. Implement a method public String toString() to return the String "C"
(quotes not included).

6 Simulation

As explained in the introduction, the Simulation is the hearth of this
program. It creates the initial population of Organisms and orchestrates
the simulation. Complete the implementation by creating the following two
methods.
1. Implement the method public String toString() that displays the
content of the grid. The format is as follows, the header consists of
the label "Generation = " followed by the current number of
generations. It is followed by a two dimensional grid of cells, where
each cell is represented by the String "[X]", where X is