AP Computer Science A 2012 Free-Response Questions: About The College Board
AP Computer Science A 2012 Free-Response Questions: About The College Board
© 2012 The College Board. College Board, Advanced Placement Program, AP, AP Central, SAT, and the acorn logo are registered
trademarks of the College Board. Admitted Class Evaluation Service and inspiring minds are trademarks owned by the College
Board. All other products and services may be trademarks of their respective owners. Visit the College Board on the Web:
www.collegeboard.org. Permission to use copyrighted College Board materials may be requested online at:
www.collegeboard.org/inquiry/cbpermit.html.
COMPUTER SCIENCE A
SECTION II
Time— 1 hour and 45 minutes
Number of questions—4
Percent of total score—50
Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE
WRITTEN IN JAVA.
Notes:
x Assume that the classes listed in the appendices have been imported where appropriate.
x Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods
are called only when their preconditions are satisfied.
x In writing solutions for each question, you may use any of the accessible methods that are listed in classes
defined in that question. Writing significant amounts of code that can be replaced by a call to one of these
methods may not receive full credit.
1. A mountain climbing club maintains a record of the climbs that its members have made. Information about a
climb includes the name of the mountain peak and the amount of time it took to reach the top. The information is
contained in the ClimbInfo class as declared below.
// There may be instance variables, constructors, and methods that are not shown.
}
The ClimbingClub class maintains a list of the climbs made by members of the club. The declaration of the
ClimbingClub class is shown below. You will write two different implementations of the addClimb
method. You will also answer two questions about an implementation of the distinctPeakNames method.
/** Adds a new climb with name peakName and time climbTime to the list of climbs.
* @param peakName the name of the mountain peak climbed
* @param climbTime the number of minutes taken to complete the climb
*/
public void addClimb(String peakName, int climbTime)
{ /* to be implemented in part (a) with ClimbInfo objects in the order they were added */
/* to be implemented in part (b) with ClimbInfo objects in alphabetical order by name */
}
// There may be instance variables, constructors, and methods that are not shown.
}
(a) Write an implementation of the ClimbingClub method addClimb that stores the ClimbInfo
objects in the order they were added. This implementation of addClimb should create a new
ClimbInfo object with the given name and time. It appends a reference to that object to the end of
climbList. For example, consider the following code segment.
When the code segment has completed executing, the instance variable climbList would contain the
following entries.
/** Adds a new climb with name peakName and time climbTime to the list of climbs.
* @param peakName the name of the mountain peak climbed
* @param climbTime the number of minutes taken to complete the climb
* Postcondition: The new entry is at the end of climbList;
* The order of the remaining entries is unchanged.
*/
public void addClimb(String peakName, int climbTime)
(b) Write an implementation of the ClimbingClub method addClimb that stores the elements of
climbList in alphabetical order by name (as determined by the compareTo method of the String
class). This implementation of addClimb should create a new ClimbInfo object with the given name
and time and then insert the object into the appropriate position in climbList. Entries that have the
same name will be grouped together and can appear in any order within the group. For example, consider the
following code segment.
When the code segment has completed execution, the instance variable climbList would contain the
following entries in either of the orders shown below.
OR
You may assume that climbList is in alphabetical order by name when the method is called. When the
method has completed execution, climbList should still be in alphabetical order by name.
/** Adds a new climb with name peakName and time climbTime to the list of climbs.
* Alphabetical order is determined by the compareTo method of the String class.
* @param peakName the name of the mountain peak climbed
* @param climbTime the number of minutes taken to complete the climb
* Precondition: entries in climbList are in alphabetical order by name.
* Postcondition: entries in climbList are in alphabetical order by name.
*/
public void addClimb(String peakName, int climbTime)
(c) The ClimbingClub method distinctPeakNames is intended to return the number of different
names in climbList. For example, after the following code segment has completed execution, the value
of the variable numNames would be 3.
Assume that addClimb works as specified, regardless of what you wrote in parts (a) and (b).
(i) Does this implementation of the distinctPeakNames method work as intended when the
addClimb method stores the ClimbInfo objects in the order they were added as described in
part (a)?
Circle one of the answers below.
YES NO
(ii) Does this implementation of the distinctPeakNames method work as intended when the
addClimb method stores the ClimbInfo objects in alphabetical order by name as described in
part (b)?
Circle one of the answers below.
YES NO
2. This question involves reasoning about the GridWorld case study. Reference materials are provided in the
appendices.
A retro bug behaves like a regular bug. It also has the ability to revert to its previous location and direction.
When a retro bug acts, it maintains information about its location and direction at the beginning of the act. The
retro bug has a restore method that restores it to the location (if possible) and direction it faced at the
beginning of its previous act. A retro bug only maintains information about its most recent act; therefore,
multiple calls to restore that occur before its next act will use the same information. The restore
method has no effect if it is called before a retro bug’s first act.
The restore method takes no parameters and does not return a value. The restore method has the
following functionality.
If the previous location of the retro bug is either unoccupied or contains a flower, the restore method
places the retro bug in that previous location. The presence of any other type of actor in that location will
prevent the retro bug from being placed in that location.
The restore method always ends with the retro bug facing in the same direction that it had been facing
at the beginning of its most recent act.
Example 1
The retro bug acts once and later calls restore. Note that the flower that was originally in front of the retro
bug is not replaced as a result of the call to restore. The retro bug is returned to its previous direction,
which, in this case, is the same as the current direction.
Example 2
The retro bug acts once and then some other actor moves into the location that the retro bug originally held. The
call to restore results in the retro bug staying in its current location. The retro bug is returned to its previous
direction (in this case it is the same as the current direction).
Example 3
The retro bug acts once and later calls restore. Because the retro bug is blocked from moving forward, it
turns as its first act. The restore method results in the retro bug staying in its current location (the same as
its previous location) and returning to its previous direction.
Write the entire RetroBug class, including all necessary instance variables and methods.
3. Consider a software system that models a horse barn. Classes that represent horses implement the following
interface.
A horse barn consists of N numbered spaces. Each space can hold at most one horse. The spaces are indexed
starting from 0; the index of the last space is N – 1. No two horses in the barn have the same name.
The declaration of the HorseBarn class is shown below. You will write two unrelated methods of the
HorseBarn class.
/** Returns the index of the space that contains the horse with the specified name.
* Precondition: No two horses in the barn have the same name.
* @param name the name of the horse to find
* @return the index of the space containing the horse with the specified name;
* -1 if no horse with the specified name is in the barn.
*/
public int findHorseSpace(String name)
{ /* to be implemented in part (a) */ }
/** Consolidates the barn by moving horses so that the horses are in adjacent spaces,
* starting at index 0, with no empty space between any two horses.
* Postcondition: The order of the horses is the same as before the consolidation.
*/
public void consolidate()
{ /* to be implemented in part (b) */ }
// There may be instance variables, constructors, and methods that are not shown.
}
(a) Write the HorseBarn method findHorseSpace. This method returns the index of the space in
which the horse with the specified name is located. If there is no horse with the specified name in the barn,
the method returns -1.
For example, assume a HorseBarn object called sweetHome has horses in the following spaces.
0 1 2 3 4 5 6
The following table shows the results of several calls to the findHorseSpace method.
String getName()
int getWeight()
/** Returns the index of the space that contains the horse with the specified name.
* Precondition: No two horses in the barn have the same name.
* @param name the name of the horse to find
* @return the index of the space containing the horse with the specified name;
* -1 if no horse with the specified name is in the barn.
*/
public int findHorseSpace(String name)
(b) Write the HorseBarn method consolidate. This method consolidates the barn by moving horses so
that the horses are in adjacent spaces, starting at index 0, with no empty spaces between any two horses.
After the barn is consolidated, the horses are in the same order as they were before the consolidation.
For example, assume a barn has horses in the following spaces.
0 1 2 3 4 5 6
The following table shows the arrangement of the horses after consolidate is called.
0 1 2 3 4 5 6
String getName()
int getWeight()
/** Consolidates the barn by moving horses so that the horses are in adjacent spaces,
* starting at index 0, with no empty space between any two horses.
* Postcondition: The order of the horses is the same as before the consolidation.
*/
public void consolidate()
4. A grayscale image is represented by a 2-dimensional rectangular array of pixels (picture elements). A pixel is an
integer value that represents a shade of gray. In this question, pixel values can be in the range from 0 through
255, inclusive. A black pixel is represented by 0, and a white pixel is represented by 255.
The declaration of the GrayImage class is shown below. You will write two unrelated methods of the
GrayImage class.
/** Processes this image in row-major order and decreases the value of each pixel at
* position (row, col) by the value of the pixel at position (row + 2, col + 2) if it exists.
* Resulting values that would be less than BLACK are replaced by BLACK.
* Pixels for which there is no pixel at position (row + 2, col + 2) are unchanged.
*/
public void processImage()
{ /* to be implemented in part (b) */ }
}
(a) Write the method countWhitePixels that returns the number of pixels in the image that contain the
value WHITE. For example, assume that pixelValues contains the following image.
0 1 2 3 4
2 78 255 0 0 78
A call to countWhitePixels method would return 5 because there are 5 entries (shown in boldface)
that have the value WHITE.
(b) Write the method processImage that modifies the image by changing the values in the instance
variable pixelValues according to the following description. The pixels in the image are processed one
at a time in row-major order. Row-major order processes the first row in the array from left to right and then
processes the second row from left to right, continuing until all rows are processed from left to right. The
first index of pixelValues represents the row number, and the second index represents the column
number.
The pixel value at position (row, col) is decreased by the value at position (row + 2, col + 2) if such a
position exists. If the result of the subtraction is less than the value BLACK, the pixel is assigned the value
of BLACK. The values of the pixels for which there is no pixel at position (row + 2, col + 2) remain
unchanged. You may assume that all the original values in the array are within the range
[BLACK, WHITE], inclusive.
The following diagram shows the contents of the instance variable pixelValues before and after a call
to processImage. The values shown in boldface represent the pixels that could be modified in a
grayscale image with 4 rows and 5 columns.
0 1 2 3 4 0 1 2 3 4
2 78 255 0 0 78 2 78 255 0 0 78
/** Processes this image in row-major order and decreases the value of each pixel at
* position (row, col) by the value of the pixel at position (row + 2, col + 2) if it exists.
* Resulting values that would be less than BLACK are replaced by BLACK.
* Pixels for which there is no pixel at position (row + 2, col + 2) are unchanged.
*/
public void processImage()
STOP
END OF EXAM
-21-