Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Do you know 2 1.

The sideLength instance variable defines the number of steps a BoxBug moves on each side of its box. 2. The steps instance variable keeps track of how many steps a BoxBug has moved on the current side of its box. 3. The turn method only executes a 45 degree turn; therefore it takes two turn method calls to turn 90 degrees. 4. The BoxBug class extends the Bug class, and the Bug class has a public move method. Since the BoxBug class is a subclass of the Bug class, it inherits the move method from the Bug class. 5. Yes. When a BoxBug is constructed, the side length is determined and cannot be changed by client code. Do You know 3 1. loc1.getRow() 2. False 3. 4,4 4. 135-Southeast 5. One of the parameters in the method indicate the direction to look for a neighbor, and it will return a neighbor closest to that direction. Do You Know 4 1. grid.getOccupiedLocations().size() for occupied; grid.getNumRows()*grid.getNumCols() - grid.getOccupiedLocations().size() for empty 2. If grid.isValid(new Location(10,10)) is true, then it exsists 3. Grid is an interface in this case. Classes such as AbstractGrid,BoundedGrid, and UnboundedGrid use the methods defined in the Grid interface.

4. The main reason an arraylist is preferred is because arraylists have no parameters, so it is easy to add elements or remove them, while in arrays, a lot more work would have to be done to get how many bugs there are and create an array of that size, but then one could not add more bugs. Also it is easier to access elements Do You Know 5

1. Color, direction, and location 2. The initial color is blue and it is facing north 3. Because the Actor class has instance variables and specific methods which cant be defined in interfaces. 4. A. No it can not put itself twice B. No it can not remove itself twice C. Yes the actor can place itself in a grid, then remove itself, then add it again. 5. setDirection(getDirection() + 90); Do you know 6

1. if(!gr.isValid(next)) return false; 2. Actor neighbor = gr.get(next); return (neighbor == null) || (neighbor instanceof Flower); 3. isValid and get are used because the square needs to be checked if it exsists and a bug can be placed there 4. getAdjacentLocation is used so the bug can find its next possible location. 5. getLocation, getDirection, getGrid 6. The bug removes itself from the grid lol 7. Loc is needed so that a flower can be inserted in the bugs original location 8. It can be seen which bug dropped which flower which is good for keeping track and such 9. Yes as long the bug move method is calling removeSelfFromGrid 10. Flower flower = new Flower(getColor()); flower.putSelfInGrid(gr, loc); 11. 4 turns Group Activity 1a. It would turn b. It would turn c. It would turn d. remove the actor from the grid e. remove the jumper from the grid f. Can a Jumper jump over only rocks and flowers? 2a. Actor class

b. The bug class is similar c. There does not have to be unless one wants to change the color of the jumper. d. act should be overridden e. canJump should be added f. We should place the jumper in every scenario e.g. in front of the edge of a grid, in front of a rock, near an actor,etc 3. import info.gridworld.actor.Actor; import info.gridworld.actor.Flower; import info.gridworld.actor.Rock; import info.gridworld.grid.Grid; import info.gridworld.grid.Location; import java.awt.Color; public class Jumper extends Actor { public Jumper() { setColor(Color.PINK); } /** * Constructs a Jumper of a given color. * @param JumperColor the color for this Jumper */ public Jumper(Color JumperColor) { setColor(JumperColor); }

public void act() {if (canJump()) jump(); else turn();} public void turn() { setDirection(getDirection() + Location.HALF_RIGHT); } public void jump() {Grid<Actor> gr = getGrid(); if (gr == null) return; Location loc = getLocation(); Location next = loc.getAdjacentLocation(getDirection()); Location twoAway = next.getAdjacentLocation(getDirection()); if (gr.isValid(twoAway)) moveTo(twoAway); else removeSelfFromGrid();} public boolean canJump() { Grid<Actor> gr = getGrid(); if (gr == null) return false; Location loc = getLocation();

Location next = loc.getAdjacentLocation(getDirection()); if (!gr.isValid(next)) return false; Actor neighbor = gr.get(next); if (!((neighbor == null) || (neighbor instanceof Flower) || (neighbor instanceof Rock))) return false; Location twoAway = next.getAdjacentLocation(getDirection()); if (!gr.isValid(twoAway)) return false; neighbor = gr.get(twoAway); return (neighbor == null) || (neighbor instanceof Flower); }} Jumper Runner Class import info.gridworld.actor.ActorWorld; import info.gridworld.actor.Bug; import info.gridworld.actor.Rock; import info.gridworld.actor.Flower; public class JumperRunner { public static void main(String[] args) { ActorWorld world = new ActorWorld(); world.add(new Jumper()); world.add(new Rock()); world.add(new Bug());

world.add(new Flower()); world.show(); }} 4. Did that checked it it works .

You might also like