收藏 分享(赏)

AP计算机科学A考试附录.pdf

上传人:精品资料 文档编号:11227725 上传时间:2020-02-21 格式:PDF 页数:16 大小:178.58KB
下载 相关 举报
AP计算机科学A考试附录.pdf_第1页
第1页 / 共16页
AP计算机科学A考试附录.pdf_第2页
第2页 / 共16页
AP计算机科学A考试附录.pdf_第3页
第3页 / 共16页
AP计算机科学A考试附录.pdf_第4页
第4页 / 共16页
AP计算机科学A考试附录.pdf_第5页
第5页 / 共16页
点击查看更多>>
资源描述

1、 2011 The College Board. Visit the College Board on the Web: www.collegeboard.org. Quick Reference AP Computer Science A i Content of Appendixes Appendix A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Java Quick Reference Appendix B . . . . . . . . . . . . . . .

2、. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testable API Appendix C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testable Code Appendix E . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . GridWorld Quick Ref

3、erence Appendix G . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Index for Source Code Appendix A Java Quick Reference -A1- Appendix A Java Quick Reference Accessible Methods from the Java Library That May Be Included on the Exam class java.lang.Object boolean equa

4、ls(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 ja

5、va.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 compar

6、eTo(String 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

7、.List 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 size) index , / 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)

8、 / 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 elem

9、ent formerly at the specified position class java.util.ArrayList implements java.util.List Appendix B Testable API -B1- 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

10、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 comp

11、ass 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 inte

12、ger 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 lo

13、cation, 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

14、 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_CI

15、RCLE = 180; public static final int AHEAD = 0; Appendix B Testable API -B2- info.gridworld.grid.Grid 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) re

16、turns 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

17、(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 unoc

18、cupied) Precondition: loc is valid in this grid ArrayList getOccupiedLocations() returns an array list of all occupied locations in this grid ArrayList getValidAdjacentLocations(Location loc) returns an array list of the valid locations adjacent to loc in this grid Precondition: loc is valid in this

19、 grid ArrayList 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 getOccupiedAdjacentLocations(Location loc) returns an array list of the valid occupied locations adjacent to loc i

20、n this grid Precondition: loc is valid in this grid ArrayList 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 Appendix B Testable API -B3- info.gridworld.actor.Actor class public Actor() co

21、nstructs 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(

22、int newDirection) sets the direction of this actor to the angle between 0 and 359 degrees that is equivalent to newDirection public Grid 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 nul

23、l if this actor is not contained in a grid public void putSelfInGrid(Grid 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()

24、 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 gr

25、id 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 Appendix B Testable API -B4- info.g

26、ridworld.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

27、 a pink flower public Flower(Color initialColor) constructs a flower with color initialColor public void act() causes the color of this flower to darken Appendix C Bug.java -C1- Appendix C Testable Code Bug.java package info.gridworld.actor; import info.gridworld.grid.Grid; import info.gridworld.gri

28、d.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); /* * Construct

29、s 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

30、void turn() setDirection(getDirection() + Location.HALF_RIGHT); Appendix C Bug.java -C2- /* * Moves the bug forward, putting a flower into the location it previously occupied. */ public void move() Grid gr = getGrid(); if (gr = null) return; Location loc = getLocation(); Location next = loc.getAdjac

31、entLocation(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. */ publ

32、ic boolean canMove() Grid 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 e

33、mpty location or onto flower / not ok to move onto any other actor Appendix C BoxBug.java -C3- 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 exte

34、nds 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 sideLeng

35、th steps+; else turn(); turn(); steps = 0; Appendix C Critter.java -C4- 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

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

37、 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 actors = getActors(); processActors(actors); ArrayList moveLocs

38、 = 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

39、 all actors is unchanged. * return a list of actors that this critter wishes to process */ public ArrayList getActors() return getGrid().getNeighbors(getLocation(); Appendix C Critter.java -C5- /* * Processes the elements of actors. New actors may be added to empty locations. * Implemented to “eat”

40、(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 un

41、changed. * param actors the actors to be processed */ public void processActors(ArrayList actors) for (Actor a : actors) if (!(a instanceof Rock) /* * 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 nei

42、ghboring 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 getMoveLocations() return getGrid().getEmptyAdjacentLocations(getLocation(

43、); /* * 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

44、 location is an element of locs, this critters 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 locs) int n = locs.siz

45、e(); if (n = 0) return getLocation(); int r = (int) (Math.random() * n); return locs.get(r); Appendix C Critter.java -C6- /* * 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 acto

46、r 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 t

47、hose 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.L

48、ocation; 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 change

49、s this critters color to be the same as that neighbors. * If there are no neighbors, no action is taken. */ public void processActors(ArrayList 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 makeM

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报