1 / 15

GridWorld

GridWorld. Case Study for AP Exam Provides experiments to observe the attributes and behavior of the actors LOG IN QUICKLY . Interface . I nterface is a group of related methods with empty bodies. Grid is an interface.

vui
Download Presentation

GridWorld

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GridWorld Case Study for AP Exam Provides experiments to observe the attributes and behavior of the actors LOG IN QUICKLY

  2. Interface Interface is a group of related methods with empty bodies. Grid is an interface. All methods defined by that interface must appear in its source code before the class will successfully compile.

  3. ActorWorld Must create an ActorWorld for objects to be added to Grid. Created in the runner class. (main method) ActorWorld world = new ActorWorld();

  4. Inheritance Inheritance is way to define a new class using classes which have already been created The word extends allows the class to inherit all the field data and methods from the class it is extending public class Bug extends Actor { • The parent class is termed super class also known as base class and the inherited class is the sub classalso known as child. • The keyword extends is used by the subclass to inherit the features of super class • The subclass inheritsall of the field data and all of the methods defined in thesuperclass.

  5. Bug Bug b = new Bug(); Will create a red bug that is facing North Constructors: public Bug() { setColor(Color.RED); } /** * Constructs a bug of a given color. * @parambugColor the color for this bug */ public Bug(Color bugColor) { setColor(bugColor); } Bug b = new Bug(Color.BLUE); Choose Color; facing North Color.BLACK, Color.DARK_GRAY, Color.GRAY, Color.LIGHT_GRAY, Color.WHITE, Color.MAGENTA, Color.RED, Color.PINK, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE Color is in the java.awt Class import java.awt.Color;

  6. Methods in Bug public booleancanMove() { Grid<Actor> gr = getGrid(); // gets all actors in the grid if (gr == null) return false; Location loc = getLocation(); // returns location Location next = loc.getAdjacentLocation(getDirection()); // space ahead if (!gr.isValid(next)) // if space ahead is not empty or valid space Actor neighbor = gr.get(next); // get the actor at space ahead return (neighbor == null) || (neighbor instanceof Flower); // return t or f // True: ok to move into empty location or onto flower // False: not ok to move onto any other actor }

  7. Bug public void move() { Grid<Actor> gr = getGrid(); // get all actors in grid if (gr == null) // if no actors in grid return; Location loc = getLocation(); //get location Location next = loc.getAdjacentLocation(getDirection()); if (gr.isValid(next)) // if location next is valid True moveTo(next); // move to next location else removeSelfFromGrid(); // if not remove from grid Flower flower = new Flower(getColor()); //create flower flower.putSelfInGrid(gr, loc); //put flower in loc }

  8. Bug * Turns the bug 45 degrees to the right without changing its location. */ public void turn() { setDirection(getDirection() + Location.HALF_RIGHT); } //gets current direction and adds 45 What other angles can he turn?

  9. Bug /** * Moves if it can move, turns otherwise. */ public void act() { if (canMove()) move(); else turn(); }

  10. Inherited from Actor

  11. BugRunner2 • Open BugRunner and change name to BugRunner2. • Create a Bug and make his color be one of the color words • Create a Location Location loc2 = new Location(4,7); Bug b2 = new Bug(Color.MAGENTA); world.add(loc2,b2);

  12. Bug • Never change Bug • You create different types of Bugs by extending Bug. • BoxBugextends Bug

  13. To Do • Finish observation of Bug from yesterday and complete the questions. (Should be done quickly) • Change BugRunner to BugRunner2 and make additions as indicated on the sheet. • Start reading and looking at the BoxBug program. Complete questions from observation.

  14. BoxBug extends Bug • BoxBug - BoxBug is a child of Bug. • A BoxBug moves like a bug but it traces out a square with a certain side length. • The act method is overridden. (Changed) This is polymorphism. • When a subclass overrides a method from the parent class using the same method name it is called polymorphism. The compiler chooses the method to execute at run time. • if a BoxBug is blocked, it makes two right turns and starts again. A BoxBug has property called sideLength. If a BoxBug has a sideLength of k, then you will see k+1 flowers on each side of its traced out diagram.

  15. Assignment Read pages 10-12Answer questions 1-7Create programs 1-3 page 13,14 Gridworld – Part 2 (BoxBug) • Read (and re-read and re-re-read) Part 2 pages 10 - 12. • BoxBugand BoxBugRunner are in the boxBugfolder under GridWorldCode\projects. • Work with your "table" partner(s) and answer the questions in Set 2 on page 12 (#1 - 7). Everyone should fill out their own sheet. • Work with a partner to complete Exercises 1 – 3 CircleBug, SpiralBug, and ZBug. You should create a new "runner" program for each bug. • You may work together, but each person needs to type up the code and get it to run.

More Related