1 | What methods are implemented inCritter? | The following methods are implemented: - act() - getActors() - getMoveLocation() - electMoveLocation() - processActors() - makeMove() |
2 | What are the five basic actions common to all critters when they act? | They get actors and they process the actor, they get move locations, they choose a block to move to and finally they move. |
3 | Should subclasses of Critter override thegetActors() method? Explain | They can have it, if they get actor, and want to change location or change the colour. |
4 | Describe three ways that a critter could process actors. | Rearrange the placements of the objects inside the grid, removing all rocks, changing colour of the flowers |
5 | What three methods must be invoked to make a critter move? Explain each of these methods. | Three methods are: - getMoveLocations() - selectMoveLocation() - makeMove() 1. getMoveLocations() gives us a list of all the moving possible locations on the grid 2. selectMoveLocation() selects a location on the list of locations 3. makeMove then moves object to the chosen spot. |
6 | Why is there no Critter constructor? | It extends colour. |
Sunday, 27 February 2011
GridWorld page 30
Grid World Page 25 and 26
1 | Which statement(s) in the canMove() method ensures that a bug does not try to move out of its grid? | If (!gr.isValid(next)) return false; |
2 | Which statement(s) in the canMove()method determines that a bug will not walk into a rock? | return (neighbor == null) || (neighbor instanceOf Flower); |
3 | Which methods of the Grid interface are invoked by the canMove() method and why? | Not sure. |
4 | Which method of the Location class is invoked by the canMove() method and why? | getAdjacentLocation() its checks if the other blocks in the grid are valid so the object can move into the empty space. |
5 | Which methods inherited from the Actor class are invoked in the canMove()method? | getDirection getLocation get Grid |
6 | What happens in the move() method when the location immediately in front of the bug is out of the grid? | The bug is automatically deleted from the grid. |
7 | Is the variable loc needed in the move()method, or could it be avoided by callinggetLocation() multiple times? | Getlocation method |
8 | Why do you think the flowers that are dropped by a bug have the same color as the bug? | Because they inherit the colour class from the bug |
9 | When a bug removes itself from the grid, will it place a flower into its previous location? | Yes it will |
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()); |
11 | If a bug needs to turn 180 degrees, how many times should it call the turn()method? | four |
GridWorld Page 23
1 | Name three properties of every actor. | The three fields are color, location and direction. |
2 | When an actor is constructed, what is its direction and color? | The colour is blue and direction is set to |
3 | Why do you think that the Actor class was created as a class instead of an interface? | |
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? | 3. Firstly because some methods are implemented. 4. It will give you an error if you try to put it in the grid twice. We are allowed to replace, that means placing something completely new instead of the old one, but you cannot put it in twice. |
5 | How can an actor turn 90 degrees to the right? | by using the setdirection method. |
Sunday, 13 February 2011
Inheritance & Polymorphism
Over the weekend, we were supposed to do 28 multiple choices from the Barrons Book. Inheritance was very simple for me, its come to me like second nature because it was basically a review but I got stuck on Polymorphism. Unfortunately I got only 19 questions of these right and this time I did every single multiple choice question by myself. I did not get nay help on understanding and I did not want to slack of because I really believe that I need to apply myself more properly in this class, so I will be more focused in class this week and I will be on top of my coding. Apart from that, I did try to understand and solve the questions that I got wrong but Polymorphism is a concept which will take some time for me to understand like recursion did at the starting. Over the weekend, I also took the advise from Mr.Daly and I did like 15 JavaBat questions all from different sections like I did some from Logic 2 and I did a few Recursion problems and some String-2 as well. Just so that I get better at the coding because I feel that my weakness is defiantly in the free response but I need to build up on it rather than slack it till later. I usually do well on multiple choice right. Also I tried to go over the Sudoku coding that we did a while back, and I tried to code it myself. I saw that there was a lot of problem and confusion so I will be working on that throughout next week. Apart from this, I need to work on the grid world as well, which I will hopefully get done and post it tonight.
Sunday, 6 February 2011
Grid World 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? | Done it. I had a little difficulty but I got some help. I made two new classes, CircleBugEat and CircleBug. The CircleBugEat produces the grid and puts the CircleBug in it. The CircleBug only extends the Bug class and overrides its act method, that will bring change its behavior. |
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. | Not easy. I had to do is change the act() method of the CircleBug create the SpiralBug. |
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. | Question to ask. I did not understand how to work with this. Will get some Help asap. |
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. | Done. I tried to create this but I understand that I had to work with the act()method, and bring alterations but that was not working for me so I got stuck. |
5. Study the code for the BoxBugRunner class. Summarize the steps you would use to add another BoxBug actor to the grid. | Firstly we need to initialize the new BoxBug, and the another step is to put it inside the grid. |
Grid World Page 12
1. What is the role of the instance variable sideLength? | sideLength calculates how many steps BoxBug travels before it turns 90 degrees right.(clockwise) |
2. What is the role of the instance variable steps? | Like the name of the variable indicates, it counts the amount of steps the BoxBug has done since last turn.Every turn, the variable resets, or goes back to 0 and counts until the next turn. |
3. Why is the turn() method called twice when steps becomes equal to sideLength? | A single turn() method would turn it by 45 degrees.So twice calling it will make it do a 90 degree turn. |
4. Why can the move() method be called in the BoxBug class when there is no move() method in the BoxBug code? | All the methods from the BoxBug class extend to the Bug class so the move() method should also be available in the Bug class. |
5. After a BoxBug is constructed, will the size of its square pattern always be the same? Why or why not? | No the size of the square pattern will never change.There is not method to change the size of the square pattern so it will remain the same as default. |
6. Can the path a BoxBug travels ever change? Why or why not? | Yes the path will always change. Even if the amount of steps is not sufficient, the bug will change its direction at every single corner or touching the boundary. |
7. When will the value of steps be zero? | It changes to zero when it has reached the right amount of steps or it meets a corner of the grid. |
Grid World Page 6 + Page 8
1. Does the bug always move to a new location? Explain. | If there is rock infront or bug reaches corner/boundary of the grind, it turn clockwise. |
2. In which direction does the bug move? | The bug will always move straight by default but with every turn or corner the direction of straight changes. |
3. What does the bug do if it does not move? | Clockwise |
4. What does a bug leave behind when it moves? | A flower. |
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.) | Clockwise, when bug hits the edge. |
6. What happens when a bug has a rock in the location immediately in front of it? | Every edge the bug turns clockwise, the rock is therefore also treated like an edge and it turn clockwise. |
7. Does a flower move? | Nope |
8. What behavior does a flower have? | Flower slowly fades to black. |
9. Does a rock move or have any other behavior? | Rock is always stationary. |
10. Can more than one actor (bug, flower, rock) be in the same location in the grid at the same time? | Nope, only one object at a time. |
PAGE 8 1. Test the setDirection() method with the following inputs and complete the table, giving the compass direction each input represents. |
| ||||||||||||||||||||
2. Move a bug to a different location using the moveTo() 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? | The moveTo() takes the co-ordinate of the bug into consideration, which is the row and the column where the bug is.We can place the bug anywhere in the grid, according to the coordinates. If we try to place it outside the grid, we will get an error. | ||||||||||||||||||||
3. Change the color of a bug, a flower, and a rock. Which method did you use? | setColor() is a method of the superclass. It is implemented into all the classes and can be used to change the colour of all the objects inside the grid. So the stone, the bug the flower, their colours can be changed using this method. | ||||||||||||||||||||
4. Move a rock on top of a bug and then move the rock again. What happened to the bug? | There is no more bug. This is because their is only space for 1 bug to fit into each space of the grid. When we try to place a bug on top of a place of an existing bug, the previous bug will be deleted and the new bug will take its place. |
Subscribe to:
Posts (Atom)