This chapters Exercise 15 presents code containing the statement ActorWorld world = new ActorWorld(); After that, we
Question:
This chapter’s Exercise 15 presents code containing the statement ActorWorld world = new ActorWorld();
After that, we said, “ActorWorld’s constructor immediately creates a 10 × 10 BoundedGrid.” But if you look at GridWorld’s source code for the ActorWorld class, you’ll see that the zero-parameter constructor is empty:
How does this empty constructor create “a 10 × 10 BoundedGrid”?
Exercise 15
GridWorld’s firstProject file contains the BugRunner driver:
BugRunner starts by creating an instance of ActorWorld named world. ActorWorld’s constructor immediately creates a 10 × 10 BoundedGrid. Next, BugRunner asks the ActorWorld object to add a new Bug at a random location, and it then adds a new Rock at a different random location. ActorWorld’s add method tells each new occupant where it is supposed to go in the grid and then tells that occupant to put itself into the grid at that location.
BugRunner’s last statement calls ActorWorld’s show method. This show method instantiates a WorldFrame (a GUI window) with an argument referring back to the ActorWorld object.
WorldFrame’s constructor instantiates a GridPanel. Then it uses its reference to the ActorWorld object to get a reference to the BoundedGrid, and it passes this BoundedGrid reference on to the new GridPanel. GridPanel’s drawOccupants method uses this BoundedGrid reference to call BoundedGrid’s getOccupiedLocations method. This allows GridPanel’s paintComponent method to put bug and rock images in their proper places each time it repaints the screen.
Provide an abbreviated UML class diagram that contains all classes identified in the above two paragraphs, except for the BugRunner class. Use appropriate composition (filled diamond) and aggregation (hollow diamond) links. Include multiplicity symbols where one of the multiplicities might be greater than 1. For the relationship between ActorWorld and WorldFrame, use a simple bidirectional association, like this:
Or use a bidirectional dependence, like this:
Step by Step Answer:
Introduction To Programming With Java A Problem Solving Approach
ISBN: 9781260575248
3rd International Edition
Authors: John Dean