Chapter 2. Programming and Simulation

Table of Contents
2.1. What is an Object?
2.2. The Variety of Objects
2.3. The Advantages of Object Oriented Programming
2.3.1. Encapsulation
2.3.2. Inheritance
2.4. Discrete Event Simulation

Swarm is designed to help researchers build models in which low-level actors interact (often called "complex systems"). The researcher has to give content to "agents," possibly by thinking of them as honey bees, investors, trees, or (the ubiquitous) "bugs." One research goal is to discern overall patterns that emerge from these detailed behaviors at the individual level.

Figure 2-1. Agent-based modeling

Object oriented programming is ideally suited to represent models of this sort. As we shall see, the objects are self-contained. Objects may be designed to convey information (answer questions) from other objects and also they can retain, categorize, and summarize information.

2.1. What is an Object?

A careful study of either of the object-oriented programming languages (Java or Objective-C) is required before any significant progress can be made in building a Swarm model. The material presented here is intended as a summary or reminder of such a study, rather than a substitute.

An object consists of two kinds of information

Variables and methods are given meaningful names, so code is easier to read. The custom is to run words together to make meaningful tags, such as goToStore or goHome.

Objects are created through a process called "instantiation." Put tersely, code is written in "classes" and then objects are created as instances of their classes. The varibles that an instance, or object, keeps inside itself are called "instance variables". The information contained inside instance variables is available to all methods inside that object. If one of the methods in an object needs to have "private" information that is not available to other methods in the object, then "method variables" can be created to hold that information.

In both Objective-C and Java, the term message is often used to refer to an instruction that tells an object to carry out one of its methods. (For readers more familiar to C++, the term member function, refers to the same thing as the term method). Here is an example of a message that tells an object known as bobDole to execute its method runForPresident.

Objective C exampleJava example
[bobDole runForPresident];
bobDole.runForPresident();

In Objective C, some methods have parameters that specify details and they are added with colons (:) after the name of the method to be executed. In Java, the entire method name is listed before the parameters are given [1]. For example, if the method runForPresident required additional parameters, such as the year and the name of the runningmate, then the message might look like so:

Objective C exampleJava example
[bobDole runForPresident:2000 with: RossPerot];
bobDole.runForPresident$with (2000, RossPerot);

We will have plenty of additional examples in the rest of the Guide.

Notes

[1]

In our Java example we use a dollar sign ($) inline between the parts of the method that are separated in the Objective C case. This is purely a convention introduced to stay as close to conventions adopted by the Java Swarm libraries. This is in no way enforced by the Java language itself.