Monday, 14 February 2011

Gridworld Part 3

Assume the following statements when answering the following questions.
Location loc1 = new Location(4, 3);
Location loc2 = new Location(3, 4);

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


1. loc1.getRow() would return the row value of loc1.

2. What is the value of  b  after the following statement is executed?
boolean b = loc1.equals(loc2);

2. The value of b will be false because loc1 does not have the same values for row and column as loc2.

3. What is the value of loc3  after the following statement is executed?
Location loc3 = loc2.getAdjacentLocation(Location.SOUTH); 

3. loc3 will have the values (4, 4).

4. What is the value of dir  after the following statement is executed?
 int dir = loc1.getDirectionToward(new Location(6, 5));

4. dir will have the value 135 because Location(6, 5) is directly South East from loc1.


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

5. It knows which adjacent location to return by taking a direction as a parameter, which will direct it towards the requested location.


pg. 21
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?

1. You can make a loop to go through the grid incrementing rows and collumns by 2 in order to not count the same object twice, and increment a counter value by the getNeighbors() method for each of the locations. You can do the same but with incrementing the counter value by the getEmptyAdjacentLocations() method to find the empty locations in a bounded grid.


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

2. You can call the isValid() method and put the location (10, 10) as a parameter. If it returns true it is valid, otherwise it is false.

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

3. 3. Because some might be labeled as abstract and to be implemented by its subclasses. In the subclasses you can find implementations of these methods.
 

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.  

4. No, because an ArrayList can grow infinitely while an array has a set size. It could be that we would want to mess with the size of the ArrayList, and therefore, it serves as a better holder of the objects.

pg. 23
1. Name three properties of every actor.

1. They all have a color, a direction, and an implementation of the act() method.

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

2. When an actor is constructed its direction is north and its color is blue.

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

3. It was probably made as a class instead of an interface because it is a general rule for all objects on the grid, and not necessarily something they implement. The Actor class also defines fields which an interface cannot do, which apply to its subclasses.

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?

4. No, it can't put itself into a grid twice without first removing itself. No it also can't remove itself twice. It also can't place itself, then remove itself, and then put itself back, because as soon as it is removed it disappears.

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

5. It can call its setDirection() method and place the direction + 90 as the parameter of the method thus turning it 90 degrees to the right.

pg. 25-26
1
Which statement(s) in the canMove()  method ensures that a bug does not try to move out of its grid?  
The statement "if(!gr.isValid(next))
    return false;
else ... ensures the bug doesn't try to move out of the grid.


2
Which statement(s) in the canMove() method determines that a bug will not walk into a rock? 
 The statement "return (neighbor == null) || (neighbor instanceof Flower)" determines that a bug will not walk into a walk because it evaluates to false if the neighbor object is a rock, which will cause the canMove() method to be false as a whole and not let the bug walk into a rock.


3
Which methods of the Grid  interface are invoked by the canMove()  method and why?
The isValid() method of the Grid interface is invoked by the canMove method to check whether the location chosen by the bug is still in grid.


4
Which method of the Location  class is invoked by the canMove() method and why?
The Location class is invoked in order to find a new location for the bug to move to invoking its getAdjacentLocation() method.

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

6
What happens in the move() method when the location immediately in front of the bug is out of the grid?
When the location immediately in front of the bug is out of the grid it removes itself from the grid.


7
Is the variable loc needed in the move() method, or could it be avoided by calling getLocation() multiple times?
It is not needed in the move() method because the loc variable is only referred to once, and therefore is redundant.
 

8
Why do you think the flowers that are dropped by a bug have the same color as the bug?
Because it calls the getColor() method when constructed which will return the bug's color, and construct a flower with that color.
 

9
When a bug removes itself from the grid, will it place a flower into its previous location?
Depends when. If it simply calls the removeSelfFromGrid() method then it won't. However, there is one case where it will. In the move() method, if the bug finds no valid place to go to, it will remove itself from the grid, but at the same time place a flower in its place.
 

10
Which statement(s) in the  move() method places the flower into the grid at the bug’s previous location?
"Flower flower = new Flower(getColor());
flower.putSlefInGrid(gr, loc);" puts a flower in the bug's previous location.
 

11
If a bug needs to turn 180 degrees, how many times should it call the turn() method?
It will have to call the turn() method 4 times, because each one call moves the but 45 degrees to the right.



1 comment:

  1. All your posts are very helpful...Thank You very much!!!!!

    ReplyDelete