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
(A Markdown template for writing up answers to the Analysis Questions on this page is here.)
- Read over the
SimpleMBSDemo1
class. How is it similar to, and different from, themain
method in the Aquarium Lab Series before you refactored it?- 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?
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
- Read over the
SimpleMBSDemo2
andSimulation
classes. Notice how they are similar to, and different from, your refactored Aquarium Lab Series program.Thestep
method in theSimulation
class asks the grid for a list of all the objects (fish) in it. The list is returned as an array of grid objects (GridObject[]
), rather than an ArrayList. You can step through it using the same type of for-each loop that you use with ArrayLists. We will come back to the topic of arrays later in the term.Answer Pat's Questions
- Where and when is the display object asked to show the environment?
- How does the
sim.run(NUM_STEPS);
statement in Demo 2 achieve the same effects as the for loop in Demo 1?- How does the
step
method inSimulation
know which fish to ask to act? How did it get enough information fromSimpleMBSDemo2
to be able to do this?- 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 ashowEnv
method, which is actually calledshowGrid
.)
Optional Additional Experimentation
Could
SimpleMBSDemo2
create multiple simulations? Each simulation would need its own grid, its own fish, and its own display. Try it.Tips:
- Put the two calls to
sim1.run
andsim2.run
at the end of the method, after you have created all of the objects you need for both simulations. Can you see both displays simultaneously? (They might come up on top of each other, so you might have to pull one aside.)- Why does one simulation run through all its steps before the other one starts? If you put a loop in
SimpleMBSDemo2
and call each simulation'sstep
method directly, can you see the two simulations run in parallel?