200 likes | 476 Views
Agenda. Diagonal bug class Z Bug class Location class in GridWorld Preview topic for next class homework. Diagonal bug class. Scanner class should not be inside the constructor, how can this be accomplished???. ZBug implementation. public class ZBug extends Bug { private int steps;
E N D
Agenda • Diagonal bug class • Z Bug class • Location class in GridWorld • Preview topic for next class • homework
Diagonal bug class • Scanner class should not be inside the constructor, how can this be accomplished???
ZBug implementation public class ZBug extends Bug { private int steps; private int sideLength; private int counter; public ZBug(int length) { steps = 0; sideLength = length; counter = 1; }
public void act() { if (counter <= 3 && steps < sideLength) { if (canMove()) { move(); steps++; } } else if (counter == 1) { setDirection(Location.SOUTHWEST); steps = 0; counter++; }
else if (counter == 2) { setDirection(Location.EAST); steps = 0; counter++; } } }
public class DiagonalBug extends Bug { public DiagonalBug(int loc) { setDirection(loc); } public void turn(){ setDirection(getDirection() + Location.HALF_CIRCLE); } } //where is getDirection()???
public class Actor { private Grid<Actor> grid; private Location location; private int direction; private Color color; public Actor(){ color = Color.BLUE; direction = Location.NORTH; grid = null; location = null; } …..
Gets the current direction of this actor. @return the direction of this actor, an angle between 0 and 359 degrees public int getDirection() { return direction; } Actor class
Sets the current direction of this actor. @param newDirection the new direction. The direction of this actor is set to the angle between 0 and 359 degrees that is quivalent to <code>newDirection</code>. public void setDirection(int newDirection){ direction = newDirection % Location.FULL_CIRCLE; if (direction < 0) direction += Location.FULL_CIRCLE; } Actor class
Location Class public class Location implements Comparable { private int row; // row location in grid private int col; // column location in grid public static final int LEFT = -90; public static final int RIGHT = 90; public static final int HALF_LEFT = -45; ….. public static final int NORTH = 0; …..
public Location(int r, int c) { row = r; col = c; } public int getRow() { return row; } public int getCol() { return col; }
public int compareTo(Object other){ Location otherLoc = (Location) other; if (getRow() < otherLoc.getRow()) return -1; if (getRow() > otherLoc.getRow()) return 1; if (getCol() < otherLoc.getCol()) return -1; if (getCol() > otherLoc.getCol()) return 1; return 0; }
Location class • Methods getAdjacentLocation(int direction) based on the parameter direction and get the new location(row, col) for the current location getDirectionToward(Location loc); Based on the parameter location(row, col), get the direction angle for the current loaction
1 2 3 4 0 0 1 2 3 4 Location loc1 = new Location(?, ?);
1 2 3 4 0 0 1 2 3 4 Location loc2 = loc1.getAdjacentLocation(Location.WEST); //loc2 has row 2, column 1
1 2 3 4 0 0 1 2 3 4 Location loc3 = loc2.getAdjacentLocation(Location.NORTHEAST); //loc3 has row 1, column 2
toString() method • Always return a String representation of the object defined by you. • toString() method is defined in the superclass Object. • Every Object can call this toString(), but if it is not overridden(defined) in your own class, the default one in Object will be used which may not be meaningful.
Practice on the board Class: Student Variables: lastName, firstName, grades Constructor with 2 parameters to initialize the state variables lastName and firstName; set grades to 0 methods: getName- return the full name(lastName + firstName) setGrade- take a parameter and set the grades
Preview topics I. Generic classes/ interfaces ArrayList<String> aryLst= new ArrayList<String>(); Materials • BPJ book – 38-5, 43-1, 47-2, Appendix AF • http://www.tutorialspoint.com/java/java_generics.htm • http://docs.oracle.com/javase/tutorial/extra/generics/index.html II. The Grid Interface – GridWorld student manual P20-21
Homework Download the homework file "Homework12-17" from the link "Course Slides" and finish all the exercises.