יום שני, 28 בפברואר 2011

Monday 28/02/11 Some Standard Classes

After reading the assigned pages a lot of syntactical problems were cleared up. It was all review but it helped me noticeably. The questions were not difficult and I got most of them right however the ones I got wrong I am not sure where my mistakes are and I am hoping to clear these up in class tomorrow.

The Coding bat problems were not easy. I have the logic down for the three but for the two of them I am having unsolvable syntactical problems which I also hope to clear up in class tomorrow.

The implementation of caterpillar is something that I am not sure even where to start.

The GridWorld project assigned is an interesting one. I can't say I fully understand it at this stage but I am sure I will as time goes by.

יום שני, 14 בפברואר 2011

Gridworld: Part 3: Classes & Interfaces: Set 6: Page 25-26



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

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

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

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

5
Which methods inherited from the Actor class 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?  

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

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

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

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

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

Gridworld: Part 3: Classes & Interfaces: Set 5: Page 23



1
What methods are implemented inCritter?  

2
What are the five basic actions common to all critters when they act?  

3
Should subclasses of Critter override thegetActors()   method?

Explain

4
Describe three ways that a critter could process actors.  

5
What three methods must be invoked to make a critter move?

Explain each of  these methods.  

6
Why is there no Critter constructor?  

Gridworld: Part 3: Classes & Interfaces: Set 4: Page 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?

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

3. Grid contains method declarations, but no code is supplied in the methods.

Why?

Where can you find the 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.  



Gridworld: Part 3: Classes & Interfaces: Set 3: Page 19



1.

Location loc1 = new Location(4, 3);
Location loc2 = new Location(3, 4);

How would you access the row value for loc1?  

2.
Location loc1 = new Location(4, 3);
Location loc2 = new Location(3, 4);

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

boolean b = loc1.equals(loc2);


3.
Location loc1 = new Location(4, 3);
Location loc2 = new Location(3, 4);

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

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

4.
Location loc1 = new Location(4, 3);
Location loc2 = new Location(3, 4);


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

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

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



Gridworld: Part 2: Exercises: Page 13-15



1. Write a class CircleBug  that is identical to BoxBug,  except that in the act()  method the turn() method is called once instead of twice. How is its behavior different from aBoxBug?  
public void act()
   {
         if (steps<sideLength && can Move())
         {
               move();
               steps++;
         }
          else
         {
                turn();
                steps=0;
          }

It turns 45 degrees instead of 90 degrees obviously and therefore creates and octagon.
2. Write a class SpiralBug  that drops flowers in a spiral pattern.

Hint: Imitate BoxBug,  but adjust the side length when the bug turns. You may want to change the world to an UnboundedGrid  to see the spiral pattern more clearly.

    public void act()
    {
        if (steps < sideLength && canMove())
        {
            move();
            steps++;
        }
        else
        {
            turn();
            turn();
            steps = 0;
            sideLength++;
        }
    }

Because it increases every time by the same increment it will never hit its original path and will therefore spiral.
3. Write a class ZBug  to implement bugs that move in a “Z” pattern, starting in the top left corner. After completing one “Z” pattern, a ZBug  should stop moving.

In any step, if a ZBug  can’t move and is still attempting to complete its “Z” pattern, the ZBug  does not move and should not turn to start a new side. Supply the length of the “Z” as a parameter in the constructor. The following image shows a “Z” pattern of length 4.

Hint: Notice that a ZBug  needs to be facing east before beginning its “Z” pattern.  

   public void act()
    {
        if (steps < sideLength && canMove())
        {
            move();
            steps++;
        }
        else
        {
            if (east)
                {
                    turn();
                    turn();
                    turn();
                    steps = 0;
                    east = false;
                }
            else
                {
                    turn();
                    turn();
                    turn();
                    turn();
                    turn();
                    steps = 0;
                    east = true;
                }
        }
    }

Took a little big of trial an error. PS in the given I wrote that it is already facing east with a boolean.
4. Write a class DancingBug  that “dances” by making different turns before each move. The DancingBug  constructor has an integer array as parameter. The integer entries in the array represent how many times the bug turns before it moves.

For example, an array entry of 5 represents a turn of 225 degrees (recall one turn is 45 degrees). When a dancing bug acts, it should turn the number of times given by the current array entry, then act like a Bug.  In the next move, it should use the next entry in the array. After carrying out the last turn in the array, it should start again with the initial array value so that the dancing bug continually repeats the same turning pattern.

The DancingBugRunner  class should create an array and pass it as a parameter to the DancingBug  constructor.
wow.
5. Study the code for the BoxBugRunner  class. Summarize the steps you would use to add another BoxBug  actor to the grid.  
Somehow create a new bug by clicking an empty box in the grid.


Gridworld: Part 2: Bug Variations: Set 2: Page 12


1. What is the role of the instance variablesideLength?
Number of moves until the bug turns
2. What is the role of the instance variablesteps?  
Counts amount of steps.
3. Why is the turn() method called twice when steps becomes equal to sideLength?  
One call turns it 45 degrees, we want it to turn 90 degrees.
4. Why can the move() method be called in the BoxBug class when there is no move() method in the BoxBug code?  
It is defined in the Bug class and inherits it.
5. After a BoxBug is constructed, will the size of its square pattern always be the same? Why or why not?
Nope, there is no way to change it's size after it's constructed.
6. Can the path a BoxBug travels ever change? Why or why not?  
If it reaches the edge or if it comes to face something.
7. When will the value of steps be zero?  
When the bug turns.