© A+ Computer Science - . Grid is an interface that details the behaviors expected of a grid. All...

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

Transcript of © A+ Computer Science - . Grid is an interface that details the behaviors expected of a grid. All...

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

© A+ Computer Science - www.apluscompsci.com

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

© A+ Computer Science - www.apluscompsci.com

Grid is an interface that details thebehaviors 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.

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

© A+ Computer Science - www.apluscompsci.com

A grid can store any type of Object.

Grid<Integer> intGrid;intGrid = new BoundedGrid<Integer>(5,5);

Grid<String> stringGrid;stringGrid = new UnboundedGrid<String>();

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

© A+ Computer Science - www.apluscompsci.com

Gridabstract methods

Name Useget(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.

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

© A+ Computer Science - www.apluscompsci.com

Gridabstract methods (continued)Name Use

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.

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

Grid Interfaceint 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 otherwiseprecondition: 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

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

Grid InterfaceE 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<Location> getOccupiedLocations()returns all occupied locations in this grid

ArrayList<Location> 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

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

Grid InterfaceArrayList<Location> getEmptyAdjacentLocations(Location

loc)returns all valid empty locations adjacent to loc in this gridprecondition: loc is valid and in this grid

ArrayList<Location> getOccupiedAdjacentLocations(Location loc)returns all valid occupied locations adjacent to loc in this grid

precondition: loc is valid in this grid

ArrayList<E> getNeighbors(Location loc)returns all objects in the occupied locations adjacent to loc in this gridprecondition: 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

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

Exercise Answers Set 4Assume 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.

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

© A+ Computer Science - www.apluscompsci.com

BoundedGrid<Integer> grid;grid = new BoundedGrid<Integer>(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}falsetrue

11

4

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

© A+ Computer Science - www.apluscompsci.com

BoundedGrid<Integer> grid;grid = new BoundedGrid<Integer>(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}{(0, 1)=6, (0, 2)=3, (2, 2)=4}

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

© A+ Computer Science - www.apluscompsci.com

UnboundedGrid<Integer> grid;grid = new UnboundedGrid<Integer>();System.out.println(grid.getNumRows());System.out.println(grid.getNumCols());System.out.println(grid);World world = new World(grid);world.show();

OUTPUT-1-1{}

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

© A+ Computer Science - www.apluscompsci.com

UnboundedGrid<Integer> grid;grid = new UnboundedGrid<Integer>();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}truetrue

11

4

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

© A+ Computer Science - www.apluscompsci.com

UnboundedGrid<Integer> grid;grid = new UnboundedGrid<Integer>();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}{(0, 1)=6, (0, 2)=3, (-2, -2)=-2, (2, 2)=4}

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

© A+ Computer Science - www.apluscompsci.com

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

© A+ Computer Science - www.apluscompsci.com

Worldfrequently used methods

Name Use

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;

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

© A+ Computer Science - www.apluscompsci.com

World<Monster> world = new World<Monster>();world.add(new Location(0,0), new Monster());world.add(new Location(9,0), new Monster()); world.show(); orGrid<Monster> grid = new BoundedGrid<Monster>(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();

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

© A+ Computer Science - www.apluscompsci.com

Worldfrequently used methods

Name Use

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;

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

© A+ Computer Science - www.apluscompsci.com

BoundedGrid<Integer> grid;grid = new BoundedGrid<Integer>(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<Integer> world;world = new World<Integer>(grid);world.show();

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

© A+ Computer Science - www.apluscompsci.com

World<Monster> world;world = new World<Monster>(new BoundedGrid(6,6));world.show(); Grid<Monster> 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)); }}

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

© 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;

}}