© 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.
© 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.
Inheritance Inheritance is the process of using features (both attributes and methods) from an existing class. The existing class is called the superclass.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects Real and Java COMP.
GridWorld Case Study1 Barbara Ericson Georgia Tech Jan 2008.
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
© A+ Computer Science - Row = 0 Column = 0.
How to do well on the AP CS Free Response Questions
Inheritance Inheritance is the process of using features (both attributes and methods) from an existing class. The existing class is called the superclass.
© A+ Computer Science - Chicken yeller = new Chicken();
GridWorld Case Study Barbara Ericson March 24, 2007.
Java Arrays  Java has a static array capable of multi-dimensions.  Java has a dynamic array, which is also capable of multi-dimensions.  The ArrayList.
© A+ Computer Science - Grid is an interface that details the behaviors expected of a grid. All its methods are abstract methods.
1 Advanced Computer Programming Lab Calculator Project.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
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.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
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.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
© 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.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
GridWorld.
Objects Real and Java COMP T1 3
User-Written Functions
Topics Graphical User Interfaces Using the tkinter Module
OOP Powerpoint slides from A+ Computer Science
Lecture 27 Creating Custom GUIs
Agenda About Quiz ChameleonCritter class CrabCritter class homework.
Barbara Ericson Georgia Tech Jan 2008
AP "Case Studies" A big program for AP students to understand
Creating Games with Greenfoot
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
CS 302 Week 11 Jim Williams, PhD.
Multiple Choice -answer the easiest question 1st
Board: Objects, Arrays and Pedagogy
Lesson A4 – Object Behavior
Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects Outline Test-Driving the Microwave Oven Application Designing.
Section 8.7 The Consequences of Scope.
CSC 113: Computer programming II
© A+ Computer Science - GridWorld © A+ Computer Science -
A closer look at the world
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
A+ Computer Science METHODS.
© A+ Computer Science - What is a LOOP? © A+ Computer Science -
© A+ Computer Science - OOP © A+ Computer Science -
Recitation 7 October 7, 2011.
© A+ Computer Science - OOP Pieces © A+ Computer Science -
A+ Computer Science METHODS.
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 PARAMETERS
A+ Computer Science AP Review 2019 AP CS A EXAM
© 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 Location © A+ Computer Science - www.apluscompsci.com

frequently used methods © A+ Computer Science - www.apluscompsci.com Location frequently used methods Name Use Location(row, col) creates a new row,col Location getCol() gets the column value for this location getRow() gets the row value for this location import info.gridworld.grid.Location; © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Location Location locTwo = new Location(3,5); System.out.println(locTwo); System.out.println(locTwo.getRow()); System.out.println(locTwo.getCol()); OUTPUT (3, 5) 3 5 Location is a class that stores row and column information. Location spot = new Location(4,5); spot has a row value of 4 and a column value of 5. The Location class stores row and column information. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Location reference variable Location theLoc = new Location(3,4); Location is a class which must be instantiated before it can be used. In other words, you must make a new Location if you want to use a Location. A reference must be used to store the location in memory of the Location object created. A row value and a column value must be passed as parameters to the Location constructor. theLoc is a reference that will store the location/memory address of newly created Location object. object instantiation © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Location reference command / method theLoc.getRow(); © A+ Computer Science - www.apluscompsci.com

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

frequently used fields © A+ Computer Science - www.apluscompsci.com Location frequently used fields Name Use NORTH indicates going north – value of 0 SOUTH indicates going south – value of 180 EAST indicates going east – value of 90 WEST indicates going west – value of 270 The location class contains the following 8 directions : Location.NORTH Location.SOUTH Location.EAST Location.WEST Location.NORTHWEST Location.SOUTHWEST Location.NORTHEAST All of the direction fields in the location class are final integers. import info.gridworld.grid.Location; © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Location NORTH - 0 Dude WEST - 270 EAST - 90 The location class contains the following 8 directions : Location.NORTH Location.SOUTH Location.EAST Location.WEST Location.NORTHWEST Location.SOUTHWEST Location.NORTHEAST All of the direction fields in the location class are final integers. SOUTH - 180 © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Location System.out.println(Location.NORTH); System.out.println(Location.SOUTH); System.out.println(Location.EAST); System.out.println(Location.WEST); OUTPUT 180 90 270 The location class contains the following 8 directions : Location.NORTH Location.SOUTH Location.EAST Location.WEST Location.NORTHWEST Location.SOUTHWEST Location.NORTHEAST All of the direction fields in the location class are final integers. © A+ Computer Science - www.apluscompsci.com

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

frequently used methods © A+ Computer Science - www.apluscompsci.com Location frequently used methods Name Use getAdjacentLocation(dir) get nearest loc in the dir getDirectionToward(dest) gives dir needed to reach dest compareTo(thang) compares this to thang equals(thang) test equality of this and thang import info.gridworld.grid.Location; © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Location Location locOne = new Location(2,1); Location locTwo = new Location(1,3); out.println(locOne.getAdjacentLocation(Location.NORTH)); out.println(locOne.getAdjacentLocation(Location.SOUTH)); out.println(locOne.getAdjacentLocation(Location.EAST)); out.println(locOne.getAdjacentLocation(Location.WEST)); out.println(locOne.getDirectionToward(locTwo)); OUTPUT (1, 1) (3, 1) (2, 2) (2, 0) 45 getAdjacentLocation will return a nearby location based on a provided direction. getDirectionToward will look at two locations and return the direction required to go from a to b. 0,0 0,1 0,2 0,3 0,4 1,0 1,1 1,2 1,3 1,4 2,0 2,1 2,2 2,3 2,4 3,0 3,1 3,2 3,3 3,4 © A+ Computer Science - www.apluscompsci.com

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

© A+ Computer Science - www.apluscompsci.com Location Location locOne = new Location(9,1); Location locTwo = new Location(3,6); System.out.println(locOne.equals(locTwo)); System.out.println(locOne.compareTo(locTwo)); System.out.println(locTwo.compareTo(locOne)); The equals method compares two locations to see if both have the same row and column values. The compareTo method compares two locations for equality, greater than, and less than. If A>B, compareTo returns a value >0. If A<B, compareTo returns a value <0; If A==B, compareTo returns 0. When comparing locations, compareTo first compares the row value. If the row values are the same, the column value is compared. OUTPUT false 1 -1 © A+ Computer Science - www.apluscompsci.com

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

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

© A+ Computer Science - www.apluscompsci.com Actor Actor is the basic object from which all other GridWorld actors will be built. Each of the new actors created will extend the original actor class. © A+ Computer Science - www.apluscompsci.com

frequently used methods © A+ Computer Science - www.apluscompsci.com Actor frequently used methods Name Use Actor() creates new blue north bound actor act() reverses the direction for actor getColor() gets the actor’s color getDirection() gets the actor’s direction getLocation() gets the actor’s location setColor(col) sets the actor’s color to col setDirection(dir) sets the actor’s direction to dir moveTo(loc) moves the actor to new location loc Notice that actor has only one constructor and that that constructor takes no parameters. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Actor ActorWorld world = new ActorWorld(); Actor dude = new Actor(); world.add(new Location(0,0), dude); world.show(); The add method of class world receives a location parameter and an actor. The actor reference is stored in the grid at the specified location. The default color of an actor is BLUE. What happens if you click on the actor? © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Actor When you click on an actor, a list of methods is shown. © A+ Computer Science - www.apluscompsci.com

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

© A+ Computer Science - www.apluscompsci.com Actor ActorWorld world = new ActorWorld(); Actor dude = new Actor(); dude.setColor(Color.GREEN); dude.setDirection(Location.SOUTH); Location loc = new Location(2,2); world.add(loc, dude); world.show(); If you want an actor have a color other than BLUE, simply call the setColor method and provide the color of your choice. What happens if you click on an empty location? © A+ Computer Science - www.apluscompsci.com

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

© A+ Computer Science - www.apluscompsci.com Actor What does an Actor do when its act() method is called ? How does the act() method get called? An actor does not move from cell to cell, but it does flip from side to side within its cell. An actor’s act method is called when the step or run button is called. The act method contains the code that determines the behavior for each type of actor. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com act The actor act method calls methods to make the actor do something. Each time the act method for the default actor is called, the actor changes to the opposite direction. An actor does not move from cell to cell, but it does flip from side to side within its cell. An actor’s act method is called when the step or run button is called. The act method contains the code that determines the behavior for each type of actor. The act method will be overridden when new types of actors are created. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com moveTo The moveTo method is essentially a setLocation method. The moveTo method is used to make an actor move to a new location. chucky.moveTo(new Location(3,3)); The actor moveTo method can be used to move an actor to another location. The actor moveTo method functions like a setLocation method. Actor does not have a setLocation method. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Actor ActorWorld world = new ActorWorld(); Actor dude = new Actor(); dude.setColor(Color.ORANGE); dude.setDirection(Location.WEST); world.add(new Location(1,2), dude); dude.moveTo(new Location(6,7)); dude.moveTo(new Location(8,7)); world.show(); You can place an actor in the grid at a specified location and then that actor’s location may change as the program runs. You can also place the actor in the grid in the main and then for testing and understanding, change the location with code statements to see the effect. For the code above, the actor will appear at position 8,7. The actor had an initial location of 1,2 that was changed to 6,7 and then finally set as 8,7. Where does dude show up? © A+ Computer Science - www.apluscompsci.com

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

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

© A+ Computer Science - www.apluscompsci.com Extending Actor To make a new actor, you must extend the Actor class and override the act method to give the new actor its own unique behavior. What would have to be done to make a new actor that only moved to the right? When extending actor, the act method will most always be overridden to create some new type of actor with new behaviors. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Extending Actor public class SideWaysActor extends Actor { public void act() { //move to the right } When extending actor, the act method will most always be overridden to create some new type of actor with new behaviors. © A+ Computer Science - www.apluscompsci.com

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

© A+ Computer Science - www.apluscompsci.com Start work on Actor Labs © A+ Computer Science - www.apluscompsci.com

frequently used methods © A+ Computer Science - www.apluscompsci.com Actor frequently used methods Name Use putSelfInGrid(grid, loc) put this actor in grid at loc removeSelfFromGrid() takes this actor out of the grid getGrid() gets the grid which contains this actor toString() gets actor data as a String import info.gridworld.actor.Actor; © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com putSelfInGrid The putSelfInGrid method puts an actor into a grid at a specified location. The world add method calls putSelfInGrid when adding an actor to the grid. world.add(loc, chucky); chucky.putSelfInGrid(grid, loc); When placing an actor in the grid, the world add method or actor putSelfInGrid method must be called. The world add method calls the actor putSelfInGrid method automatically. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com removeSelfFromGrid The removeSelfFromGrid method removes an actor from its grid. When it is time to do away with an actor, call removeSelfFromGrid and the actor will disappear. chucky.removeSelfFromGrid(); The removeSelfFromGrid is useful to remove an actor from a grid. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Actor ActorWorld world = new ActorWorld(); Actor dude = new Actor(); dude.setColor(Color.YELLOW); dude.setDirection(Location.SOUTH); Location loc = new Location(1,4); dude.putSelfInGrid(world.getGrid(),loc); world.show(); When placing an actor in the grid, the world add method or actor putSelfInGrid method must be called. The world add method calls the actor putSelfInGrid method automatically. What happens when you hit the run button? © A+ Computer Science - www.apluscompsci.com

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

© A+ Computer Science - www.apluscompsci.com Grid All of the actors are stored in a grid. A grid has rows and columns. cols rows 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 World is used to show the grid graphically. World stores the grid of Objects. World is a graphical environment that displays the grid and its contents graphically. © A+ Computer Science - www.apluscompsci.com

Grid frequently used methods Name Use get(loc) returns the object at location loc getNumCols() gets the # of cols for this grid getNumRows() gets the # of rows for this grid 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 getGrid The getGrid method returns the grid housing this actor. Grid<Actor> grid = chucky.getGrid(); The getGrid method returns a reference to a grid in which this actor is stored. If you need an actor’s grid, call getGrid. getGrid will become more useful as new types of actors are created. © A+ Computer Science - www.apluscompsci.com

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

© A+ Computer Science - www.apluscompsci.com Continue work on Actor Labs © A+ Computer Science - www.apluscompsci.com