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

Slides:



Advertisements
Similar presentations
GridWorld Case Study The Classes A Summary by Jim Mims.
Advertisements

GridWorld Case Study Part 1 Experiments to Observe Attributes and Behavior of Actors A Summary by Jim Mims.
GridWorld Case Study Part 3 GridWorld Classes and Interfaces A Summary by Jim Mims.
GridWorld Case Study Part 2 Bug Variations 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?
Critters A Study in Design Patterns. Design Patterns  Not specific algorithms or data structures  A general reusable solution to a common problem.
Place captured red pieces here Place captured yellow pieces here To use as Kings Rules New Game Exit Left mouse click on piece – drag to desired location.
Location Class Gridworld. Location Class Encapsulates the coordinates for an actor’s position in a grid – Row and Column number Rows increase going down.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Using a different image than normal Multiple images for a class Controlling the mouse click Controlling the keys pressed Launching an input dialog box.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
GridWorld Case Study1 Barbara Ericson Georgia Tech Jan 2008.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
© A+ Computer Science - Row = 0 Column = 0.
How to do well on the AP CS Free Response Questions
Announcements This Wednesday, Class and Labs are cancelled! The last lab is due this Wednesday … how many people are planning on doing it? Finally posted.
Lecture 4. Greenfoot 1 Pablo Romero, Department of Informatics Greenfoot Programming simulations and games in Java.
GridWorld Case Study Barbara Ericson March 24, 2007.
© A+ Computer Science - Grid is an interface that details the behaviors expected of a grid. All its methods are abstract methods.
CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
Excel Spreadsheet Notes. What is a Spreadsheet? Columns and rows of data.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
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.
Building Java Programs Chapter 8 Lecture 8-3: Object state; Homework 8 (Critters) reading:
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
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.
© A+ Computer Science - Visit my site at Full Curriculum Solutions M/C Review Question Banks Live Programming.
Data Structures Arrays and Lists Part 2 More List Operations.
Improving the Crab Mrs. C. Furman August 19, 2010.
© 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.
CONDITIONAL FORMATTING AND CUSTOM NUMBER FORMATS LEC 5 1.
© 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.
Interface, Subclass, and Abstract Class Review
Karel J Robot.
Sit-In Lab 1 Ob-CHESS-ion
Agenda About Quiz ChameleonCritter class CrabCritter class homework.
Barbara Ericson Georgia Tech Jan 2008
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
AP "Case Studies" A big program for AP students to understand
Creating Games with Greenfoot
Object Oriented Programming
Pointers and Linked Lists
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
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Agenda About Homework for BPJ lesson 36 About practice of last class
Lecture 13: Two-Dimensional Arrays
Unit 1 Lab14 & Lab15.
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.
GridWorld Case Study.
More on Creating Classes part 3
© A+ Computer Science - GridWorld © A+ Computer Science -
Agenda Dancing bug class Class vs object Comparable interface
© A+ Computer Science - GridWorld © A+ Computer Science -
Presentation transcript:

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

© A+ Computer Science - www.apluscompsci.com What is GridWorld? Row = 0 Column = 0 A grid is a structure that has rows and columns. A spreadsheet is a grid. A checker board is a grid. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com What is GridWorld? cols rows A grid is a structure that has rows and columns. A grid is a structure that has rows and columns. A spreadsheet is a grid. A checker board is a grid. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com What is GridWorld? Row = 2 Column = 1 A grid is a structure that has rows and columns. A spreadsheet is a grid. A checker board is a grid. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com What is GridWorld? © A+ Computer Science - www.apluscompsci.com

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

© A+ Computer Science - www.apluscompsci.com Grid Grid is an interface that details the behaviors expected of a Grid. Grid was designed as an interface because many different structures could be used to store the grid values. An interface works perfectly due to the large number of unknowns. Grid is a row / column structure that stores Objects. The location of each Object is determined by the Location provided when putting the Object in the grid. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Grid abstract methods Name Use get(loc) returns the ref at location loc getEmptyAdjacentLocations(loc) gets the valid empty locs in 8 dirs getNeighbors(loc) returns the objs around this getNumCols() gets the # of cols for this grid getNumRows() gets the # of rows for this grid getOccupiedAdjacentLocations(loc) gets the valid locs in 8 dirs that contain objs getOccupiedLocations() gets locs that contain live objs getValidAdjacentLocations(loc) gets the valid locs in 8 dirs isValid(loc) checks to see if loc is valid put(loc, obj) put the obj in grid at location loc remove(loc) take the obj at location loc out of the grid import info.gridworld.grid.Grid; © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Grid rows rows A grid is a structure that has rows and columns. A spreadsheet is a grid. A checker board is a grid. A grid is a structure that has rows and columns. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Grid cols cols A grid is a structure that has rows and columns. A grid is a structure that has rows and columns. A spreadsheet is a grid. A checker board is a grid. © A+ Computer Science - www.apluscompsci.com

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

© A+ Computer Science - www.apluscompsci.com Bug Bug differs from actor in that a bug actually moves from cell to cell. A bug moves to the cell immediately in front if possible. If a move is not possible, the bug turns in 45 degree increments until it finds a spot to which it can move. Bug is a suped up actor. Bug extends actor. Bug has two constructors, one of which takes a Color parameter. Bug will move when its act method is called. © A+ Computer Science - www.apluscompsci.com

frequently used methods © A+ Computer Science - www.apluscompsci.com Bug extends Actor frequently used methods Name Use getColor() gets the bug's color getDirection() gets the bug's direction getLocation() gets the bug's location setColor(col) sets the bug's color to col setDirection(dir) sets the bug's direction to dir The methods listed below were inherited from actor. The act method has been overridden as the behavior of a bug is quite different from an actor. The other methods listed above that were inherited have not been changed. import info.gridworld.actor.Bug; © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Bug ActorWorld world = new ActorWorld(); Bug dude = new Bug(); world.add(new Location(3,3), dude); Bug sally = new Bug(Color.GREEN); sally.setDirection(Location.SOUTHEAST); world.add(new Location(2,2), sally); Bug ali = new Bug(Color.ORANGE); ali.setDirection(Location.NORTHEAST); world.add(new Location(1,1), ali); world.show(); In this example, a default bug is created. A default bug is red and facing NORTH. The bug moves NORTH until it reaches the top of the grid. The bug them turns in 45 degree increments until it finds an empty location to which to move. Two other bugs are created as well. One of the bugs is green and the other is orange. © A+ Computer Science - www.apluscompsci.com

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

© A+ Computer Science - www.apluscompsci.com open bugone.java © A+ Computer Science - www.apluscompsci.com

Bug extends Actor frequently used constructors Name Use Bug() make a new red bug going north Bug(color) make a new bug set to color Bug is a suped up actor. Bug extends actor. Bug has a default parameter-less constructor and an additional constructor that receives a Color parameter. Bug has overridden the act method inherited from actor. Bug has some new methods that are unique to bug : canMove, move, and turn. import info.gridworld.actor.Bug; © A+ Computer Science - www.apluscompsci.com

Bug extends Actor frequently used methods – Bug specific Name Use act() move if possible or turn canMove() check to see if a move is possible move() move forward and leave a flower turn() turn 45 degrees without moving Bug is a suped up actor. Bug extends actor. Bug has a default parameter-less constructor and an additional constructor that receives a Color parameter. Bug has overridden the act method inherited from actor. Bug has some new methods that are unique to bug : canMove, move, and turn. import info.gridworld.actor.Bug; © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Bug What does a Bug do when its act() method is called ? What methods does the act() method appear to call? Each time the act method is called the bug will move to the location/cell immediately in front of it. The bug will turn if the cell/location in front of it is occupied or invalid. A flower of the same color as the bug is left in the cell/location vacated by the bug when it moves. The bug act method calls the canMove method to see if the bug can move. If the bug can move, the bug move method is called. If the bug cannot move, the turn method is called. Open up Bug.java and look at the code. Much can be gained from looking at the bug code. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com canMove The bug act method looks to see if a move is possible by calling canMove. canMove looks at the location in front of this bug to see if it is empty or if it contains a flower. canMove returns true or false. Each time the bug act method is called the bug will move to the location/cell immediately in front of it. The bug will turn if the cell/location in front of it is occupied or invalid. A flower of the same color as the bug is left in the cell/location vacated by the bug when it moves. The default color of a bug is red and the default direction of a bug is NORTH. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com move The bug act method calls move if canMove returns true. move calls moveTo to move the bug to the location in front of this bug. move leaves a flower in the old location. Each time the bug act method is called the bug will move to the location/cell immediately in front of it. The bug will turn if the cell/location in front of it is occupied or invalid. A flower of the same color as the bug is left in the cell/location vacated by the bug when it moves. The default color of a bug is red and the default direction of a bug is NORTH. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com turn The bug act method calls turn if canMove returns false. turn changes the direction of the bug by 45 degrees to the right. Each time the bug act method is called the bug will move to the location/cell immediately in front of it. The bug will turn if the cell/location in front of it is occupied or invalid. A flower of the same color as the bug is left in the cell/location vacated by the bug when it moves. The default color of a bug is red and the default direction of a bug is NORTH. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Bug ActorWorld world = new ActorWorld(); Bug dude = new Bug(Color.GREEN); dude.setDirection(Location.EAST); Location loc = new Location(5,5); world.add(loc , new Rock()); loc = new Location(2,5); world.add(loc, new Flower()); loc = new Location(2,7); world.add(loc, dude); world.show(); Each time the bug act method is called the bug will move to the location/cell immediately in front of it. The bug will turn if the cell/location in front of it is occupied or invalid. A flower of the same color as the bug is left in the cell/location vacated by the bug when it moves. The default color of a bug is red and the default direction of a bug is NORTH. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com open bugtwo.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Extending Bug © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Extending Bug How will the new bug differ from the original bug? Can the new behavior be created using existing methods? Which of the methods will be overridden? Will new methods need to be added? In order to make a new type of bug, you must understand all of the original actor and bug methods. When creating a new bug, it is important to determine what the new bug will do and how it will differ from the original bug. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Extending Bug What has to change if you want the bug to go backwards instead of forwards? Use the GW quick reference! What must change from the original bug if the new bug is to move backwards rather than forwards? How does a bug move forward? Which method makes a bug move? How does act use canMove, move, and turn? © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Extending Bug public class BackwardBug extends Bug { //constructor public void act() } //other methods Is this the only way to write this class? What methods could be changed? What must change from the original bug if the new bug is to move backwards rather than forwards? How does a bug move forward? Which method makes a bug move? How does act use canMove, move, and turn? © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com open backwardbug.java backwardbugrunner.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com open boxbug.java boxbugrunner.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Start work on Bug Exercises and Labs © A+ Computer Science - www.apluscompsci.com