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. |
|
|
All your posts are very helpful...Thank You very much!!!!!
ReplyDelete