How to do well on the AP CS Free Response Questions

Slides:



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

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?
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
How to do well on the AP CS Free Response Questions
Critters A Study in Design Patterns. Design Patterns  Not specific algorithms or data structures  A general reusable solution to a common problem.
Using a different image than normal Multiple images for a class Controlling the mouse click Controlling the keys pressed Launching an input dialog box.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
AP Computer Science.  Not necessary but good programming practice in Java  When you override a super class method notation.
© A+ Computer Science - ArrayList © A+ Computer Science - Lab 16.
Multiple Choice Solutions True/False a c b e d   T F.
GridWorld Case Study1 Barbara Ericson Georgia Tech Jan 2008.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
© A+ Computer Science - Inheritance © A+ Computer Science - Lab 20.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
© 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.
1 Abstract Classes “I prefer Agassiz in the abstract, rather than in the concrete.” CS Computer Science II.
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.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
© 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 - Grid is an interface that details the behaviors expected of a grid. All its methods are abstract methods.
Copyright 2010 by Pearson Education Homework 9: Critters (cont.) reading: HW9 spec.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 4.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
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.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
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.
How to do well on the AP CS Free Response Questions
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
© 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.
© 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 - Elevens is a lab about classes and Lists. List is a major concept being tested by the Elevens lab. Elevens.
An Array-Based Implementation of the ADT List
GridWorld.
Agenda About Quiz ChameleonCritter class CrabCritter class homework.
Barbara Ericson Georgia Tech Jan 2008
A+ Computer Science AP Review 2018 AP CS A EXAM
Multiple Choice -answer the easiest question 1st
© A+ Computer Science - GridWorld © A+ Computer Science -
© A+ Computer Science - GridWorld © A+ Computer Science -
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
© A+ Computer Science - OOP © A+ Computer Science -
© A+ Computer Science - OOP Pieces © A+ Computer Science -
© 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 -
Review of Previous Lesson
GridWorld Case Study.
© A+ Computer Science - GridWorld © A+ Computer Science -
A+ Computer Science PARAMETERS
Objects with ArrayLists as Attributes
A+ Computer Science AP Review 2019 AP CS A EXAM
© A+ Computer Science - GridWorld © A+ Computer Science -
Topics OOP Review Inheritance Review Abstract Classes
Presentation transcript:

© A+ Computer Science - www.apluscompsci.com How to do well on the AP CS Free Response 2006 Questions © A+ Computer Science - www.apluscompsci.com

Visit us at www.apluscompsci.com Full Curriculum Solutions Provided by A+ Computer Science Visit us at www.apluscompsci.com Full Curriculum Solutions M/C Review Question Banks Live Programming Problems Tons of great content! www.facebook.com/APlusComputerScience © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Free Response Questions © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -Read all 4 questions before writing anything -answer the easiest question 1st -most times question 1 is the easiest -see if part B calls part A and so on -many times part C consists of A and B calls -write something on every question -write legibly / use PENCIL!!!!!!!!!! -keep track of your time © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -When writing methods -use parameter types and names as provided -do not redefine the parameters listed -do not redefine the methods provided -return from all return methods -return correct data type from return methods © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -When writing a class or methods for a class -know which methods you have -know which instance variables you have -check for public/private on methods/variables -return from all return methods -return correct data type from return methods © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -When extending a class -know which methods the parent contains -have the original class where you can see it -make sure you have super calls -check for public/private on methods/variables -make super calls in sub class methods as needed © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -When extending abstract / implementing interface -know which methods the parent contains -have the original class where you can see it -make sure you have super calls -check for public/private on methods/variables -make super calls in sub class methods as needed -implement all abstract methods in sub class © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -When writing GridWorld question -use the GridWorld quick reference -use original method code when overidding -use Bug and BoxBug code if extending Bug -use Critter and ChameleonCritter code if extending Critter -use Critter and Bug for extends demo -use Critter for ArrayList and Math.random() demo -use GW quick reference on the entire AP test © A+ Computer Science - www.apluscompsci.com

AP A Free Response Topics © A+ Computer Science - www.apluscompsci.com ArrayList – get,set,remove,add,size Abstract / Interface – super / abstract GridWorld - location, actor, bug, critter, grid Hodgepodge – array/sort/search © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com AP A - ArrayList A typical ArrayList question involves putting something into an ArrayList and removing something from an ArrayList. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com AP A - ArrayList Arraylist is a class that houses an array. An ArrayList can store any type. All ArrayLists store the first reference at spot / index position 0. ArrayList can store a reference to any type of Object. ArrayList was built using an array[] of object references. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com AP A - ArrayList int[] nums = new int[10]; //Java int array 0 1 2 3 4 5 6 7 8 9 nums An array is a group of items all of the same type which are accessed through a single identifier. © A+ Computer Science - www.apluscompsci.com

frequently used methods © A+ Computer Science - www.apluscompsci.com ArrayList frequently used methods Name Use add(item) adds item to the end of the list add(spot,item) adds item at spot – shifts items up-> set(spot,item) put item at spot z[spot]=item get(spot) returns the item at spot return z[spot] size() returns the # of items in the list remove() removes an item from the list clear() removes all items from the list import java.util.ArrayList; © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com AP A - ArrayList OUTPUT h c List ray = new ArrayList(); ray.add("hello"); ray.add("whoot"); ray.add("contests"); out.println(((String)ray.get(0)).charAt(0)); out.println(((String)ray.get(2)).charAt(0)); In the example above, ray is an ArrayList that stores Object references. In order to call non-Object methods on a spot in ray, casting would be required. ray.add(0,"hello"); out.println(((String)ray.get(0)).charAt(0)); ray stores Object references. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com AP A - ArrayList List<String> ray; ray = new ArrayList<String>(); ray.add("hello"); ray.add("whoot"); ray.add("contests"); out.println(ray.get(0).charAt(0)); out.println(ray.get(2).charAt(0)); OUTPUT h c In the example above, ray is an ArrayList that stores String references. Casting would not be required to call non-Object methods on ray. ray.add(0,"hello"); ray.add(1,"chicken"); out.println(ray.get(0).charAt(0)); out.println(ray.get(1).charAt(5)); ray stores String references. © A+ Computer Science - www.apluscompsci.com

AP A - ArrayList Removal © A+ Computer Science - www.apluscompsci.com int spot=list.size()-1; while(spot>=0) { if(list.get(spot).equals("killIt")) list.remove(spot); spot--; } © A+ Computer Science - www.apluscompsci.com

AP A - ArrayList Removal © A+ Computer Science - www.apluscompsci.com for(int spot=list.size()-1; i>=0; i--) { if(list.get(spot).equals("killIt")) list.remove(spot); } © A+ Computer Science - www.apluscompsci.com

AP A - ArrayList Removal © A+ Computer Science - www.apluscompsci.com int spot=0; while(spot<list.size()) { if(list.get(spot).equals("killIt")) list.remove(spot); else spot++; } © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Free Response © A+ Computer Science - www.apluscompsci.com

2006 Question 1 - part A public boolean conflictsWith( Appointment other ) { return getTime().overlapsWith(other.getTime()); }

2006 Question 1 - part B public void clearConflicts(Appointment appt) { int spot= 0; while (spot< apptList.size()) { Appointment currAppt = appList.get(spot); if (currAppt.conflictsWith(appt)) apptList.remove(spot); else spot++; } }

2006 Question 1 - part B public void clearConflicts(Appointment appt) { for( int spot = apptList.size()-1; spot>=0; spot-- ) { Appointment curr = appList.get(spot); if (curr.conflictsWith(appt)) apptList.remove(spot); } }

2006 Question 1 – part C public boolean addAppt(Appointment appt, boolean emergency) { if( emergency ) { clearConflicts( appt ); } else { for( Appointment curr : appList ) if( appt.conflictsWith( curr ) ) return false; } return appList.add( appt ); }

Visit us at www.apluscompsci.com Full Curriculum Solutions Provided by A+ Computer Science Visit us at www.apluscompsci.com Full Curriculum Solutions M/C Review Question Banks Live Programming Problems Tons of great content! www.facebook.com/APlusComputerScience © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com A typical Abstract/Interface question requires that a class be written that extends the abstract class or implements the interface and that all abstract method(s) be implemented. © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com Abstract classes are used to define a class that will be used only to build new classes. No objects will ever be instantiated from an abstract class. Abstract Classes are used to create hierarchies of classes. Abstract classes are used to setup future classes. An abstract class can not be instantiated. © A+ Computer Science - www.apluscompsci.com

Mammal (abstract class) Human Whale Cow AP A - Abstract/Interface Mammal (abstract class) In this example, Mammal is the abstract class. You would never have just a Mammal. For instance, you would not walk outside and go “Hey, look at that Mammal!” Mammal would be used to create something more specific, like a Dog, Human, or Whale. Human Whale Cow © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com Any sub class that extends a super abstract class must implement all methods defined as abstract in the super class. All abstract methods in the Abstract class must be implemented by the sub class extending the abstract class. This process is very similar to implementing an interface. When implementing an interface, all abstract methods in the interface must be implemented in the class. All methods in an interface are Abstract. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com abstract class Monster{ private String myName; public Monster() { myName =""; } public Monster(String name) { myName =name; public String getName() { return myName; public abstract void talk( ); public String toString() { return myName + "\n"; AP A - Abstract / Interface Monster is an abstract class. Monster x = new Monster(); //illegal Monster y = new Ghost(); //legal Monster cannot be instantiated. Monster contains one abstract method, method talk(); The assumption is that all Monsters will have a name and name related methods. Also, it is assumed that all Monsters will talk. There is no way to know at the Monster level what exactly a particular type of Monster will say. The talk() method is abstract because it makes no sense it implement talk() at the Monster level. It makes more sense to write talk() for each specific Monster as each specific Monster will say something specific. © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com Why define talk as abstract? public abstract void talk( ); Does each Monster say the exact same thing? Each Monster will say something specific. A Ghost will say something different than a Werewolf and a Yetti will say something different from a Vampire. Thus, the talk() method is abstract as it will be written in each of the specific Monsters in a way specific to each Monster. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Vampire class Vampire extends Monster { public Vampire( String name ) { super(name); } public void talk() out.println("Vampire " + getName() + " say \"I want to drink your blood!\""); Vampire is a Monster, but it is a Monster that talks in its own specific way. A Vampire says “I want to drink your blood!”. Sub Class © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com public abstract class APlus { public APlus(int x) //constructor code not shown public abstract double goForIt(); //other fields/methods not shown } Pet Item © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com public class PassAPTest extends APlus { public PassAPTest(int x) { super(x); } public double goForIt() { double run=0.0; //write some code - run = x*y/z return run; } //other fields/methods not shown } public abstract class APlus { public APlus(int x) //constructor code not shown public abstract double goForIt(); //other fields/methods not shown } © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com public interface Exampleable { int writeIt(Object o); int x = 123; } All methods listed in an interface are public abstract. Abstract methods have no code. Each abstract method listed in an interface must be implemented in the class that implements the interface. All variables listed in an interface are public static final, making them final class variables. Interfaces cannot contain implemented methods, constructors, or instance variables. Methods are public abstract! Variables are public static final! © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com public interface Exampleable { public abstract int writeIt(Object o); public static final int x = 123; } All methods listed in an interface are public abstract. Abstract methods have no code. Each abstract method listed in an interface must be implemented in the class that implements the interface. All variables listed in an interface are public static final, making them final class variables. Interfaces cannot contain implemented methods, constructors, or instance variables. Methods are public abstract! Variables are public static final! © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com An interface is a list of abstract methods that must be implemented. An interface may not contain any implemented methods. Interfaces cannot have constructors!!! All methods listed in an interface are public abstract. Abstract methods have no code. Each abstract method listed in an interface must be implemented in the class that implements the interface. All variables listed in an interface are public static final, making them final class variables. Interfaces cannot contain implemented methods, constructors, or instance variables. © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com Interfaces are typically used when you know what you want an Object to do, but do not know how it will be done. If only the behavior is known, use an interface. Interfaces are used to detail what things an Object should do. Interfaces are used typically when the way an Object will do things is unknown. Comparable is a great example. With Comparable, it is clear that each Object should be compared to another Object of the same type. Comparable is an interface because it is not known what the Objects that implement Comparable will contain. It is known that the Object should be compared to other Obejcts of the same type in a certain way. © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com Abstract classes are typically used when you know what you want an Object to do and have a bit of an idea how it will be done. If the behavior is known and some properties are known, use an abstract class. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Free Response © A+ Computer Science - www.apluscompsci.com

2006 Question 2 - part A public double purchasePrice() { return getListPrice() + getListPrice()* taxRate; }

2006 Question 2 - part B You must call super! public class Vehicle extends TaxableItem { private double dCost; private double dMarkup; public Vehicle(double cost, double markup, double tRate) { super(tRate); dMarkup = markup; dCost = cost; } public double getListPrice() { return dMarkup + dCost; } public void changeMarkup(double markup) { dMarkup = markup; } } You must call super!

Visit us at www.apluscompsci.com Full Curriculum Solutions Provided by A+ Computer Science Visit us at www.apluscompsci.com Full Curriculum Solutions M/C Review Question Banks Live Programming Problems Tons of great content! www.facebook.com/APlusComputerScience © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com AP A - HodgePodge One question on the A test free response is usually a random question that is hard to predict. CustomerSort Robot Reservation © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com AP A - HodgePodge This question usually involves an array and many times has sorting and searching components. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Free Response © A+ Computer Science - www.apluscompsci.com

2006 Question 3 - part A public int compareCustomer(Customer other) { int cmp = getName().compareTo(other.getName()); if(cmp != 0) { return cmp; } return getID() - other.getID(); }

2006 Question 3 - part B You must know merge sort! public static void preFixMerge(Customer[] list1, Customer[] list2, Customer[] result) { int front1 = 0; int front2 = 0; for( int i = 0; i < result.length; i++) { int cmp = list[front1].compareCustomer(list2[front2]); if( cmp < 0 ){ results[i] = list[front1]; front1++; } else if (cmp > 0){ result[i] = list2[front2]; front2++; } else { result[i] = list1[front1]; front1++; front2++; } } } You must know merge sort!

Visit us at www.apluscompsci.com Full Curriculum Solutions Provided by A+ Computer Science Visit us at www.apluscompsci.com Full Curriculum Solutions M/C Review Question Banks Live Programming Problems Tons of great content! www.facebook.com/APlusComputerScience © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com AP A - Case Study One question on the A test free response will test the GridWorld Case Study. © 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 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. import info.gridworld.grid.Location; © 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 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

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

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 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 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 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

frequently used methods © A+ Computer Science - www.apluscompsci.com Bug extends Actor frequently used methods Name Use act() move if possible or turn 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

Bug extends Actor frequently used methods – Bug only Name Use Bug() make a new red bug going north Bug(color) make a new color bug 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 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 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

frequently used methods © A+ Computer Science - www.apluscompsci.com Critter extends Actor frequently used methods Name Use getColor() gets the critter's color getDirection() gets the critter's direction getLocation() gets the critter's location setColor(col) sets the critter's color to col setDirection(dir) sets the critter'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.Critter; © A+ Computer Science - www.apluscompsci.com

Critter extends Actor frequently used methods – Critter specific Name Use act() calls the methods listed below getActors() gets all actors around this location processActors(actors) do something to actors sent in getMoveLocations() gets list of possible move locs selectMoveLocation(locs) picks loc to move to makeMove(loc) moves this critter to loc Critter’s act method first calls the getActors method to get a list of actors around this critter. Act then calls processActors and sends it the ArrayList of actors built by getActors. processActors typically does something to some or all of the actors in the list. Act calls getMoveLocations next. getMoveLocations builds and returns an ArrayList of locations to which this critter could move. selectMoveLocation is called and sent the ArrayList built by getMoveLocations. selectMoveLocation randomly picks one of the locations and returns it. makeMove is called last by act and makes the critter move if possible. import info.gridworld.actor.Critter; © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com act if no grid present – stop call getActors to get list of actors to proces processActors received from getActors call getMoveLocations to get a list of locations to which the critter might move call selectMoveLocation to select new location move to the new loc Critter’s act method first calls the getActors method to get a list of actors around this critter. Act then calls processActors and sends it the ArrayList of actors built by getActors. processActors typically does something to some or all of the actors in the list. Act calls getMoveLocations next. getMoveLocations builds and returns an ArrayList of locations to which this critter could move. selectMoveLocation is called and sent the ArrayList built by getMoveLocations. selectMoveLocation randomly picks one of the locations and returns it. makeMove is called last by act and makes the critter move if possible. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com getActors The getActors method returns an ArrayList containing all of the actors around this critter using the 4 cardinal(N,S,E,W) and 4 intercardinal directions(NE, NW, SE, SW). In order to change which actors are returned by getActors, override the method and provide a different method of selecting actors. getActors must not modify any actors. Critter’s act method first calls the getActors method to get a list of actors around this critter. Act then calls processActors and sends it the ArrayList of actors built by getActors. processActors typically does something to some or all of the actors. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Free Response © A+ Computer Science - www.apluscompsci.com

2006 Question 4 - part A public Location dropLocationForColumn(int column) { for(int r = theEnv.getNumRows()-1; r>=0; r--) { Location loc = new Location(r, column); if(theEnv.isValid(loc) && theEnv.get(loc)==null) { return loc; } } return null; }

Visit us at www.apluscompsci.com Full Curriculum Solutions Provided by A+ Computer Science Visit us at www.apluscompsci.com Full Curriculum Solutions M/C Review Question Banks Live Programming Problems Tons of great content! www.facebook.com/APlusComputerScience © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -Read all 4 questions before writing anything -answer the easiest question 1st -most times question 1 is the easiest -see if part B calls part A and so on -many times part C consists of A and B calls -write something on every question -write legibly / use PENCIL!!!!!!!!!! -keep track of your time © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -When writing methods -use parameter types and names as provided -do not redefine the parameters listed -do not redefine the methods provided -return from all return methods -return correct data type from return methods © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -When writing a class or methods for a class -know which methods you have -know which instance variables you have -check for public/private on methods/variables -return from all return methods -return correct data type from return methods © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -When extending a class -know which methods the parent contains -have the original class where you can see it -make sure you have super calls -check for public/private on methods/variables -make super calls in sub class methods as needed © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -When extending abstract / implementing interface -know which methods the parent contains -have the original class where you can see it -make sure you have super calls -check for public/private on methods/variables -make super calls in sub class methods as needed -implement all abstract methods in sub class © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -When writing GridWorld question -use the GridWorld quick reference -use original method code when overidding -use Bug and BoxBug code if extending Bug -use Critter and ChameleonCritter code if extending Critter -use Critter and Bug for extends demo -use Critter for ArrayList and Math.random() demo -use GW quick reference on the entire AP test © A+ Computer Science - www.apluscompsci.com

Visit us at www.apluscompsci.com Full Curriculum Solutions Provided by A+ Computer Science Visit us at www.apluscompsci.com Full Curriculum Solutions M/C Review Question Banks Live Programming Problems Tons of great content! www.facebook.com/APlusComputerScience © A+ Computer Science - www.apluscompsci.com