The foxes-and-rabbits project

Một phần của tài liệu Objects first with java a practical introduction using bluej 5th edition (Trang 356 - 359)

10.2 The foxes-and-rabbits simulation

10.2.1 The foxes-and-rabbits project

Open the foxes-and-rabbits-v1 project. The class diagram is shown in Figure 10.1.

Rabbit

Fox

Field

Location

Randomizer FieldStats

Counter SimulatorView

Simulator Figure 10.1

Class diagram of the foxes-and-rabbits project

10.2 The foxes-and-rabbits simulation | 329

The main classes we will focus on in our discussion are Simulator,Fox, and Rabbit. The Fox and Rabbit classes provide simple models of the behavior of a predator and prey, respec- tively. In this particular implementation, we have not tried to provide an accurate biological model of real foxes and rabbits; rather, we are simply trying to illustrate the principles of typical predator–prey simulations. Our main concerns will be on the aspects that most affect population size: birth, death, and food supply.

TheSimulator class is responsible for creating the initial state of the simulation, and then controlling and executing it. The basic idea is simple: the simulator holds collections of foxes and rabbits, and it repeatedly gives those animals an opportunity to live through one step2 of their life cycle. At each step, each fox and each rabbit is allowed to carry out the actions that characterize their behaviors. After each step (when all the animals have had the chance to act), the new current state of the field is displayed on screen.

We can summarize the purpose of the remaining classes as follows:

Field represents a two-dimensional enclosed field. The field is composed of a fixed num- ber of locations, which are arranged in rows and columns. At most, one animal may occupy a single location within the field. Each field location can hold an animal or it can be empty.

Location represents a two-dimensional position within the field, specified by a row and a column value.

■ These five classes together (Simulator,Fox,Rabbit,Field, and Location) provide the model for the simulation. They completely determine the simulation behavior.

■ The Randomizer class provides us with a degree of control over random aspects of the simulation, such as when new animals are born.

■ The classes SimulatorView,FieldStats, and Counter provide a graphical display of the simulation. The display shows an image of the field and counters for each species (the current number of rabbits and foxes).

SimulatorView provides a visualization of the state of the field. An example can be seen in Figure 10.2.

FieldStats provides to the visualization counts of the numbers of foxes and rabbits in the field.

■ ACounter stores a current count for one type of animal to assist with the counting.

Try the following exercises to gain an understanding of how the simulation operates before reading about its implementation.

2We won’t define how much time a “step” actually represents. In practice, this has to be decided by a combination of such things as what we are trying to discover, what events we are simulating, and how much real time is available to run the simulation.

Exercise 10.1 Create a Simulator object, using the constructor without parameters, and you should see an initial state of the simulation similar to that in Figure 10.2. The more numerous rectangles represent the rabbits. Does the number of foxes change if you call the simulateOneStep method just once?

Figure 10.2 The initial state of the foxes-and-rabbits simulation

Exercise 10.2 Does the number of foxes change on every step? What natural processes do you think we are modeling that cause the number of foxes to increase or decrease?

Exercise 10.3 Call the simulate method with a parameter to run the simulation continu- ously for a significant number of steps, such as 50 or 100. Do the numbers of foxes and rab- bits increase or decrease at similar rates?

Exercise 10.4 What changes do you notice if you run the simulation for a much longer time, say for 4,000 steps? You can use the runLongSimulation method to do this.

Exercise 10.5 Use the reset method to create a new starting state for the simulation, and then run it again. Is an identical simulation run this time? If not, do you see broadly similar pat- terns emerging anyway?

Exercise 10.6 If you run a simulation for long enough, do all of the foxes or all of the rabbits ever die off completely? If so, can you pinpoint any reasons why that might be occurring?

Exercise 10.7 Make a note of the numbers of foxes and rabbits at each of the first few steps and at the end of a long run. It will be useful to have a record of these when we come to make changes later on and perform regression testing.

Exercise 10.8 After having run the simulation for a while, reset it and also call the static reset method of the Randomizer class. Now run the first few steps again, and you should see the original simulation repeated. Take a look at the code of the Randomizer class to see if you can work out why this might be. You might need to look at the API for the java.util.

Random class to help you with this.

10.2 The foxes-and-rabbits simulation | 331

Now that we have a broad, external understanding of what this project does, we will look in detail at the implementation of the Rabbit,Fox, and Simulator classes.

Một phần của tài liệu Objects first with java a practical introduction using bluej 5th edition (Trang 356 - 359)

Tải bản đầy đủ (PDF)

(578 trang)