当前位置: 代码迷 >> 综合 >> Part3
  详细解决方案

Part3

热度:53   发布时间:2024-03-08 05:37:55.0

Set 3

1.How would you access the row value for loc1?

使用getRow()函数loc1.getRow()

  //@file: info/gridworld/grid/Location.java//@line: 110~113public int getRow(){return row;}

2.What is the value of b after the following statement is executed?

boolean b = loc1.equals(loc2);
false

//@file: info/gridworld/grid/Location.java//@line:205~212public boolean equals(Object other)
{if (!(other instanceof Location))return false;Location otherLoc = (Location) other;return getRow() == otherLoc.getRow() && getCol() == otherLoc.getCol();
}

3.What is the value of loc3 after the following statement is executed?

Location loc3 = loc2.getAdjacentLocation(Location.SOUTH);

(4,4)

4.What is the value of dir after the following statement is executed?

int dir = loc1.getDirectionToward(new Location(6, 5));

Southeast

5.How does the getAdjacentLocation method know which adjacent location to return?

The parameter in the getAdjacentLocation method indicates the direction of the adjacent neighbor to find. It returns the adjacent location in the compass direction that is closest to the direction given in the parameter list.

Set 4

1.How can you obtain a count of the objects in a grid? How can you obtain a count of the empty locations in a bounded grid?

assume that a is a reference to a Grid object.
a.getOccupiedLocations().size() is the number of occupied location.
a.getNumRows() * a.getNumcols()-a.getOccupiedLocations().size() is the number of the empty locations.

//@file: info/gridworld/grid/Grid.java//@line: 85ArrayList<Location> getOccupiedLocations();//@line: 35int getNumRows();//@line: 41int getNumCols();

2.How can you check if location (10,10) is in a grid?

gr.isValid(new Location(10,10))

//@file: info/gridworld/grid/Grid.java
//@line: 50
boolean isValid(Location loc);

3.Grid contains method declarations, but no code is supplied in the methods. Why? Where can you find the implementations of these methods?

Grid is an interface. In Java, an interface specifies which methods another class must implement. You can find the implementations of the methods in the AbstractGrid and the BoundedGrid and UnboundedGrid classes. Since the AbstractGrid only implements some of the required methods of the Grid interface, it is declared an abstract class. The BoundedGrid and UnboundedGrid extend the AbstractGrid class and implement the rest of the methods required by the Grid interface

4.All methods that return multiple objects return them in an ArrayList. Do you think it would be a better design to return the objects in an array? Explain your answer.

As a user of the Grid classes, perhaps. Accessing elements with the [] notation is somewhat easier than using different method calls. For example: locs[j] versus locs.get(j)
In terms of implementing the methods, an ArrayList does not require the user to size the list before filling it. An array does. Since the BoundedGrid does not keep track of the number of objects in the grid, you would have to first count the number of occupied locations to size the array, and then go back through the grid to find and store each of the locations. If the Grid kept track of the number of occupied locations, filling an array would be just as easy as filling an ArrayList.

Set 5

1.Name three properties of every actor.

An actor has a color, a direction, and a location.

2.When an actor is constructed, what is its direction and color?

An actor’s initial color is blue and its initial direction is North

//@file: info/gridworld/actor/Actor.java
//@line: 39~45
public Actor()
{color = Color.BLUE;direction = Location.NORTH;grid = null;location = null;
}

3.Why do you think that the Actor class was created as a class instead of an interface?

An actor has both state and behavior. An interface does not allow the programmer to declare instance variables or implement methods

4.Can an actor put itself into a grid twice without first removing itself? Can an actor remove itself from a grid twice? Can an actor be placed into a grid, remove itself, and then put itself back? Try it out. What happens?

(a) No—if the actor is already in the grid, it may not put itself in the grid again. This version of BugRunner.java will compile, but when it runs it will throw an IllegalStateException.

//@file: info/gridworld/actor/Actor.java
//@line: 117~118if (grid != null)throw new IllegalStateException("This actor is already contained in a grid.");

(b) No—if the actor has already been removed from the grid (and is currently out of the grid), it cannot be removed again. This version of BugRunner.java will compile, but when it runs it will throw an IllegalStateException.

//@file: info/gridworld/grid/Location.java
//@line: 135~137if (grid == null)throw new IllegalStateException("This actor is not contained in a grid.");

? Yes—an actor can be placed in the grid, remove itself, and then put itself back in the grid.


5.How can an actor turn 90 degrees to the right?

To turn an actor 90 degrees to the right, use the setDirection method as follows in an Actor class: setDirection(getDirection() + Location.RIGHT) or setDirection(getDirection() + 90);

	//@file: info/gridworld/actor/Actor.java//@line: 80~85
public void setDirection(int newDirection)
{direction = newDirection % Location.FULL_CIRCLE;if (direction < 0)direction += Location.FULL_CIRCLE;
}

set 6

1.Which statement(s) in the canMove method ensures that a bug does not try to move out of its grid?

//@file: info/gridworld/actor/Bug.java
//@line: 98~99
f(!gr.isValid(next))return false; 

2.Which statement(s) in the canMove method determines that a bug will not walk into a rock?

//@file: info/gridworld/actor/Bug.java
//@line:100~101
Actor neighbor = gr.get(next);
return (neighbor == null) || (neighbor instanceof Flower); 

3.Which methods of the Grid interface are invoked by the canMove method and why?

isValid() and get().

These methods are called to ensure that the next location is a valid location in the grid and to look at the object in that location to ensure that it is empty or contains an actor that can be replaced by the bug.

//@file: info/gridworld/actor/Bug.java
//@line:98~100
if (!gr.isValid(next))return false;Actor neighbor = gr.get(next);

4.Which method of the Location class is invoked by the canMove method and why?

getAdjacentLocation

This method is called by the bug with the bug’s current direction to find its next possible location

//@file: info/gridworld/actor/Bug.java
//@line:97Location next = loc.getAdjacentLocation(getDirection());

5.Which methods inherited from the Actor class are invoked in the canMove method?

getLocation, getDirection, getGrid

//@file: info/gridworld/actor/Bug.java
//@line:93Grid<Actor> gr = getGrid();
//@line:97
Location next = loc.getAdjacentLocation(getDirection());
//@line:96Location loc = getLocation();

6.What happens in the move method when the location immediately in front of the bug is out of the grid?

The bug will remove itself from the grid.

//@file: info/gridworld/actor/Bug.java
//@line:78~81if (gr.isValid(next))moveTo(next);elseremoveSelfFromGrid();

7.Is the variable loc needed in the move method, or could it be avoided by calling getLocation() multiple times?

Yes, the variable loc is needed. The variable loc stores the bug’s location before it moves. It is used to insert a flower in the bug’s old location after the bug has moved to its new location.

//@file: info/gridworld/actor/Bug.java
//@line:76~77Location loc = getLocation();Location next = loc.getAdjacentLocation(getDirection());//@line:82~83Flower flower = new Flower(getColor());flower.putSelfInGrid(gr, loc);

8.Why do you think the flowers that are dropped by a bug have the same color as the bug?

Initially (before the flower wilts), it is easier to see which bug dropped what flowers because the color of the bug and its flowers are the same.

9.When a bug removes itself from the grid, will it place a flower into its previous location?

If you just call the removeSelfFromGrid method, no. This method is inherited from the Actor class. Actors do not put a flower in their old location. When the removeSelfFromGrid is called in the Bug move method, yes. A flower will be placed in a bug’s vacated location. The following lines from the move method show these actions.

	//@file: info/gridworld/actor/Bug.java//@line:78~83if (gr.isValid(next))moveTo(next);elseremoveSelfFromGrid();Flower flower = new Flower(getColor());flower.putSelfInGrid(gr,loc); 

10.Which statement(s) in the move method places the flower into the grid at the bug’s previous location?

//@file: info/gridworld/actor/Bug.java
//@line:82~83
Flower flower = new Flower(getColor());
flower.putSelfInGrid(gr, loc); //loc is the old location of the bug 

11.If a bug needs to turn 180 degrees, how many times should it call the turn method?

Four times—each turn is a 45 degree turn.

//@file: info/gridworld/actor/Bug.java
//@line:62~65public void turn()
{setDirection(getDirection() + Location.HALF_RIGHT);
}
  相关解决方案