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

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

GridWorld Case Study The Classes A Summary by Jim Mims.
© A+ Computer Science - GridWorld © A+ Computer Science -
Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
Location Class Gridworld. Location Class Encapsulates the coordinates for an actor’s position in a grid – Row and Column number Rows increase going down.
Using a different image than normal Multiple images for a class Controlling the mouse click Controlling the keys pressed Launching an input dialog box.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Arrays An array is a collection of variables, all of the same type. An array is created with the new operator. The size of an array must be specified at.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Multiple Choice Solutions True/False a c b e d   T F.
GridWorld Case Study1 Barbara Ericson Georgia Tech Jan 2008.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
© A+ Computer Science - Row = 0 Column = 0.
How to do well on the AP CS Free Response Questions
© A+ Computer Science - A queue is a group of same-type values. Values are added to the back of a queue and removed from the front.
Data structures Abstract data types Java classes for Data structures and ADTs.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
GridWorld Case Study Barbara Ericson March 24, 2007.
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
© A+ Computer Science - Which of the following statements assigns the letter S to the third row and first column of a two-dimensional.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
Compsci 201 Recitation 10 Professor Peck Jimmy Wei 11/1/2013.
The GridWorld Case Study Program Section 1 - Overview of the Class Hierarchies Section 2 - The Actor Class Section 3 - The Rock and Flower Classes Section.
List Interface and Linked List Mrs. Furman March 25, 2010.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Iteration Abstraction SWE Software Construction Fall 2009.
Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction.
GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Collections Dwight Deugo Nesa Matic
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Lesson 5-4 Example Example 1 Draw an array to model and find 21 ÷ 3. 1.Write the answer if you know it. Otherwise, draw an array.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
Click to edit Master text styles Stacks Data Structure.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
GridWorld.
Topic Dimensional Arrays
Barbara Ericson Georgia Tech Jan 2008
COMPUTER 2430 Object Oriented Programming and Data Structures I
Multiple Choice -answer the easiest question 1st
© A+ Computer Science - GridWorld © A+ Computer Science -
© A+ Computer Science - GridWorld © A+ Computer Science -
A closer look at the world
COMPUTER 2430 Object Oriented Programming and Data Structures I
Collections Framework
See requirements for practice program on next slide.
© A+ Computer Science - GridWorld The GridWorld case study provides a graphical environment where visual objects inhabit and interact.
© A+ Computer Science - GridWorld © A+ Computer Science -
GridWorld Case Study.
© A+ Computer Science - GridWorld © A+ Computer Science -
© A+ Computer Science - GridWorld © A+ Computer Science -
Why not just use Arrays? Java ArrayLists.
Presentation transcript:

© A+ Computer Science -

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.

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

© A+ Computer Science - 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.

© A+ Computer Science - 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.

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

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

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

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.

© A+ Computer Science - 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

© A+ Computer Science - 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}

© A+ Computer Science - 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 {}

© A+ Computer Science - 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

© A+ Computer Science - 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}

© A+ Computer Science -

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;

© A+ Computer Science - 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();

© A+ Computer Science - 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;

© A+ Computer Science - 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();

© A+ Computer Science - 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)); }

© A+ Computer Science - 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; }