Presentation is loading. Please wait.

Presentation is loading. Please wait.

© A+ Computer Science - www.apluscompsci.com. Grid is an interface that details the behaviors expected of a grid. All its methods are abstract methods.

Similar presentations


Presentation on theme: "© A+ Computer Science - www.apluscompsci.com. Grid is an interface that details the behaviors expected of a grid. All its methods are abstract methods."— Presentation transcript:

1 © A+ Computer Science - www.apluscompsci.com

2 Grid is an interface that details the behaviors expected of a grid. All its methods are abstract methods (no code). Grid was designed as an interface because many different structures could be used to store the grid values. An interface works perfectly in this case due to the large number of unknowns.

3 © A+ Computer Science - www.apluscompsci.com A grid can store any type of Object. Grid intGrid; intGrid = new BoundedGrid (5,5); Grid stringGrid; stringGrid = new UnboundedGrid ();

4 © A+ Computer Science - www.apluscompsci.com Grid abstract methods NameUse get(loc)returns the object reference at location loc getEmptyAdjacentLocations(loc)returns an ArrayList of the valid empty locations in 8 directions getNeighbors(loc)returns an ArrayList of the objects around location loc getNumCols()returns the number of columns in this grid getNumRows()returns the number of rows in this grid getOccupiedAdjacentLocations(loc)returns an ArrayList of the valid locations in 8 directions that contain objects getOccupiedLocations()returns an ArrayList of locations in the entire grid that contain objects import info.gridworld.grid.Grid; Note: loc is a reference to a Location object.

5 © A+ Computer Science - www.apluscompsci.com Grid abstract methods (continued) NameUse getValidAdjacentLocations(loc)returns an ArrayList of the valid locations in 8 directions isValid(loc)checks to see if location is valid (returns true/false) put(loc, obj)puts the obj in grid at location loc. Returns the object that was previously in the location. Note: If you want to add Actors to a world, then don’t use grid.put() since it will not update the location of the Actor. Use world.add() instead. remove(loc)takes the object at location loc out of the grid. Returns the object that was removed. Note: Use world.remove() instead of grid.remove() when removing Actors. import info.gridworld.grid.Grid; Note: loc is a reference to a Location object. obj is a reference to an Object.

6 Grid Interface int getNumRows() returns the number of rows, or -1 if this grid is unbounded int getNumCols() returns the number of columns, or -1 if this grid is unbounded Boolean isValid(Location loc) returns true if loc is valid in this grid, false otherwise precondition: loc is not null E put(Location loc, E obj) puts obj at location loc in this grid and returns the object previously at that location (or null if the location was previously empty) precondition: loc is valid in this grid and obj is not null Testable API E stands for Object

7 Grid Interface E remove(Location loc) removes the object at location loc and returns it (or null if the location is unoccupied) precondition: loc is valid in this grid E get(Location loc) returns the object at location loc (or null if the location is unoccupied) precondition: loc is valid in this grid ArrayList getOccupiedLocations() returns all occupied locations in this grid ArrayList getValidAdjacentLocations(Location loc) returns all valid locations adjacent to loc in this grid. The Array list is populated by going clockwise beginning at the adjacent north location precondition: loc is valid in this grid Testable API

8 Grid Interface ArrayList getEmptyAdjacentLocations(Location loc) returns all valid empty locations adjacent to loc in this grid precondition: loc is valid and in this grid ArrayList getOccupiedAdjacentLocations(Location loc) returns all valid occupied locations adjacent to loc in this grid precondition: loc is valid in this grid ArrayList getNeighbors(Location loc) returns all objects in the occupied locations adjacent to loc in this grid precondition: loc is valid in this grid Note: All the Grid methods that look at adjacent locations and return an array list will populate the array list by first looking at the adjacent north location and then going clockwise to retrieve all applicable locations Testable API

9 Exercise Answers Set 4 Assume we have a BoundedGrid reference named grid. 1.How can you get a count of the objects in the grid? – grid.getOccupiedLocations().size() 2.How can you get a count of the empty locations in the grid? – grid.getNumRows() * grid.getNumCols() – grid.getOccupiedLocations.size() 3.How can you check if location (10,10) is in the grid? – if (grid.isValid(new Location(10,10))) 4.Grid contains method declarations but no code. Why? – It is an interface 5. Where can you find the code for the methods? – In the classes that implement the interface: BoundedGrid and UnboundedGrid 6.All methods that can return multiple objects return them in an ArrayList. Would it have been a better design to return them in an array? Explain your answer. – ArrayLists are arrays that can grow or shrink. They are better than arrays when you don’t know how many items you will have.

10 © A+ Computer Science - www.apluscompsci.com BoundedGrid grid; grid = new BoundedGrid (5,5); grid.put(new Location(2,2),4); grid.put(new Location(1,1),11); System.out.println(grid); System.out.println(grid.isValid(new Location(-2,3))); System.out.println(grid.isValid(new Location(2,3))); OUTPUT {(1, 1)=11, (2, 2)=4} false true 11 4

11 © A+ Computer Science - www.apluscompsci.com BoundedGrid grid; grid = new BoundedGrid (5,5); grid.put(new Location(2,2),4); grid.put(new Location(1,1),11); grid.put(new Location(0,2),3); grid.put(new Location(0,1),6); System.out.println(grid); grid.remove(new Location(1,1)); System.out.println(grid); grid.remove(new Location(1,1)); System.out.println(grid); World world = new World(grid); world.show(); OUTPUT {(0, 1)=6, (0, 2)=3, (1, 1)=11, (2, 2)=4} {(0, 1)=6, (0, 2)=3, (2, 2)=4}

12 © A+ Computer Science - www.apluscompsci.com UnboundedGrid grid; grid = new UnboundedGrid (); System.out.println(grid.getNumRows()); System.out.println(grid.getNumCols()); System.out.println(grid); World world = new World(grid); world.show(); OUTPUT {}

13 © A+ Computer Science - www.apluscompsci.com UnboundedGrid grid; grid = new UnboundedGrid (); grid.put(new Location(2,2),4); grid.put(new Location(1,1),11); System.out.println(grid); System.out.println(grid.isValid(new Location(-2,3))); System.out.println(grid.isValid(new Location(2,3))); World world = new World(grid); world.show(); OUTPUT {(1, 1)=11, (2, 2)=4} true 11 4

14 © A+ Computer Science - www.apluscompsci.com UnboundedGrid grid; grid = new UnboundedGrid (); grid.put(new Location(2,2),4); grid.put(new Location(1,1),11); grid.put(new Location(0,2),3); grid.put(new Location(0,1),6); grid.put(new Location(-2,-2),-2); System.out.println(grid); grid.remove(new Location(1,1)); System.out.println(grid); grid.remove(new Location(1,1)); System.out.println(grid); World world = new World(grid); world.show(); OUTPUT {(0, 1)=6, (0, 2)=3, (-2, -2)=-2, (2, 2)=4, (1, 1)=11} {(0, 1)=6, (0, 2)=3, (-2, -2)=-2, (2, 2)=4}

15 © A+ Computer Science - www.apluscompsci.com

16 World frequently used methods NameUse World()creates a new world using 10X10 grid World(grid)creates a new world using grid add(loc, obj)add obj to world at location loc show()makes the world visible import info.gridworld.world.World;

17 © A+ Computer Science - www.apluscompsci.com World world = new World (); world.add(new Location(0,0), new Monster()); world.add(new Location(9,0), new Monster()); world.show(); or Grid grid = new BoundedGrid (10,10); World world = new World(grid); grid.put(new Location(0,0), new Monster()); grid.put(new Location(9,0), new Monster()); world.show();

18 © A+ Computer Science - www.apluscompsci.com World frequently used methods NameUse getGrid()returns a reference to the world’s grid remove(loc)removes the object at location loc locationClicked(loc)activated by a mouse click in the grid. Returns boolean (true). You can control what happens when clicking a cell. import info.gridworld.world.World;

19 © A+ Computer Science - www.apluscompsci.com BoundedGrid grid; grid = new BoundedGrid (5,5); grid.put(new Location(2,2),4); grid.put(new Location(1,1),9); grid.put(new Location(0,4),11); grid.put(new Location(4,3),5); grid.put(new Location(2,0),1); World world; world = new World (grid); world.show();

20 © A+ Computer Science - www.apluscompsci.com World world; world = new World (new BoundedGrid(6,6)); world.show(); Grid grid = world.getGrid(); for(int r=0; r<grid.getNumRows(); r++) { for(int c=0; c<grid.getNumCols(); c++) { grid.put(new Location(r,c), new Monster(r,c,2)); }

21 © A+ Computer Science - www.apluscompsci.com import info.gridworld.world.World; import info.gridworld.grid.Location; public class RowColMultiplyWorld extends World { public boolean locationClicked(Location loc) { add(loc, new RowColMultiplyBug(loc.getCol()*loc.getRow())); System.out.println("Location "+loc+" clicked"); return true; }


Download ppt "© A+ Computer Science - www.apluscompsci.com. Grid is an interface that details the behaviors expected of a grid. All its methods are abstract methods."

Similar presentations


Ads by Google