function:RenderSidePart pageleftbodycaption pageleftbody sidenote Main.AntFarm-SideNote Main.SideNote Site.SideNote

# Ant Farm

See a video of it running here.

This project was based on Robert Glen Martin's Art Farm, which I adapted to make ants remember exhausted food supplies, so they effectively go back and forth between random "hunt" mode and "bring food the queen" mode. As you work through this project, you will complete an interface and both concrete and abstract classes. Your solution will demonstrate inheritance, encapsulation, and polymorphism. Prior to beginning this project, you must read and understand the first four chapters of the GridWorld Student Manual.

The project utilizes four new types of objects: two kinds of food ( Cookie and Cake) and two kinds of ants ( WorkerAnt and QueenAnt). Initially, the worker ants walk around in search of food. When they find food, they take a bite. Ants with food turn red. Then the worker ants go in search of a queen ant to give food. Once they give their food to a queen, they turn black and go back to get more food.

Food and queens remain stationary. Worker ants remember the locations of the food and queen. Additionally, they share those locations with other worker ants they meet. When the Ant Farm program starts, the worker ants are spread around the grid in random locations. Initially, they are disorganized as they search for food. As the worker ants start to find food and the queen, they get more organized (see Figure 2 below). After all the ants learn the locations, they exhibit an emergent behavior that is very organized.

GridWorld has a "built-in" Actor class for objects that "live" in the grid. Actors that have minimal interaction with other objects in the grid normally inherit from Actor. This is appropriate for QueenAnt and Food. Cake and Cookie inherit indirectly from Actor.

The other "built-in" Actor is Critter, which inherits from Actor. Critters have additional methods that are useful for interacting with other Actors. WorkerAnts need to "communicate" with the QueenAnt, Cake, Cookie, and other WorkerAnt objects. So inheriting from Critter is appropriate for them.

Ant Farm also has a new Processable interface. This interface has a single process method that is the key to communication between worker ants and the other Actors. It is critical that you understand the Processable interface and how it is used in Ant Farm.

# Processable.javapublic interface Processable { public void process(WorkerAnt ant); } 

The Processable interface, contains a single void process method. This process method takes one parameter of type WorkerAnt. Add the method heading for the process method in the Processable interface. Note: All interface methods are automatically public and abstract.

When implemented in QueenAnt, Food, and WorkerAnt, the process method processes (communicates with) a single WorkerAnt object (the one passed as a parameter). This interface allows worker ants to invoke the other Actor's process methods polymorphically. The individual process methods in each class will do the following:

• QueenAnt
• Get food from the worker ant.
• Give the queen's location to the worker ant.
• Food
• Give food to the worker ant.
• Give the food's location to the worker ant.
• WorkerAnt
• Give the saved food location to the other worker ant.
• Give the saved queen location to the other worker ant.

Note that Ant Farm uses the Processable interface to implement an interface variant of the Template Design Pattern. The Template Design Pattern normally uses an abstract class to contain the abstract method(s). Then concrete classes (which inherit from the abstract class) implement the method(s) as appropriate. In Ant Farm, we use the Processable interface to hold the abstract process method. process methods are written in the QueenAnt, Food, and WorkerAnt classes, each of which implement Processable.