Java Reference
In-Depth Information
Exercise 10.39
Review the overriding rules for methods and fields discussed in ChapterĀ 9.
Why are they particularly significant in our attempts to introduce inheritance into this
application?
Exercise 10.40
The changes made in this section have removed the dependences (cou-
plings) of the
simulateOneStep
method on the
Fox
and
Rabbit
classes. The
Simulator
class, however, is still coupled to
Fox
and
Rabbit
, because these classes are referenced in
the
populate
method. There is no way to avoid this; when we create animal instances, we
have to specify exactly what kind of animal to create.
This could be improved by splitting the
Simulator
into two classes: one class,
Simulator
,
that runs the simulation and is completely decoupled from the concrete animal classes, and
another class,
PopulationGenerator
(created and called by the simulator), that creates
the population. Only this class is coupled to the concrete animal classes, making it easier for
a maintenance programmer to find places where change is necessary when the application is
extended. Try implementing this refactoring step. The
PopulationGenerator
class should
also define the colors for each type of animal.
The project
foxes-and-rabbits-v2
provides an implementation of our simulation with the
improvements discussed here. It is important to note that the change in
Simulator
to process-
ing all the animals in a single list, rather than in separate lists, means that the simulation results
in version 2 will not be identical to those in version 1.
In the topic projects, you will find a third version of this project:
foxes-and-rabbits-graph
. This
project is identical to
foxes-and-rabbits-v2
in its model (i.e., the animal/fox/rabbit/simulator
implementations), but it adds a second view to the project: a graph showing population num-
bers over time. We will discuss some aspects of its implementation a little later in this chapter;
for now, just experiment with this project.
Exercise 10.41
Open and run the
foxes-and-rabbits-graph
project. Pay attention to the
Graph View
output. Explain, in writing, the meaning of the graph you see, and try to explain
why it looks the way it looks. Is there a relationship between the two curves?
Exercise 10.42
Repeat some of your experiments with different sizes of fields (especially
smaller fields). Does the graph view give you any new insights or help you understand or
explain what you see?
If you have done all the exercises in this chapter so far, then your version of the project will be
the same as
foxes-and-rabbits-v2
and similar to
foxes-and-rabbits-graph
, except for the graph
display. You can continue the exercises from here on with either version of these projects.
10.4
More abstract methods
When we created the
Animal
superclass in Section 10.3, we did this by identifying common
elements of the subclasses, but we chose not to move the
age
field and the methods associated
with it. This might be overly conservative. We could, in fact, have quite easily moved the
age
Search WWH ::
Custom Search