Quick Reference AP Computer Science A



Similar documents
Part 3: GridWorld Classes and Interfaces

GridWorld AP Computer Science Case Study. Solutions Manual

AP Computer Science A 2011 Free-Response Questions

Maria Litvin Phillips Academy, Andover, Massachusetts. Gary Litvin Skylight Publishing, Andover, Massachusetts

AP Computer Science A 2010 Free-Response Questions

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

AP Computer Science Java Subset

AP Computer Science A 2012 Scoring Guidelines

Computer Science III Advanced Placement G/T [AP Computer Science A] Syllabus

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

CS 111 Classes I 1. Software Organization View to this point:

AP Computer Science A 2004 Free-Response Questions

J a v a Quiz (Unit 3, Test 0 Practice)

Recursion and Recursive Backtracking

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Class 32: The Java Collections Framework

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

Java Programming Language

Overview. java.math.biginteger, java.math.bigdecimal. Definition: objects are everything but primitives The eight primitive data type in Java

CMSC 202H. ArrayList, Multidimensional Arrays

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

AP Computer Science A 2010 Scoring Guidelines

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Software Development with UML and Java 2 SDJ I2, Spring 2010

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

Stacks. Linear data structures

AP Computer Science A - Syllabus Overview of AP Computer Science A Computer Facilities

Data Structures and Algorithms

AP Computer Science Java Mr. Clausen Program 9A, 9B

Part I. Multiple Choice Questions (2 points each):

Iteration CHAPTER 6. Topic Summary

Easy-Cassandra User Guide

Chapter 13 - Inheritance

Example of a Java program

Java Interview Questions and Answers

CS193j, Stanford Handout #10 OOP 3

Overview. What is software testing? What is unit testing? Why/when to test? What makes a good test? What to test?

Contents. 9-1 Copyright (c) N. Afshartous

CS106A, Stanford Handout #38. Strings and Chars

1 of 1 24/05/ :23 AM

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

AP Computer Science A Picture Lab Student Guide

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

The Java Collections Framework

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 1 October 14, Instructions

Unit 6. Loop statements

Case studies: Outline. Requirement Engineering. Case Study: Automated Banking System. UML and Case Studies ITNP090 - Object Oriented Software Design

Software Engineering Techniques

1 Hour, Closed Notes, Browser open to Java API docs is OK

CS170 Lab 11 Abstract Data Types & Objects

CompSci-61B, Data Structures Final Exam

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

Licensed for viewing only. Printing is prohibited. For hard copies, please purchase from

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Collections in Java. Arrays. Iterators. Collections (also called containers) Has special language support. Iterator (i) Collection (i) Set (i),

Author: Sascha Wolski Sebastian Hennebrueder Tutorials for Struts, EJB, xdoclet and eclipse.

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Assignment 3 Version 2.0 Reactive NoSQL Due April 13

JDBC (Java / SQL Programming) CS 377: Database Systems

GridWorld Installation Guide

cs2010: algorithms and data structures

Microsoft Access 3: Understanding and Creating Queries

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Programming Database lectures for mathema

Number Representation

7.1 Our Current Model

OTN Developer Day Enterprise Java. Hands on Lab Manual JPA 2.0 and Object Relational Mapping Basics

REPAST JAVA GETTING STARTED

The resulting tile cannot merge with another tile again in the same move. When a 2048 tile is created, the player wins.

LOOPS CHAPTER CHAPTER GOALS

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Arrays in Java. Working with Arrays

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

The Interface Concept

Supporting Data Set Joins in BIRT

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

dictionary find definition word definition book index find relevant pages term list of page numbers

Introduction to Programming System Design. CSCI 455x (4 Units)

Java Programming Fundamentals

Sample CSE8A midterm Multiple Choice (circle one)

CmpSci 187: Programming with Data Structures Spring 2015

Grundlæggende Programmering IT-C, Forår Written exam in Introductory Programming

3 Improving the Crab more sophisticated programming

ICOM 4015: Advanced Programming

Lecture Notes on Linear Search

Two-Dimensional Arrays Summer 2010 Margaret Reid-Miller

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Transcription:

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. 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 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 causes the color of this flower to darken -B4-

Appendix C Bug.java Bug.java package info.gridworld.actor; import info.gridworld.grid.grid; import info.gridworld.grid.location; import java.awt.color; Appendix C Testable Code * 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. 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. 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. 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 String tostring() -E1-

Appendix E GridWorld Quick Reference Rock Class (extends Actor) public Rock() public Rock(Color rockcolor) Flower Class (extends Actor) public Flower() public Flower(Color initialcolor) Bug Class (extends Actor) public Bug() public Bug(Color bugcolor) public void turn() public void move() public boolean canmove() BoxBug Class (extends Bug) public BoxBug(int n) Critter Class (extends Actor) 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() Bug(Color bugcolor) act() turn() move() canmove() C1 C1 C1 C1 C2 C2 BoxBug.java BoxBug(int length) act() C3 C3 Critter.java act() getactors() processactors(arraylist<actor> actors) getmovelocations() selectmovelocation(arraylist<location> locs) makemove(location loc) C4 C4 C5 C5 C5 C6 ChameleonCritter.java processactors(arraylist<actor> actors) makemove(location loc) C6 C6 -G1-