Demo 1

As I was compiling and running the program, I noticed two files called SimpleMBSDemo1.java and SimpleMBSDemo2.java. I decided to run the first one and discovered that it was a simpler version of the simulation program with just three fish. It didn't have Step and Run buttons; instead, it always ran for 15 timesteps.

Although it wasn’t as interesting as the full version of the program, the filename said “Simple” so I thought that I might be able to read it and understand it even before meeting with Jamie. I decided to look at the code. I noticed that it defined several named constants, and then consisted of a main method that created the environment (represented by a class called BoundedGrid), then created three fish and a SimpleMBSDisplay object, and finally ran through a loop, telling the fish to "act." I noticed that the program specified the initial location of each fish as it was constructed, but I wasn't sure why the program was also passing the environment object to the Fish constructor, and made a note to ask Jamie when we met.

Analysis Questions

  1. Read over the SimpleMBSDemo1 class. How is it similar to, and different from, the main method in the Aquarium Lab Series before you refactored it?
  2. Given what you know about the Grid Package from previous labs and programming projects (especially Histogram and GridPlotter), why do you think the Fish constructor takes two parameters: a grid and a location?
(A Markdown template for writing up answers to the Analysis Questions on this page is here.)

Demo 2

Now I was curious about what the difference was between SimpleMBSDemo1 and SimpleMBSDemo2. I ran SimpleMBSDemo2, but it seemed to do pretty much what SimpleMBSDemo1 had done. (It was a little difficult to know for sure, since the behavior of every run of the program was different anyway.) So I decided to compare the code in the two files. The only difference was near the end. The loop in the first demo completely disappeared in Demo 2, which instead created an object of a new class, called Simulation, and called a run method on it, specifying the number of steps.

I found the code for SimpleMBSDemo2 harder to understand, and I was glad that I had looked at SimpleMBSDemo1 first. First of all, SimpleMBSDemo2 constructs the display object, but never asks it to show the environment. Secondly, even though SimpleMBSDemo2 constructs three fish just as SimpleMBSDemo1 did, it never asks them to act. Instead, it constructs the Simulation object and asks it to run. I noticed that the program passes the environment and the display object to the Simulation constructor, so I assumed that the Simulation object must ask the display object to show the environment when the Simulation object is constructed. I also assumed that its run method must repeatedly ask the fish to act, although I wasn’t sure how, since the program never passes the fish to the simulation.

Analysis

Simulation step illustration; click to enlarge

Answer Pat's Questions

  1. Where and when is the display object asked to show the environment?
  2. How does the sim.run(NUM_STEPS); statement in Demo 2 achieve the same effects as the for loop in Demo 1?
  3. How does the step method in Simulation know which fish to ask to act? How did it get enough information from SimpleMBSDemo2 to be able to do this?
  4. Explain how the diagram to the right illustrates the step method. (Click to view a larger version.) (Note: the diagram contains a couple of typos in it; for example, it refers to a showEnv method, which is actually called showGrid.)

Optional Additional Experimentation



Alyce Brady, Kalamazoo College