Monday, 14 February 2011

GRIDWORLD PART II

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




1. What is the role of the instance variablesideLength?
The number input will determine the number of moves the class makes until the bug turns. 
2. What is the role of the instance variablesteps?  
count the bug steps by incrementing it every move 
3. Why is the turn() method called twice when steps becomes equal to sideLength?  
because one turn method call only turns the bug halfway and in order to make it turn completley (90 degrees) we must call it twice. 
4. Why can the move() method be called in the BoxBug class when there is no move() method in the BoxBug code?  
Since it inherits the methods from its super class which contain this method. 
5. After a BoxBug is constructed, will the size of its square pattern always be the same? Why or why not?
yes, because there is no method that changes the size of the squirrel, therefore the size will remain the same. 
6. Can the path a BoxBug travels ever change? Why or why not?  
Yes it can change,if the bug is obstructed by either a rock or the edge of the grid it will turn and change its path 
7. When will the value of steps be zero?  
I noticed it is zero right when the bug makes a turn, but there could me more options I am not sure. 


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 && canMove())
        {
            move();
            steps++;
        }
        else
        {
            turn();
            turn();
            steps = 0;
        }


it doesnt make 90 degree turns, but rather 45 degree turns. As a result it creates an 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++;
        }
    }
I incremented the sidelength variable in order to form the spiral, it must increase every move to make sure that the bug does not hit its own trail of flowers and ruin flowers.
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;
I altered the act method in order to make sure the bug makes the correct number of moves to form the perfect Z. im awesome
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.
?
5. Study the code for the BoxBugRunner  class. Summarize the steps you would use to add another BoxBug  actor to the grid.  
You would create a method that works by clicking on an empty cell
The method will consruct a bug and its parameters will be the sidelength variable

Monday, 7 February 2011

Priority Queue Exercise

This is my Add method:


 public void add (Message msg)
        {
            messageQueues.get(msg.getPriority()).add(msg);
          
            total++;

        }

This is my remove method:


public Message remove()
        {
      
          Message temp = null;
                        for (int i = 9; i>-1; i--)
                       {
                          if (messageQueues.get(i).size()!=0)
                                   {
                                      temp = messageQueues.get(i).get(0);
                                            messageQueues.get(i).remove(0);
                                               return temp;
                                            
                                            }
                                            total--;
                                    }
                   return temp;
                    }
          
            }

Sunday, 6 February 2011

Gridworld Part 1

Gridworld: Set 1 page 6



1. Does the bug always move to a new location? Explain.
Not always, it sometimes turns or moves in a circle. 
2. In which direction does the bug move?
Straight until something obstructs it which in that case it turns until it finds a free cell.
3. What does the bug do if it does not move?
turns 
4. What does a bug leave behind when it moves?
Flowers  
5. What happens when the bug is at an edge of the grid? (Consider whether the bug
is facing the edge as well as whether the bug is facing some other direction when
answering this question.)
It turns clockwise and continues its route 
6. What happens when a bug has a rock in the location immediately in front of it?
It turns and moves to a different cell which is free 
7. Does a flower move?
No, they just change colors 
8. What behavior does a flower have?
It changes colors(into a darker color and eventually to black) 
9. Does a rock move or have any other behavior?
Nope 
10. Can more than one actor (bug, flower, rock) be in the same location in the grid at
the same time?
Nope 

Gridworld: Part1: Exercises page 8




1. Test the setDirection()   method with the following inputs and complete the table, giving the compass direction each input represents.



degrees
compass direction
0
North
45
 North East
90
 East
135
 South East
180
 South
225
 South West
270
 West
315
 North West
360
 North

2. Move a bug to a different location using themoveTo()   method.

In which directions can you move it?

How far can you move it?

What happens if you try to move the bug outside the grid?

3. Change the color of a bug, a flower, and a rock.

Which method did you use?
setColor method 
4. Move a rock on top of a bug and then move the rock again.

What happened to the bug?
The bug is gone. The rock probably squished it.. 

**********************************************************************************
MR DALY,
I DID THESE EXERCISES IN CLASS WITH AMIT ON HIS COMPUTER SINCE I DID NOT HAVE MINE AT THE TIME. HOWEVER, I AM NOT ABLE OF MAKING GRIDWORLD FUNCTION PROPERLY ON MY LAPTOP WHICH IS WHY I CANNOT COMPLETE PART 2
PLEASE TAKE THIS INTO CONSIDERATION AND HOPEFULLY WE WILL FIX THE PROBLEM TOMORROW IN CLASS
THANKS,


JONATHAN
**********************************************************************************

Monday, 31 January 2011

Introductory Java Language Features:

By Monday (31) 8:00pm in your blog

How did you do on the multiple choice questions?
I did quite well, got about 80% of them correct

Did you understand your errors?
I understood most of my errors but I still have some question I would like to go over in class

What do you not understand?

I did not understand anything regarding the Hexadecimal and binary section, The book only gives a few lines of informations and explanations on this subject and I was unable to understand it whatsoever. I even tried reading the explanations to the multiple choice questions about hexadecimals but they were useless as well. Hopefully we will go over this subject in class and clarify this issue. 

Can you convert between hexadecimal and decimal (and binary)?

Not at all, see answer to previous question.

Sunday, 23 January 2011

Sorting and Searching

How did you do on the multiple choice questions?

I did the multiple choice question with Sungho, which was extremley helpful and as a result I did very well on the questions and got almost all of them correct.

Did you understand your errors?

I understood all of my errors thanks to the book explanations and Sungho's explanations. 

What do you not understand?

I am not completely sure about how binary search works

To what extent did you succeed in implementing the sorting and searching algorithm exercises?

A high extent, me and Amit Maor were succesful in implementing almost all of the algorithms in the array exercise that was given. 

Sunday, 9 January 2011

Recursion and what not..

How did you do on the multiple choice questions?

I got 11 question right and 10 wrong

Did you understand your errors?

I understood most of them, although the explanations in the book aren't helpful enough for me to truly understand my mistake in order to avoid it in the future.
I have written down the question which i did not understand even after reading the explanations in the book and I will go over them during class.
Did you use run-time stacks to understand the progress of the recursion?

Only when I could not figure out the answer right away, I used the stacks in order to calculate the output of the recursive method.

How many codingBat recursion exercises have you done? Have the exercises helped you to understand recursion?

I have done 9 codingBat recursion exercises. The exercises have helped me a lot since i was able to get the idea of basic coding and format of simple recursion methods and qeustions.
What do you not understand?

I am not sure what I do not understand, but I know the type of questions I keep getting wrong and I am planning on going over them with you and my peers so I can truly understand.