SlideShare une entreprise Scribd logo
1  sur  16
Télécharger pour lire hors ligne
Quick Reference
AP® Computer Science A




                 © 2011 The College Board.
Visit the College Board on the Web: www.collegeboard.org.
Content of Appendixes


Appendix A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Java Quick Reference
Appendix B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testable API
Appendix C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testable Code
Appendix E . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . GridWorld Quick Reference
Appendix G . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Index for Source Code




                                                           i
Appendix A                                                                   Java Quick Reference


                   Appendix A — Java Quick Reference
          Accessible Methods from the Java Library That May Be Included on the Exam

class java.lang.Object
• boolean equals(Object other)
• String toString()
class java.lang.Integer
• Integer(int value)
• int intValue()
• Integer.MIN_VALUE                        // minimum value represented by an int or Integer
• Integer.MAX_VALUE                        // maximum value represented by an int or Integer

class java.lang.Double
• Double(double value)
• double doubleValue()
class java.lang.String
• int length()
• String substring(int from, int to) //         returns the substring beginning at from
                                     //         and ending at to-1
• String substring(int from)         //         returns substring(from, length())
• int indexOf(String str)            //         returns the index of the first occurrence of str;
                                     //         returns -1 if not found
• int compareTo(String other)        //         returns a value < 0 if this is less than other
                                     //         returns a value = 0 if this is equal to other
                                     //         returns a value > 0 if this is greater than other

class java.lang.Math
• static int abs(int x)
• static double abs(double x)
• static double pow(double base, double exponent)
• static double sqrt(double x)
• static double random()             // returns a double in the range [0.0, 1.0)

interface java.util.List<E>
• int size()
• boolean add(E obj)                       //   appends obj to end of list; returns true
• void add(int index, E obj)               //   inserts obj at position index (0 ≤ index ≤ size) ,
                                           //   moving elements at position index and higher
                                           //   to the right (adds 1 to their indices) and adjusts size
•   E get(int index)
•   E set(int index, E obj)                //   replaces the element at position index with obj
                                           //   returns the element formerly at the specified position
•   E remove(int index)                    //   removes element from position index, moving elements
                                           //   at position index + 1 and higher to the left
                                           //   (subtracts 1 from their indices) and adjusts size
                                           //   returns the element formerly at the specified position

class java.util.ArrayList<E> implements java.util.List<E>




                                            -A1-
Appendix B                                                                                              Testable API


                                  Appendix B — Testable API
info.gridworld.grid.Location class (implements Comparable)
public Location(int r, int c)
      constructs a location with given row and column coordinates

public int getRow()
      returns the row of this location

public int getCol()
      returns the column of this location

public Location getAdjacentLocation(int direction)
      returns the adjacent location in the direction that is closest to direction

public int getDirectionToward(Location target)
      returns the closest compass direction from this location toward target

public boolean equals(Object other)
      returns true if other is a Location with the same row and column as this location; false otherwise

public int hashCode()
      returns a hash code for this location

public int compareTo(Object other)
      returns a negative integer if this location is less than other, zero if the two locations are equal, or a positive
      integer if this location is greater than other. Locations are ordered in row-major order.
      Precondition: other is a Location object.

public String toString()
      returns a string with the row and column of this location, in the format (row, col)

Compass directions:
public    static      final   int   NORTH = 0;
public    static      final   int   EAST = 90;
public    static      final   int   SOUTH = 180;
public    static      final   int   WEST = 270;
public    static      final   int   NORTHEAST = 45;
public    static      final   int   SOUTHEAST = 135;
public    static      final   int   SOUTHWEST = 225;
public    static      final   int   NORTHWEST = 315;

Turn angles:
public    static      final   int   LEFT = -90;
public    static      final   int   RIGHT = 90;
public    static      final   int   HALF_LEFT = -45;
public    static      final   int   HALF_RIGHT = 45;
public    static      final   int   FULL_CIRCLE = 360;
public    static      final   int   HALF_CIRCLE = 180;
public    static      final   int   AHEAD = 0;




                                                          -B1-
Appendix B                                                                                          Testable API




info.gridworld.grid.Grid<E> interface
int getNumRows()
      returns the number of rows, or -1 if this grid is unbounded

int getNumCols()
      returns the number of columns, or -1 if this grid is unbounded

boolean isValid(Location loc)
      returns true if loc is valid in this grid, false otherwise
      Precondition: loc is not null

E put(Location loc, E obj)
      puts obj at location loc in this grid and returns the object previously at that location (or null if the
      location was previously unoccupied).
      Precondition: (1) loc is valid in this grid (2) obj is not null

E remove(Location loc)
      removes the object at location loc from this grid and returns the object that was removed (or null if the
      location is unoccupied)
      Precondition: loc is valid in this grid

E get(Location loc)
      returns the object at location loc (or null if the location is unoccupied)
      Precondition: loc is valid in this grid

ArrayList<Location> getOccupiedLocations()
      returns an array list of all occupied locations in this grid

ArrayList<Location> getValidAdjacentLocations(Location loc)
      returns an array list of the valid locations adjacent to loc in this grid
      Precondition: loc is valid in this grid

ArrayList<Location> getEmptyAdjacentLocations(Location loc)
      returns an array list of the valid empty locations adjacent to loc in this grid
      Precondition: loc is valid in this grid

ArrayList<Location> getOccupiedAdjacentLocations(Location loc)
      returns an array list of the valid occupied locations adjacent to loc in this grid
      Precondition: loc is valid in this grid

ArrayList<E> getNeighbors(Location loc)
      returns an array list of the objects in the occupied locations adjacent to loc in this grid
      Precondition: loc is valid in this grid




                                                           -B2-
Appendix B                                                                                           Testable API




info.gridworld.actor.Actor class
public Actor()
      constructs a blue actor that is facing north

public Color getColor()
      returns the color of this actor

public void setColor(Color newColor)
      sets the color of this actor to newColor

public int getDirection()
      returns the direction of this actor, an angle between 0 and 359 degrees

public void setDirection(int newDirection)
      sets the direction of this actor to the angle between 0 and 359 degrees that is equivalent to newDirection

public Grid<Actor> getGrid()
      returns the grid of this actor, or null if this actor is not contained in a grid

public Location getLocation()
      returns the location of this actor, or null if this actor is not contained in a grid

public void putSelfInGrid(Grid<Actor> gr, Location loc)
      puts this actor into location loc of grid gr. If there is another actor at loc, it is removed.
      Precondition: (1) This actor is not contained in a grid. (2) loc is valid in gr.

public void removeSelfFromGrid()
      removes this actor from its grid
      Precondition: this actor is contained in a grid

public void moveTo(Location newLocation)
      moves this actor to newLocation. If there is another actor at newLocation, it is removed.
      Precondition: (1) This actor is contained in a grid. (2) newLocation is valid in the grid of this actor.

public void act()
      reverses the direction of this actor. Override this method in subclasses of Actor to define types of actors with
      different behavior.

public String toString()
      returns a string with the location, direction, and color of this actor




                                                           -B3-
Appendix B                                                        Testable API




info.gridworld.actor.Rock class (extends Actor)
public Rock()
      constructs a black rock

public Rock(Color rockColor)
      constructs a rock with color rockColor

public void act()
      overrides the act method in the Actor class to do nothing



info.gridworld.actor.Flower class (extends Actor)
public Flower()
      constructs a pink flower

public Flower(Color initialColor)
      constructs a flower with color initialColor

public void act()
      causes the color of this flower to darken




                                                  -B4-
Appendix C                                                                     Bug.java


                              Appendix C — Testable Code
Bug.java
package info.gridworld.actor;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
import java.awt.Color;

/**
 * A Bug is an actor that can move and turn. It drops flowers as it moves.
 * The implementation of this class is testable on the AP CS A and AB Exams.
 */
public class Bug extends Actor
{
  /**
   * Constructs a red bug.
   */
  public Bug()
  {
    setColor(Color.RED);
  }


  /**
   * Constructs a bug of a given color.
   * @param bugColor the color for this bug
   */
  public Bug(Color bugColor)
  {
    setColor(bugColor);
  }


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


  /**
   * Turns the bug 45 degrees to the right without changing its location.
   */
  public void turn()
  {
    setDirection(getDirection() + Location.HALF_RIGHT);
  }




                                                      -C1-
Appendix C                                                                                           Bug.java


    /**
     * Moves the bug forward, putting a flower into the location it previously occupied.
     */
    public void move()
    {
      Grid<Actor> gr = getGrid();
      if (gr == null)
        return;
      Location loc = getLocation();
      Location next = loc.getAdjacentLocation(getDirection());
      if (gr.isValid(next))
        moveTo(next);
      else
        removeSelfFromGrid();
      Flower flower = new Flower(getColor());
      flower.putSelfInGrid(gr, loc);
    }


    /**
     * Tests whether this bug can move forward into a location that is empty or contains a flower.
     * @return true if this bug can move.
     */
    public boolean canMove()
    {
      Grid<Actor> gr = getGrid();
      if (gr == null)
        return false;
      Location loc = getLocation();
      Location next = loc.getAdjacentLocation(getDirection());
      if (!gr.isValid(next))
        return false;
      Actor neighbor = gr.get(next);
      return (neighbor == null) || (neighbor instanceof Flower);
      // ok to move into empty location or onto flower
      // not ok to move onto any other actor
    }
}




                                                          -C2-
Appendix C                                                                     BoxBug.java



BoxBug.java
import info.gridworld.actor.Bug;
/**
 * A BoxBug traces out a square “box” of a given size.
 * The implementation of this class is testable on the AP CS A and AB Exams.
 */
public class BoxBug extends Bug
{
  private int steps;
  private int sideLength;

    /**
     * Constructs a box bug that traces a square of a given side length.
     * @param length the side length
     */
    public BoxBug(int length)
    {
      steps = 0;
      sideLength = length;
    }

      /**
       * Moves to the next location of the square.
       */
    public void act()
    {
      if (steps < sideLength && canMove())
      {
        move();
        steps++;
      }
      else
      {
        turn();
        turn();
        steps = 0;
      }
    }
}




                                                           -C3-
Appendix C                                                                                               Critter.java



Critter.java
package info.gridworld.actor;
import info.gridworld.grid.Location;
import java.util.ArrayList;
/**
 * A Critter is an actor that moves through its world, processing
 * other actors in some way and then moving to a new location.
 * Define your own critters by extending this class and overriding any methods of this class except for act.
 * When you override these methods, be sure to preserve the postconditions.
 * The implementation of this class is testable on the AP CS A and AB Exams.
 */
public class Critter extends Actor
{
  /**
   * A critter acts by getting a list of other actors, processing that list, getting locations to move to,
   * selecting one of them, and moving to the selected location.
   */
  public void act()
  {
    if (getGrid() == null)
       return;
    ArrayList<Actor> actors = getActors();
    processActors(actors);
    ArrayList<Location> moveLocs = getMoveLocations();
    Location loc = selectMoveLocation(moveLocs);
    makeMove(loc);
  }


  /**
   * Gets the actors for processing. Implemented to return the actors that occupy neighboring grid locations.
   * Override this method in subclasses to look elsewhere for actors to process.
   * Postcondition: The state of all actors is unchanged.
   * @return a list of actors that this critter wishes to process
   */
  public ArrayList<Actor> getActors()
  {
    return getGrid().getNeighbors(getLocation());
  }




                                                        -C4-
Appendix C                                                                                                  Critter.java


 /**
  * Processes the elements of actors. New actors may be added to empty locations.
  * Implemented to “eat” (i.e., remove) selected actors that are not rocks or critters.
  * Override this method in subclasses to process actors in a different way.
  * Postcondition: (1) The state of all actors in the grid other than this critter and the
  * elements of actors is unchanged. (2) The location of this critter is unchanged.
  * @param actors the actors to be processed
  */
 public void processActors(ArrayList<Actor> actors)
 {
   for (Actor a : actors)
   {
      if (!(a instanceof Rock) && !(a instanceof Critter))
        a.removeSelfFromGrid();
   }
 }

 /**
  * Gets a list of possible locations for the next move. These locations must be valid in the grid of this critter.
  * Implemented to return the empty neighboring locations. Override this method in subclasses to look
  * elsewhere for move locations.
  * Postcondition: The state of all actors is unchanged.
  * @return a list of possible locations for the next move
  */
 public ArrayList<Location> getMoveLocations()
 {
   return getGrid().getEmptyAdjacentLocations(getLocation());
 }

 /**
  * Selects the location for the next move. Implemented to randomly pick one of the possible locations,
  * or to return the current location if locs has size 0. Override this method in subclasses that
  * have another mechanism for selecting the next move location.
  * Postcondition: (1) The returned location is an element of locs, this critter’s current location, or null.
  * (2) The state of all actors is unchanged.
  * @param locs the possible locations for the next move
  * @return the location that was selected for the next move
  */
 public Location selectMoveLocation(ArrayList<Location> locs)
 {
   int n = locs.size();
   if (n == 0)
      return getLocation();
   int r = (int) (Math.random() * n);
   return locs.get(r);
 }




                                                          -C5-
Appendix C                                                                                                  Critter.java

    /**
     * Moves this critter to the given location loc, or removes this critter from its grid if loc is null.
     * An actor may be added to the old location. If there is a different actor at location loc, that actor is
     * removed from the grid. Override this method in subclasses that want to carry out other actions
     * (for example, turning this critter or adding an occupant in its previous location).
     * Postcondition: (1) getLocation() == loc.
     * (2) The state of all actors other than those at the old and new locations is unchanged.
     * @param loc the location to move to
     */
    public void makeMove(Location loc)
    {
      if (loc == null)
         removeSelfFromGrid();
      else
         moveTo(loc);
    }
}

ChameleonCritter.java
import info.gridworld.actor.Actor;
import info.gridworld.actor.Critter;
import info.gridworld.grid.Location;
import java.util.ArrayList;
/**
 * A ChameleonCritter takes on the color of neighboring actors as it moves through the grid.
 * The implementation of this class is testable on the AP CS A and AB Exams.
 */
public class ChameleonCritter extends Critter
{
  /**
   * Randomly selects a neighbor and changes this critter’s color to be the same as that neighbor’s.
   * If there are no neighbors, no action is taken.
   */
  public void processActors(ArrayList<Actor> actors)
  {
    int n = actors.size();
    if (n == 0)
       return;
    int r = (int) (Math.random() * n);

        Actor other = actors.get(r);
        setColor(other.getColor());
    }

    /**
     * Turns towards the new location as it moves.
     */
    public void makeMove(Location loc)
    {
      setDirection(getLocation().getDirectionToward(loc));
      super.makeMove(loc);
    }
}




                                                           -C6-
Appendix E                                                  GridWorld Quick Reference


               Appendix E — GridWorld Quick Reference

Location Class (implements Comparable)
public   Location(int r, int c)
public   int getRow()
public   int getCol()
public   Location getAdjacentLocation(int direction)
public   int getDirectionToward(Location target)
public   boolean equals(Object other)
public   int hashCode()
public   int compareTo(Object other)
public   String toString()
NORTH, EAST, SOUTH, WEST, NORTHEAST, SOUTHEAST, NORTHWEST, SOUTHWEST
LEFT, RIGHT, HALF_LEFT, HALF_RIGHT, FULL_CIRCLE, HALF_CIRCLE, AHEAD




Grid<E> Interface
int getNumRows()
int getNumCols()
boolean isValid(Location loc)
E put(Location loc, E obj)
E remove(Location loc)
E get(Location loc)
ArrayList<Location> getOccupiedLocations()
ArrayList<Location> getValidAdjacentLocations(Location loc)
ArrayList<Location> getEmptyAdjacentLocations(Location loc)
ArrayList<Location> getOccupiedAdjacentLocations(Location loc)
ArrayList<E> getNeighbors(Location loc)




Actor Class
public   Actor()
public   Color getColor()
public   void setColor(Color newColor)
public   int getDirection()
public   void setDirection(int newDirection)
public   Grid<Actor> getGrid()
public   Location getLocation()
public   void putSelfInGrid(Grid<Actor> gr, Location loc)
public   void removeSelfFromGrid()
public   void moveTo(Location newLocation)
public   void act()
public   String toString()




                                         -E1-
Appendix E                                                GridWorld Quick Reference


Rock Class (extends Actor)
public Rock()
public Rock(Color rockColor)
public void act()




Flower Class (extends Actor)
public Flower()
public Flower(Color initialColor)
public void act()




Bug Class (extends Actor)
public   Bug()
public   Bug(Color bugColor)
public   void act()
public   void turn()
public   void move()
public   boolean canMove()




BoxBug Class (extends Bug)
public BoxBug(int n)
public void act()



Critter Class (extends Actor)
public   void act()
public   ArrayList<Actor> getActors()
public   void processActors(ArrayList<Actor> actors)
public   ArrayList<Location> getMoveLocations()
public   Location selectMoveLocation(ArrayList<Location> locs)
public   void makeMove(Location loc)




ChameleonCritter Class (extends Critter)
public void processActors(ArrayList<Actor> actors)
public void makeMove(Location loc)




                                        -E2-
Appendix G                                                                  Source Code Index


                   Appendix G — Index for Source Code
This appendix provides an index for the Java source code found in Appendix C.

Bug.java
Bug()                                                                                      C1
Bug(Color bugColor)                                                                        C1
act()                                                                                      C1
turn()                                                                                     C1
move()                                                                                     C2
canMove()                                                                                  C2


BoxBug.java
BoxBug(int length)                                                                         C3
act()                                                                                      C3


Critter.java
act()                                                                                      C4
getActors()                                                                                C4
processActors(ArrayList<Actor> actors)                                                     C5
getMoveLocations()                                                                         C5
selectMoveLocation(ArrayList<Location> locs)                                               C5
makeMove(Location loc)                                                                     C6


ChameleonCritter.java
processActors(ArrayList<Actor> actors)                                                     C6
makeMove(Location loc)                                                                     C6




                                              -G1-

Contenu connexe

Tendances

Programming in Scala: Notes
Programming in Scala: NotesProgramming in Scala: Notes
Programming in Scala: NotesRoberto Casadei
 
Scalaz 8 vs Akka Actors
Scalaz 8 vs Akka ActorsScalaz 8 vs Akka Actors
Scalaz 8 vs Akka ActorsJohn De Goes
 
Functional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorldFunctional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorldJorge Vásquez
 
An Introduction to Functional Programming - DeveloperUG - 20140311
An Introduction to Functional Programming - DeveloperUG - 20140311An Introduction to Functional Programming - DeveloperUG - 20140311
An Introduction to Functional Programming - DeveloperUG - 20140311Andreas Pauley
 
Why we cannot ignore Functional Programming
Why we cannot ignore Functional ProgrammingWhy we cannot ignore Functional Programming
Why we cannot ignore Functional ProgrammingMario Fusco
 
An introduction to property based testing
An introduction to property based testingAn introduction to property based testing
An introduction to property based testingScott Wlaschin
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with ScalaDenis
 
"Java 8, Lambda e la programmazione funzionale" by Theodor Dumitrescu
"Java 8, Lambda e la programmazione funzionale" by Theodor Dumitrescu"Java 8, Lambda e la programmazione funzionale" by Theodor Dumitrescu
"Java 8, Lambda e la programmazione funzionale" by Theodor DumitrescuThinkOpen
 
Learning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a NeckbeardLearning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a NeckbeardKelsey Gilmore-Innis
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parametersKnoldus Inc.
 
Beyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckBeyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckFranklin Chen
 
Integrating Cloud Services in Behaviour Programming for Autonomous Robots
Integrating Cloud Services in Behaviour  Programming for Autonomous RobotsIntegrating Cloud Services in Behaviour  Programming for Autonomous Robots
Integrating Cloud Services in Behaviour Programming for Autonomous RobotsCorrado Santoro
 

Tendances (20)

Programming in Scala: Notes
Programming in Scala: NotesProgramming in Scala: Notes
Programming in Scala: Notes
 
Scalaz 8 vs Akka Actors
Scalaz 8 vs Akka ActorsScalaz 8 vs Akka Actors
Scalaz 8 vs Akka Actors
 
Clojure basics
Clojure basicsClojure basics
Clojure basics
 
Functional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorldFunctional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorld
 
Lec2
Lec2Lec2
Lec2
 
Lec2
Lec2Lec2
Lec2
 
An Introduction to Functional Programming - DeveloperUG - 20140311
An Introduction to Functional Programming - DeveloperUG - 20140311An Introduction to Functional Programming - DeveloperUG - 20140311
An Introduction to Functional Programming - DeveloperUG - 20140311
 
MTL Versus Free
MTL Versus FreeMTL Versus Free
MTL Versus Free
 
Why we cannot ignore Functional Programming
Why we cannot ignore Functional ProgrammingWhy we cannot ignore Functional Programming
Why we cannot ignore Functional Programming
 
An introduction to property based testing
An introduction to property based testingAn introduction to property based testing
An introduction to property based testing
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with Scala
 
"Java 8, Lambda e la programmazione funzionale" by Theodor Dumitrescu
"Java 8, Lambda e la programmazione funzionale" by Theodor Dumitrescu"Java 8, Lambda e la programmazione funzionale" by Theodor Dumitrescu
"Java 8, Lambda e la programmazione funzionale" by Theodor Dumitrescu
 
Scala functions
Scala functionsScala functions
Scala functions
 
Java 8: the good parts!
Java 8: the good parts!Java 8: the good parts!
Java 8: the good parts!
 
Linq intro
Linq introLinq intro
Linq intro
 
Learning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a NeckbeardLearning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a Neckbeard
 
E6
E6E6
E6
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parameters
 
Beyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckBeyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheck
 
Integrating Cloud Services in Behaviour Programming for Autonomous Robots
Integrating Cloud Services in Behaviour  Programming for Autonomous RobotsIntegrating Cloud Services in Behaviour  Programming for Autonomous Robots
Integrating Cloud Services in Behaviour Programming for Autonomous Robots
 

Similaire à NEW AP Computer Science Exam GridWorld Quick Reference Booklet

K is for Kotlin
K is for KotlinK is for Kotlin
K is for KotlinTechMagic
 
Java Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfJava Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfNiravPanchal50
 
Hive Functions Cheat Sheet
Hive Functions Cheat SheetHive Functions Cheat Sheet
Hive Functions Cheat SheetHortonworks
 
Read carefully. Im not sure if the point class is correct but postin.pdf
Read carefully. Im not sure if the point class is correct but postin.pdfRead carefully. Im not sure if the point class is correct but postin.pdf
Read carefully. Im not sure if the point class is correct but postin.pdfbharatchawla141
 
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018Codemotion
 
Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)Gesh Markov
 
Power of functions in a typed world
Power of functions in a typed worldPower of functions in a typed world
Power of functions in a typed worldDebasish Ghosh
 
GeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetGeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetJose Perez
 
Google Guava for cleaner code
Google Guava for cleaner codeGoogle Guava for cleaner code
Google Guava for cleaner codeMite Mitreski
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional ProgrammingEelco Visser
 
Lecture20 vector
Lecture20 vectorLecture20 vector
Lecture20 vectornurkhaledah
 
Linked list (java platform se 8 )
Linked list (java platform se 8 )Linked list (java platform se 8 )
Linked list (java platform se 8 )charan kumar
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondMario Fusco
 

Similaire à NEW AP Computer Science Exam GridWorld Quick Reference Booklet (20)

Lec3
Lec3Lec3
Lec3
 
K is for Kotlin
K is for KotlinK is for Kotlin
K is for Kotlin
 
Collection and framework
Collection and frameworkCollection and framework
Collection and framework
 
Java Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfJava Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdf
 
Lec3
Lec3Lec3
Lec3
 
Vectors,squence & list
Vectors,squence & listVectors,squence & list
Vectors,squence & list
 
Hive Functions Cheat Sheet
Hive Functions Cheat SheetHive Functions Cheat Sheet
Hive Functions Cheat Sheet
 
Read carefully. Im not sure if the point class is correct but postin.pdf
Read carefully. Im not sure if the point class is correct but postin.pdfRead carefully. Im not sure if the point class is correct but postin.pdf
Read carefully. Im not sure if the point class is correct but postin.pdf
 
Collections and generics
Collections and genericsCollections and generics
Collections and generics
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
 
Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)
 
Power of functions in a typed world
Power of functions in a typed worldPower of functions in a typed world
Power of functions in a typed world
 
GeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetGeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheet
 
Google Guava for cleaner code
Google Guava for cleaner codeGoogle Guava for cleaner code
Google Guava for cleaner code
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional Programming
 
Scala for curious
Scala for curiousScala for curious
Scala for curious
 
Lecture20 vector
Lecture20 vectorLecture20 vector
Lecture20 vector
 
Linked list (java platform se 8 )
Linked list (java platform se 8 )Linked list (java platform se 8 )
Linked list (java platform se 8 )
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
 

Plus de A Jorge Garcia

MAT122 DAY508 MEETING 44 of 45 2021.1217 FRIDAY
MAT122 DAY508 MEETING 44 of 45 2021.1217 FRIDAYMAT122 DAY508 MEETING 44 of 45 2021.1217 FRIDAY
MAT122 DAY508 MEETING 44 of 45 2021.1217 FRIDAYA Jorge Garcia
 
MAT122 DAY507 MEETING 43 of 45 2021.1216 THURSDAY
MAT122 DAY507 MEETING 43 of 45 2021.1216 THURSDAYMAT122 DAY507 MEETING 43 of 45 2021.1216 THURSDAY
MAT122 DAY507 MEETING 43 of 45 2021.1216 THURSDAYA Jorge Garcia
 
MAT122 DAY506 MEETING 42 of 45 2021.1215 WEDNESDAY
MAT122 DAY506 MEETING 42 of 45 2021.1215 WEDNESDAYMAT122 DAY506 MEETING 42 of 45 2021.1215 WEDNESDAY
MAT122 DAY506 MEETING 42 of 45 2021.1215 WEDNESDAYA Jorge Garcia
 
MAT122 DAY308 Lesson 26 of 45
MAT122 DAY308 Lesson 26 of 45MAT122 DAY308 Lesson 26 of 45
MAT122 DAY308 Lesson 26 of 45A Jorge Garcia
 
MAT122 DAY307 Lesson 25 of 45
MAT122 DAY307 Lesson 25 of 45MAT122 DAY307 Lesson 25 of 45
MAT122 DAY307 Lesson 25 of 45A Jorge Garcia
 
MAT122 DAY306 Lesson 24 of 45
MAT122 DAY306 Lesson 24 of 45MAT122 DAY306 Lesson 24 of 45
MAT122 DAY306 Lesson 24 of 45A Jorge Garcia
 
MAT122 DAY305 Lesson 23 of 45
MAT122 DAY305 Lesson 23 of 45MAT122 DAY305 Lesson 23 of 45
MAT122 DAY305 Lesson 23 of 45A Jorge Garcia
 
MAT122 DAY304 Lesson 22 of 45
MAT122 DAY304 Lesson 22 of 45MAT122 DAY304 Lesson 22 of 45
MAT122 DAY304 Lesson 22 of 45A Jorge Garcia
 
MAT122 DAY303 Lesson 21 of 45
MAT122 DAY303 Lesson 21 of 45MAT122 DAY303 Lesson 21 of 45
MAT122 DAY303 Lesson 21 of 45A Jorge Garcia
 
MAT122 DAY302 Lesson 20 of 45
MAT122 DAY302 Lesson 20 of 45MAT122 DAY302 Lesson 20 of 45
MAT122 DAY302 Lesson 20 of 45A Jorge Garcia
 
MAT122 DAY301 Lesson 19 of 45
MAT122 DAY301 Lesson 19 of 45MAT122 DAY301 Lesson 19 of 45
MAT122 DAY301 Lesson 19 of 45A Jorge Garcia
 

Plus de A Jorge Garcia (20)

LIMACON 2023 Brochure
LIMACON 2023 BrochureLIMACON 2023 Brochure
LIMACON 2023 Brochure
 
2022-RESUME-NEW
2022-RESUME-NEW2022-RESUME-NEW
2022-RESUME-NEW
 
MAT122 DAY508 MEETING 44 of 45 2021.1217 FRIDAY
MAT122 DAY508 MEETING 44 of 45 2021.1217 FRIDAYMAT122 DAY508 MEETING 44 of 45 2021.1217 FRIDAY
MAT122 DAY508 MEETING 44 of 45 2021.1217 FRIDAY
 
MAT122 DAY507 MEETING 43 of 45 2021.1216 THURSDAY
MAT122 DAY507 MEETING 43 of 45 2021.1216 THURSDAYMAT122 DAY507 MEETING 43 of 45 2021.1216 THURSDAY
MAT122 DAY507 MEETING 43 of 45 2021.1216 THURSDAY
 
MAT122 DAY506 MEETING 42 of 45 2021.1215 WEDNESDAY
MAT122 DAY506 MEETING 42 of 45 2021.1215 WEDNESDAYMAT122 DAY506 MEETING 42 of 45 2021.1215 WEDNESDAY
MAT122 DAY506 MEETING 42 of 45 2021.1215 WEDNESDAY
 
MAT122 DAY308 Lesson 26 of 45
MAT122 DAY308 Lesson 26 of 45MAT122 DAY308 Lesson 26 of 45
MAT122 DAY308 Lesson 26 of 45
 
MAT122 DAY307 Lesson 25 of 45
MAT122 DAY307 Lesson 25 of 45MAT122 DAY307 Lesson 25 of 45
MAT122 DAY307 Lesson 25 of 45
 
MAT122 DAY306 Lesson 24 of 45
MAT122 DAY306 Lesson 24 of 45MAT122 DAY306 Lesson 24 of 45
MAT122 DAY306 Lesson 24 of 45
 
MAT122 DAY305 Lesson 23 of 45
MAT122 DAY305 Lesson 23 of 45MAT122 DAY305 Lesson 23 of 45
MAT122 DAY305 Lesson 23 of 45
 
MAT122 DAY304 Lesson 22 of 45
MAT122 DAY304 Lesson 22 of 45MAT122 DAY304 Lesson 22 of 45
MAT122 DAY304 Lesson 22 of 45
 
MAT122 DAY303 Lesson 21 of 45
MAT122 DAY303 Lesson 21 of 45MAT122 DAY303 Lesson 21 of 45
MAT122 DAY303 Lesson 21 of 45
 
MAT122 DAY302 Lesson 20 of 45
MAT122 DAY302 Lesson 20 of 45MAT122 DAY302 Lesson 20 of 45
MAT122 DAY302 Lesson 20 of 45
 
MAT122 DAY301 Lesson 19 of 45
MAT122 DAY301 Lesson 19 of 45MAT122 DAY301 Lesson 19 of 45
MAT122 DAY301 Lesson 19 of 45
 
MAT122 DAY205
MAT122 DAY205MAT122 DAY205
MAT122 DAY205
 
MAT122 DAY204
MAT122 DAY204MAT122 DAY204
MAT122 DAY204
 
MAT122 DAY203
MAT122 DAY203MAT122 DAY203
MAT122 DAY203
 
MAT122 DAY202
MAT122 DAY202MAT122 DAY202
MAT122 DAY202
 
MAT122 DAY201
MAT122 DAY201MAT122 DAY201
MAT122 DAY201
 
MAT122 DAY06
MAT122 DAY06MAT122 DAY06
MAT122 DAY06
 
MAT122 DAY05
MAT122 DAY05MAT122 DAY05
MAT122 DAY05
 

Dernier

31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleCeline George
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsPooky Knightsmith
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 

Dernier (20)

31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP Module
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 

NEW AP Computer Science Exam GridWorld Quick Reference Booklet

  • 1. Quick Reference AP® Computer Science A © 2011 The College Board. Visit the College Board on the Web: www.collegeboard.org.
  • 2. Content of Appendixes Appendix A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Java Quick Reference Appendix B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testable API Appendix C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testable Code Appendix E . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . GridWorld Quick Reference Appendix G . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Index for Source Code i
  • 3. Appendix A Java Quick Reference Appendix A — Java Quick Reference Accessible Methods from the Java Library That May Be Included on the Exam class java.lang.Object • boolean equals(Object other) • String toString() class java.lang.Integer • Integer(int value) • int intValue() • Integer.MIN_VALUE // minimum value represented by an int or Integer • Integer.MAX_VALUE // maximum value represented by an int or Integer class java.lang.Double • Double(double value) • double doubleValue() class java.lang.String • int length() • String substring(int from, int to) // returns the substring beginning at from // and ending at to-1 • String substring(int from) // returns substring(from, length()) • int indexOf(String str) // returns the index of the first occurrence of str; // returns -1 if not found • int compareTo(String other) // returns a value < 0 if this is less than other // returns a value = 0 if this is equal to other // returns a value > 0 if this is greater than other class java.lang.Math • static int abs(int x) • static double abs(double x) • static double pow(double base, double exponent) • static double sqrt(double x) • static double random() // returns a double in the range [0.0, 1.0) interface java.util.List<E> • int size() • boolean add(E obj) // appends obj to end of list; returns true • void add(int index, E obj) // inserts obj at position index (0 ≤ index ≤ size) , // moving elements at position index and higher // to the right (adds 1 to their indices) and adjusts size • E get(int index) • E set(int index, E obj) // replaces the element at position index with obj // returns the element formerly at the specified position • E remove(int index) // removes element from position index, moving elements // at position index + 1 and higher to the left // (subtracts 1 from their indices) and adjusts size // returns the element formerly at the specified position class java.util.ArrayList<E> implements java.util.List<E> -A1-
  • 4. Appendix B Testable API Appendix B — Testable API info.gridworld.grid.Location class (implements Comparable) public Location(int r, int c) constructs a location with given row and column coordinates public int getRow() returns the row of this location public int getCol() returns the column of this location public Location getAdjacentLocation(int direction) returns the adjacent location in the direction that is closest to direction public int getDirectionToward(Location target) returns the closest compass direction from this location toward target public boolean equals(Object other) returns true if other is a Location with the same row and column as this location; false otherwise public int hashCode() returns a hash code for this location public int compareTo(Object other) returns a negative integer if this location is less than other, zero if the two locations are equal, or a positive integer if this location is greater than other. Locations are ordered in row-major order. Precondition: other is a Location object. public String toString() returns a string with the row and column of this location, in the format (row, col) Compass directions: public static final int NORTH = 0; public static final int EAST = 90; public static final int SOUTH = 180; public static final int WEST = 270; public static final int NORTHEAST = 45; public static final int SOUTHEAST = 135; public static final int SOUTHWEST = 225; public static final int NORTHWEST = 315; Turn angles: public static final int LEFT = -90; public static final int RIGHT = 90; public static final int HALF_LEFT = -45; public static final int HALF_RIGHT = 45; public static final int FULL_CIRCLE = 360; public static final int HALF_CIRCLE = 180; public static final int AHEAD = 0; -B1-
  • 5. Appendix B Testable API info.gridworld.grid.Grid<E> interface int getNumRows() returns the number of rows, or -1 if this grid is unbounded int getNumCols() returns the number of columns, or -1 if this grid is unbounded boolean isValid(Location loc) returns true if loc is valid in this grid, false otherwise Precondition: loc is not null E put(Location loc, E obj) puts obj at location loc in this grid and returns the object previously at that location (or null if the location was previously unoccupied). Precondition: (1) loc is valid in this grid (2) obj is not null E remove(Location loc) removes the object at location loc from this grid and returns the object that was removed (or null if the location is unoccupied) Precondition: loc is valid in this grid E get(Location loc) returns the object at location loc (or null if the location is unoccupied) Precondition: loc is valid in this grid ArrayList<Location> getOccupiedLocations() returns an array list of all occupied locations in this grid ArrayList<Location> getValidAdjacentLocations(Location loc) returns an array list of the valid locations adjacent to loc in this grid Precondition: loc is valid in this grid ArrayList<Location> getEmptyAdjacentLocations(Location loc) returns an array list of the valid empty locations adjacent to loc in this grid Precondition: loc is valid in this grid ArrayList<Location> getOccupiedAdjacentLocations(Location loc) returns an array list of the valid occupied locations adjacent to loc in this grid Precondition: loc is valid in this grid ArrayList<E> getNeighbors(Location loc) returns an array list of the objects in the occupied locations adjacent to loc in this grid Precondition: loc is valid in this grid -B2-
  • 6. Appendix B Testable API info.gridworld.actor.Actor class public Actor() constructs a blue actor that is facing north public Color getColor() returns the color of this actor public void setColor(Color newColor) sets the color of this actor to newColor public int getDirection() returns the direction of this actor, an angle between 0 and 359 degrees public void setDirection(int newDirection) sets the direction of this actor to the angle between 0 and 359 degrees that is equivalent to newDirection public Grid<Actor> getGrid() returns the grid of this actor, or null if this actor is not contained in a grid public Location getLocation() returns the location of this actor, or null if this actor is not contained in a grid public void putSelfInGrid(Grid<Actor> gr, Location loc) puts this actor into location loc of grid gr. If there is another actor at loc, it is removed. Precondition: (1) This actor is not contained in a grid. (2) loc is valid in gr. public void removeSelfFromGrid() removes this actor from its grid Precondition: this actor is contained in a grid public void moveTo(Location newLocation) moves this actor to newLocation. If there is another actor at newLocation, it is removed. Precondition: (1) This actor is contained in a grid. (2) newLocation is valid in the grid of this actor. public void act() reverses the direction of this actor. Override this method in subclasses of Actor to define types of actors with different behavior. public String toString() returns a string with the location, direction, and color of this actor -B3-
  • 7. Appendix B Testable API info.gridworld.actor.Rock class (extends Actor) public Rock() constructs a black rock public Rock(Color rockColor) constructs a rock with color rockColor public void act() overrides the act method in the Actor class to do nothing info.gridworld.actor.Flower class (extends Actor) public Flower() constructs a pink flower public Flower(Color initialColor) constructs a flower with color initialColor public void act() causes the color of this flower to darken -B4-
  • 8. Appendix C Bug.java Appendix C — Testable Code Bug.java package info.gridworld.actor; import info.gridworld.grid.Grid; import info.gridworld.grid.Location; import java.awt.Color; /** * A Bug is an actor that can move and turn. It drops flowers as it moves. * The implementation of this class is testable on the AP CS A and AB Exams. */ public class Bug extends Actor { /** * Constructs a red bug. */ public Bug() { setColor(Color.RED); } /** * Constructs a bug of a given color. * @param bugColor the color for this bug */ public Bug(Color bugColor) { setColor(bugColor); } /** * Moves if it can move, turns otherwise. */ public void act() { if (canMove()) move(); else turn(); } /** * Turns the bug 45 degrees to the right without changing its location. */ public void turn() { setDirection(getDirection() + Location.HALF_RIGHT); } -C1-
  • 9. Appendix C Bug.java /** * Moves the bug forward, putting a flower into the location it previously occupied. */ public void move() { Grid<Actor> gr = getGrid(); if (gr == null) return; Location loc = getLocation(); Location next = loc.getAdjacentLocation(getDirection()); if (gr.isValid(next)) moveTo(next); else removeSelfFromGrid(); Flower flower = new Flower(getColor()); flower.putSelfInGrid(gr, loc); } /** * Tests whether this bug can move forward into a location that is empty or contains a flower. * @return true if this bug can move. */ public boolean canMove() { Grid<Actor> gr = getGrid(); if (gr == null) return false; Location loc = getLocation(); Location next = loc.getAdjacentLocation(getDirection()); if (!gr.isValid(next)) return false; Actor neighbor = gr.get(next); return (neighbor == null) || (neighbor instanceof Flower); // ok to move into empty location or onto flower // not ok to move onto any other actor } } -C2-
  • 10. Appendix C BoxBug.java BoxBug.java import info.gridworld.actor.Bug; /** * A BoxBug traces out a square “box” of a given size. * The implementation of this class is testable on the AP CS A and AB Exams. */ public class BoxBug extends Bug { private int steps; private int sideLength; /** * Constructs a box bug that traces a square of a given side length. * @param length the side length */ public BoxBug(int length) { steps = 0; sideLength = length; } /** * Moves to the next location of the square. */ public void act() { if (steps < sideLength && canMove()) { move(); steps++; } else { turn(); turn(); steps = 0; } } } -C3-
  • 11. Appendix C Critter.java Critter.java package info.gridworld.actor; import info.gridworld.grid.Location; import java.util.ArrayList; /** * A Critter is an actor that moves through its world, processing * other actors in some way and then moving to a new location. * Define your own critters by extending this class and overriding any methods of this class except for act. * When you override these methods, be sure to preserve the postconditions. * The implementation of this class is testable on the AP CS A and AB Exams. */ public class Critter extends Actor { /** * A critter acts by getting a list of other actors, processing that list, getting locations to move to, * selecting one of them, and moving to the selected location. */ public void act() { if (getGrid() == null) return; ArrayList<Actor> actors = getActors(); processActors(actors); ArrayList<Location> moveLocs = getMoveLocations(); Location loc = selectMoveLocation(moveLocs); makeMove(loc); } /** * Gets the actors for processing. Implemented to return the actors that occupy neighboring grid locations. * Override this method in subclasses to look elsewhere for actors to process. * Postcondition: The state of all actors is unchanged. * @return a list of actors that this critter wishes to process */ public ArrayList<Actor> getActors() { return getGrid().getNeighbors(getLocation()); } -C4-
  • 12. Appendix C Critter.java /** * Processes the elements of actors. New actors may be added to empty locations. * Implemented to “eat” (i.e., remove) selected actors that are not rocks or critters. * Override this method in subclasses to process actors in a different way. * Postcondition: (1) The state of all actors in the grid other than this critter and the * elements of actors is unchanged. (2) The location of this critter is unchanged. * @param actors the actors to be processed */ public void processActors(ArrayList<Actor> actors) { for (Actor a : actors) { if (!(a instanceof Rock) && !(a instanceof Critter)) a.removeSelfFromGrid(); } } /** * Gets a list of possible locations for the next move. These locations must be valid in the grid of this critter. * Implemented to return the empty neighboring locations. Override this method in subclasses to look * elsewhere for move locations. * Postcondition: The state of all actors is unchanged. * @return a list of possible locations for the next move */ public ArrayList<Location> getMoveLocations() { return getGrid().getEmptyAdjacentLocations(getLocation()); } /** * Selects the location for the next move. Implemented to randomly pick one of the possible locations, * or to return the current location if locs has size 0. Override this method in subclasses that * have another mechanism for selecting the next move location. * Postcondition: (1) The returned location is an element of locs, this critter’s current location, or null. * (2) The state of all actors is unchanged. * @param locs the possible locations for the next move * @return the location that was selected for the next move */ public Location selectMoveLocation(ArrayList<Location> locs) { int n = locs.size(); if (n == 0) return getLocation(); int r = (int) (Math.random() * n); return locs.get(r); } -C5-
  • 13. Appendix C Critter.java /** * Moves this critter to the given location loc, or removes this critter from its grid if loc is null. * An actor may be added to the old location. If there is a different actor at location loc, that actor is * removed from the grid. Override this method in subclasses that want to carry out other actions * (for example, turning this critter or adding an occupant in its previous location). * Postcondition: (1) getLocation() == loc. * (2) The state of all actors other than those at the old and new locations is unchanged. * @param loc the location to move to */ public void makeMove(Location loc) { if (loc == null) removeSelfFromGrid(); else moveTo(loc); } } ChameleonCritter.java import info.gridworld.actor.Actor; import info.gridworld.actor.Critter; import info.gridworld.grid.Location; import java.util.ArrayList; /** * A ChameleonCritter takes on the color of neighboring actors as it moves through the grid. * The implementation of this class is testable on the AP CS A and AB Exams. */ public class ChameleonCritter extends Critter { /** * Randomly selects a neighbor and changes this critter’s color to be the same as that neighbor’s. * If there are no neighbors, no action is taken. */ public void processActors(ArrayList<Actor> actors) { int n = actors.size(); if (n == 0) return; int r = (int) (Math.random() * n); Actor other = actors.get(r); setColor(other.getColor()); } /** * Turns towards the new location as it moves. */ public void makeMove(Location loc) { setDirection(getLocation().getDirectionToward(loc)); super.makeMove(loc); } } -C6-
  • 14. Appendix E GridWorld Quick Reference Appendix E — GridWorld Quick Reference Location Class (implements Comparable) public Location(int r, int c) public int getRow() public int getCol() public Location getAdjacentLocation(int direction) public int getDirectionToward(Location target) public boolean equals(Object other) public int hashCode() public int compareTo(Object other) public String toString() NORTH, EAST, SOUTH, WEST, NORTHEAST, SOUTHEAST, NORTHWEST, SOUTHWEST LEFT, RIGHT, HALF_LEFT, HALF_RIGHT, FULL_CIRCLE, HALF_CIRCLE, AHEAD Grid<E> Interface int getNumRows() int getNumCols() boolean isValid(Location loc) E put(Location loc, E obj) E remove(Location loc) E get(Location loc) ArrayList<Location> getOccupiedLocations() ArrayList<Location> getValidAdjacentLocations(Location loc) ArrayList<Location> getEmptyAdjacentLocations(Location loc) ArrayList<Location> getOccupiedAdjacentLocations(Location loc) ArrayList<E> getNeighbors(Location loc) Actor Class public Actor() public Color getColor() public void setColor(Color newColor) public int getDirection() public void setDirection(int newDirection) public Grid<Actor> getGrid() public Location getLocation() public void putSelfInGrid(Grid<Actor> gr, Location loc) public void removeSelfFromGrid() public void moveTo(Location newLocation) public void act() public String toString() -E1-
  • 15. Appendix E GridWorld Quick Reference Rock Class (extends Actor) public Rock() public Rock(Color rockColor) public void act() Flower Class (extends Actor) public Flower() public Flower(Color initialColor) public void act() Bug Class (extends Actor) public Bug() public Bug(Color bugColor) public void act() public void turn() public void move() public boolean canMove() BoxBug Class (extends Bug) public BoxBug(int n) public void act() Critter Class (extends Actor) public void act() public ArrayList<Actor> getActors() public void processActors(ArrayList<Actor> actors) public ArrayList<Location> getMoveLocations() public Location selectMoveLocation(ArrayList<Location> locs) public void makeMove(Location loc) ChameleonCritter Class (extends Critter) public void processActors(ArrayList<Actor> actors) public void makeMove(Location loc) -E2-
  • 16. Appendix G Source Code Index Appendix G — Index for Source Code This appendix provides an index for the Java source code found in Appendix C. Bug.java Bug() C1 Bug(Color bugColor) C1 act() C1 turn() C1 move() C2 canMove() C2 BoxBug.java BoxBug(int length) C3 act() C3 Critter.java act() C4 getActors() C4 processActors(ArrayList<Actor> actors) C5 getMoveLocations() C5 selectMoveLocation(ArrayList<Location> locs) C5 makeMove(Location loc) C6 ChameleonCritter.java processActors(ArrayList<Actor> actors) C6 makeMove(Location loc) C6 -G1-