Monday, 28 February 2011

Barons

I forgot my Baron's book at school, I will do it tomorrow in my free block.

Tuesday, 22 February 2011

Barons 357-374 Multiple Choice

How did you do on the multiple choice questions?
I did pretty well, I got only a few mistakes, and most were not conceptual.

Did you understand your errors?
Not all...

What do you not understand?
 In the turning questions, I was confused. E.g. question 2 stated that when a bug turns it takes its direction and adds 45 degrees to it. So if it was South, it would be 180 + 45 = 225. Mathematically speaking, 225 degrees is South East not South West as the book said it was.

Grid World Part 4 Part 2

        

Gridworld: Part 4: Interacting Objects: Set 9: Page 35





1
Why doesn’t CrabCritter override the processActors() method?  
 It doesn't override the processActors() method because the implementation of the method in the critter class is to eat other actors, and the crab does the same with movement restrictions and eating restrictions.
2
Describe the process a CrabCritter uses to find and eat other actors.

Does it always eat all neighboring actors?

Explain.  
 After moving either left or right (depending on the random value) it will check if a neighbor which is not a rock and is a critter is in front, front left, or front right of it and remove it. It won't eat all neighboring actors if they aren't critters, or if they're a rock. This because of the implementation of the processActors() in the critter class.
3
Why is the getLocationsInDirections() method used in CrabCritter?  
 I am not sure about this question. I can't really understand the getLocationsInDirections() method.
4
If a CrabCritter has location (3, 4) and faces south, what are the possible locations for actors that are returned by a call to the getActors()  method?  
 I was under the impression that getActors() returns all actors in the grid, but I assume it is only the locations around the critter, therefore they would be (2,3), (3,3), (4, 3), (2,4), (4,4), (2,5), (3, 5), (4, 5).
5
What are the similarities and differences between the movements of a CrabCritter and a Critter?  
 CrabCritter moves in each call of act(), just as critter does; however, it can only move right, left, or turn 90 degrees either left or right, and relies on random values in order to determine its movement.
6
How does a CrabCritter determine when it turns instead of moving?  
 It checks whether it's movement is blocked in any direction. If it is, then it turns instead of moving.
7
Why don’t the CrabCritter objects eat each other?  
 I thought CrabCritter objects can eat each other... there seems to be no part of the code which suggests otherwise.















Grid World Part 4 part 1

1
page 30
What methods are implemented in Critter?
The act(), getActors(), processActors(), getMoveLocations(), and selectMoveLocation() methods are implemented in Critter, although it still contains methods from it's superclass Actor.


2
What are the five basic actions common to all critters when they act?  
 The actors are collected from the grid, they all invoke the processActors() method, they get their moves through getMoveLocations(), they select a location through selectMoveLocation(), and finally make a move through makeMove().


3
Should subclasses of Critter override the getActors()   method?
Explain
No, because the getActors() method is used to collect all actors on the grid, and if a subclass of critter overrides it, it might not even get to act because it won't be collected into the list of actors.
 

4
Describe three ways that a critter could process actors. 
  It could change the state of the critter and the actor, it could change the state of the critter based on the actor, or change the actor (i.e. removing it).


5
What three methods must be invoked to make a critter move?
 Explain each of  these methods. 
processActors(), getMoveLocations(), and selectedMoveLocation() must be implemented in order for the critter to move. 


6
Why is there no Critter constructor?
This is because the critter class is an abstract class. Furthermore, it implements methods for its subclasses to use, but can't make any objects.
 
page 33
1
Why does act() cause a ChameleonCritter to act differently from a Critter even though ChameleonCritter does not override act()?
ChameleonCritter acts differently from a Critter even though it doesn't override act() because it has a different implementation of the processActors() method which it does override to make it's color that of its neighbors.


2
Why does the makeMove() method of ChameleonCritter call super.makeMove?
ChameleonCritter calls super.makeMove() because it wants to override the makeMove method but at the same time implement the previous implementation of the method as part of the new method.
 

3
How would you make the ChameleonCritter drop flowers in its old location when it moves?
You would put an implementation of dropping flowers in the beginning of the makeMove() method, so that for each call to the makeMove() method it would drop a flower thereby putting a flower where it was.
 

4
Why doesn’t ChameleonCritter override the getActors() method? 
The ChameleonCritter class doesn't override the getActors() method because it wants to use the same implementation that the critter class used in order to find the actors currently on the grid.


5
Which class contains the getLocation() method? 
Actor contains the getLocation() method.


6
How can a Critter access its own grid?
If by grid, what is meant is location, then the critter can simply call the getLocation() method since it is implemented in the Actor class.

Wednesday, 16 February 2011

Barron's Polymorphism Multiple Choice

How did you do on the multiple choice questions?
I got most of them right, apart from one or two.
Did you understand your errors?
Yes. They were errors in understanding the question, but not conceptual.

What do you not understand?
I couldn't really understand the difference between polymorphism and overriding methods. From what I could understand, it seems overriding a method is simply redefining a method in a subclass, and polymorphism is using the correct overridden method in run-time. Is that precise?

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.