GridWorld Case Study for AP Exam Provides experiments to observe the attributes and behavior of the actors LOG IN QUICKLY 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. ActorWorld Must create an ActorWorld for objects to be added to Grid. Created in the runner class. (main method) ActorWorld world = new ActorWorld(); 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 class also known as child. • The keyword extends is used by the subclass to inherit the features of super class • The subclass inherits all of the field data and all of the methods defined in the superclass. Bug Constructors: public Bug() { setColor(Color.RED); } Bug b = new Bug(); Will create a red bug that is facing North /** * Constructs a bug of a given color. * @param bugColor the color for this bug */ public Bug(Color bugColor) Bug b = { Choose setColor(bugColor); } new Bug(Color.BLUE); 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; Methods in Bug public boolean canMove() { 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 } 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 } 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? Bug /** * Moves if it can move, turns otherwise. */ public void act() { if (canMove()) move(); else turn(); } Inherited from Actor 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); Bug • Never change Bug • You create different types of Bugs by extending Bug. • BoxBug extends Bug 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. 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. Assignment Read pages 10-12 Answer questions 1-7 Create programs 1-3 page 13,14 Gridworld – Part 2 (BoxBug) 1. Read (and re-read and re-re-read) Part 2 pages 10 - 12. 2. BoxBug and BoxBugRunner are in the boxBug folder under GridWorldCode\projects. 3. 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. 4. Work with a partner to complete Exercises 1 – 3 CircleBug, SpiralBug, and ZBug. You should create a new "runner" program for each bug. 5. You may work together, but each person needs to type up the code and get it to run.