Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda About Quiz ChameleonCritter class CrabCritter class homework.

Similar presentations


Presentation on theme: "Agenda About Quiz ChameleonCritter class CrabCritter class homework."— Presentation transcript:

1 Agenda About Quiz ChameleonCritter class CrabCritter class homework

2 Quiz State variables of subclass Static vs static final
Accessor vs mutator methods An accessor method is used to return the value of a private field A mutator method is used to set a value of a private field Accessor methods in Location class getCol, getRow, getAdjacentLocation, toString

3 Mutator methods in World class
add(..), remove(..), addGridClass(..), addOccupantCass(..) GridWorld : programming (j) : display old message + new new message aWorld.setMessage( aWorld.getMessage + “ “ aBug.getLocation.toString()); (o) Remove a bug aworld.remove(aBug.getLocation()); (Y) get adjacent empty location of critter and stores one to actorLoc. ArrayList<Location> locArry= aGrid.getEmptyAdjacentLocations(aCritter.getLocation() ; Location actorLoc = locArry.get(0);

4 Critter’s act() method
public void act() { if (getGrid() == null) return; ArrayList<Actor> actors = getActors(); processActors(actors); ArrayList<Location> moveLocs = getMoveLocations(); Location loc = selectMoveLocation(moveLocs); makeMove(loc); }

5 ChameleonCritter class
The ChameleonCritter class gets the same neighboring actors just like a Critter, but it doesn’t process actors by eating them. Instead, when a ChameleonCritter processes actors, it randomly selects one and changes its own color to the color of the selected actor.

6 ChameleonCritter class
What is its super class? State variable(s)? Constructor(s)? Method(s)? Overriding method(s)? What is the difference between ChameleonCritter and Critter?

7 Critter class public void processActors(ArrayList<Actor> actors) { for (Actor a : actors) if (!(a instanceof Rock) && !(a instanceof Critter)) a.removeSelfFromGrid(); }

8 ChameleonCritter class
public void processActors(ArrayList<Actor> actors) { int n = actors.size(); if (n == 0) return; int r = (int) (Math.random() * n); Actor other = actors.get(r); setColor(other.getColor()); }

9 Critter class public void makeMove(Location loc) { if (loc == null) removeSelfFromGrid(); else moveTo(loc); }

10 ChameleonCritter class
public void makeMove(Location loc) { setDirection( getLocation().getDirectionToward(loc)); super.makeMove(loc); }

11 CrabCritter class A CrabCritter is a critter that eats whatever is found in the locations immediately in front, to the right-front, or to the left-front of it. It will not eat a rock or another critter(inherited from the Critter class). A CrabCritter can move only to the right or to the left. If both locations are empty it randomly selects one. If a CrabCritter cannot move, then it turns 90 degrees, randomly to the left or right.

12 CrabCritter class What is its super class? State variable(s)?
Constructor(s)? Method(s)? Overriding method(s)? Why does CrabCritter need to override those methods in its parent class?

13 Homework Please download the homework file “Homework – Jan 7” from course slides and finish it by Thursday. It has to be hand written.

14 Example for 2nd part Actor Bug State variables:
grid, location, direction, color Constructor(s): Actor() Method(s): getColor(), setColor(), getDirection(), setDirection(), getGrid()….. extends Bug State variables: grid, location, direction, color Constructor(s): Bug(); Bug(color..) Method(s): ……….


Download ppt "Agenda About Quiz ChameleonCritter class CrabCritter class homework."

Similar presentations


Ads by Google