1 / 20

Agenda

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;

keegan-noel
Download Presentation

Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Agenda • Diagonal bug class • Z Bug class • Location class in GridWorld • Preview topic for next class • homework

  2. Diagonal bug class • Scanner class should not be inside the constructor, how can this be accomplished???

  3. 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; }

  4. public void act() { if (counter <= 3 && steps < sideLength) { if (canMove()) { move(); steps++; } } else if (counter == 1) { setDirection(Location.SOUTHWEST); steps = 0; counter++; }

  5. else if (counter == 2) { setDirection(Location.EAST); steps = 0; counter++; } } }

  6. public class DiagonalBug extends Bug { public DiagonalBug(int loc) { setDirection(loc); } public void turn(){ setDirection(getDirection() + Location.HALF_CIRCLE); } } //where is getDirection()???

  7. 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; } …..

  8. 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

  9. 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

  10. 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; …..

  11. public Location(int r, int c) { row = r; col = c; } public int getRow() { return row; } public int getCol() { return col; }

  12. 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; }

  13. 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

  14. 1 2 3 4 0 0 1 2 3 4 Location loc1 = new Location(?, ?);

  15. 1 2 3 4 0 0 1 2 3 4 Location loc2 = loc1.getAdjacentLocation(Location.WEST); //loc2 has row 2, column 1

  16. 1 2 3 4 0 0 1 2 3 4 Location loc3 = loc2.getAdjacentLocation(Location.NORTHEAST); //loc3 has row 1, column 2

  17. 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.

  18. 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

  19. 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

  20. Homework Download the homework file "Homework12-17" from the link "Course Slides" and finish all the exercises.

More Related