Ap-CSA-Mock-FRQ
Ap-CSA-Mock-FRQ
AP-CSA-Mock-FRQ
1. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• 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.
• 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 will not receive full credit.
This question involves simulation of the play and scoring of a single-player video game. In the game, a player
attempts to complete three levels. A level in the game is represented by the Level class.
Play of the game is represented by the Game class. You will write two methods of the Game class.
/** Returns true if this game is a bonus game and returns false
otherwise */
public boolean isBonus()
{ /* implementation not shown */ }
/** Simulates the play of this Game (consisting of three levels) and
updates all relevant
* game data
AP-CSA-Mock-FRQ
*/
public void play()
{ /* implementation not shown */ }
/** Returns the score earned in the most recently played game, as
described in part (a) */
public int getScore()
{ /* to be implemented in part (a) */ }
/** Simulates the play of num games and returns the highest score
earned, as
* described in part (b)
* Precondition: num > 0
*/
public int playManyTimes(int num)
{ /* to be implemented in part (b) */ }
Write the getScore method, which returns the score for the most recently played game. Each game consists of
three levels. The score for the game is computed using the following helper methods.
• The isBonus method of the Game class returns true if this is a bonus game and
returns false otherwise.
• The goalReached method of the Level class returns true if the goal has been reached on a
particular level and returns false otherwise.
• The getPoints method of the Level class returns the number of points recorded on a particular
level. Whether or not recorded points are earned (included in the game score) depends on the rules of the
game, which follow.
The score for the game is computed according to the following rules.
• Level one points are earned only if the level one goal is reached. Level two points are earned only if both
the level one and level two goals are reached. Level three points are earned only if the goals of all three
levels are reached.
• The score for the game is the sum of the points earned for levels one, two, and three.
• If the game is a bonus game, the score for the game is tripled.
AP-CSA-Mock-FRQ
/** Returns the score earned in the most recently played game, as
described in part (a) */
public int getScore()
Write the playManyTimes method, which simulates the play of num games and returns the highest game
score earned. For example, if the four plays of the game that are simulated as a result of the method
call playManyTimes(4) earn scores of 75, 50, 90, and 20, then the method should return 90.
Play of the game is simulated by calling the helper method play. Note that if play is called only one time
followed by multiple consecutive calls to getScore, each call to getScore will return the score earned in
the single simulated play of the game.
Complete the playManyTimes method. Assume that getScore works as intended, regardless of what you
wrote in part (a). You must call play and getScore appropriately in order to receive full credit.
/** Simulates the play of num games and returns the highest score earned,
as
* described in part (b)
* Precondition
Precondition: num > 0
*/
public int playManyTimes(int num)
AP-CSA-Mock-FRQ
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. indicates a point
earned and indicates a general or question-specific penalty.
· include parameters
· call incorrectly
[Skill 3.C] Guards update of score for bonus game based on return value
· call incorrectly
AP-CSA-Mock-FRQ
(w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
0 1 2 3 4
AP-CSA-Mock-FRQ
[penalty] (x) Local variables used but none declared (General Penalties)
Canonical Solution:
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. indicates a point
earned and indicates a general or question-specific penalty.
· return early
· include parameters
AP-CSA-Mock-FRQ
· call incorrectly
· assign a value to the identified maximum score without any loop or logic to find the maximum
(w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
AP-CSA-Mock-FRQ
max) Line 10: { Line 11: max = score; Line 12: } Line 13: } Line 14 is blank. Line 15: return max; Line 16 } end code">
0 1 2 3 4 5
Canonical Solution:
AP-CSA-Mock-FRQ
max) Line 10: { Line 11: max = score; Line 12: } Line 13: } Line 14 is blank. Line 15: return max; Line 16 } end code">
AP-CSA-Mock-FRQ
2. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• 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.
• 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 will not receive full credit.
The Book class is used to store information about a book. A partial Book class definition is shown.
/** Returns a string containing the title and price of the Book */
public String getBookInfo()
{
return title + "-" + price;
}
A Textbook has an edition number, which is a positive integer used to identify different versions of the book.
The getBookInfo method, when called on a Textbook, returns a string that also includes the edition
information, as shown in the example.
Information about the book title and price must be maintained in the Book class. Information about the edition
must be maintained in the Textbook class.
The Textbook class contains an additional method, canSubstituteFor, which returns true if
AP-CSA-Mock-FRQ
a Textbook is a valid substitute for another Textbook and returns false otherwise. The
current Textbook is a valid substitute for the Textbook referenced by the parameter of
the canSubstituteFor method if the two Textbook objects have the same title and if the edition of the
current Textbook is greater than or equal to the edition of the parameter.
The following table contains a sample code execution sequence and the corresponding results. The code execution
sequence appears in a class other than Book or Textbook.
AP-CSA-Mock-FRQ
Value Returned
(blank if no value)
bio2015 is
Textbook bio2015 = new a Textbook with a title
Textbook("Biology", 49.75, of "Biology", a price
2); of 49.75, and an edition
of 2.
bio2019 is
Textbook bio2019 = new a Textbook with a title
Textbook("Biology", 39.75, of "Biology", a price
3); of 39.75, and an edition
of 3.
bio2019.getEdition(); 3 The edition is returned.
The formatted string
containing the title, price,
bio2019.getBookInfo(); "Biology-39.75-3"
and edition of bio2019 is
returned.
bio2019 is a valid
substitute
for bio2015, since their
bio2019.
true titles are the same and the
canSubstituteFor(bio2015);
edition of bio2019 is
greater than or equal to the
edition of bio2015.
bio2015 is not a valid
substitute
bio2015. for bio2019, since the
false
canSubstituteFor(bio2019); edition of bio2015 is less
than the edition
of bio2019.
math is
Textbook math = new a Textbook with a title
Textbook("Calculus", of "Calculus", a price
45.25, 1); of 45.25, and an edition
of 1.
AP-CSA-Mock-FRQ
Write the complete Textbook class. Your implementation must meet all specifications and conform to the
examples shown in the preceding table.
Textbook (9 points)
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. indicates a point
earned and indicates a general or question-specific penalty.
[Skill 3.B] Constructor calls as the first line with the appropriate parameters
[Skill 3.B] Declares appropriate instance variable and uses appropriate parameter to initialize it
· declare the variable outside the class, or in the class within a method or constructor
[Skill 3.B] Declares at least one required method and all declared headers are correct:
· exclude
AP-CSA-Mock-FRQ
· fail to use
· include parameters
· call incorrectly
(w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
AP-CSA-Mock-FRQ
= other.getEdition(); Line 19: } Line 20 is blank. Line 21: public String getBookInfo() Line 22: { Line 23: return
super.getBookInfo() + "–" + edition; Line 24: } Line 25: } end code">
0 1 2 3 4 5 6 7 8 9
AP-CSA-Mock-FRQ
Canonical Solution:
= other.getEdition(); Line 19: } Line 20 is blank. Line 21: public String getBookInfo() Line 22: { Line 23: return
super.getBookInfo() + "–" + edition; Line 24: } Line 25: } end code">
AP-CSA-Mock-FRQ
A student plans to analyze product reviews found on a Web site by looking for keywords in posted reviews.
The ProductReview class, shown below, is used to represent a single review. A product review consists of a product name
and a review of that product.
The ReviewCollector class, shown below, is used to represent a collection of reviews to be analyzed.
AP-CSA-Mock-FRQ
AP-CSA-Mock-FRQ
3. (a) Write the addReview method, which adds a single product review, represented by
a ProductReview object, to the ReviewCollector object. The addReview method does the following
when it adds a product review.
(b) Write the getNumGoodReviews method, which returns the number of good reviews for a given product name.
A review is considered good if it contains the string "best" (all lowercase). If there are no reviews with a matching
product name, the method returns 0. Note that a review that contains "BEST" or "Best" is not considered a
good review (since not all the letters of "best" are lowercase), but a review that contains "asbestos" is
considered a good review (since all the letters of "best" are lowercase).
/** Returns the number of good reviews for a given product name, as
described in part (b). */
public int getNumGoodReviews(String prodName)
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
AP-CSA-Mock-FRQ
+1 [Skill 3.D] Correctly adds product name to if and only if the product name is not already in
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
AP-CSA-Mock-FRQ
0 1 2 3 4 5 6
Canonical Solution:
AP-CSA-Mock-FRQ
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
+1 [Skill 3.C] Selects all and only reviews with matching product names that contain
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
AP-CSA-Mock-FRQ
= 0) Line 10: { Line 11: numGoodReviews++; Line 12: } Line 13: } Line 14: } Line 15: return numGoodReviews; Line
16: }" title="">
0 1 2 3
Canonical Solution:
AP-CSA-Mock-FRQ
= 0) Line 10: { Line 11: numGoodReviews++; Line 12: } Line 13: } Line 14: } Line 15: return numGoodReviews; Line
16: }">
AP-CSA-Mock-FRQ
4. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• 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.
• 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 will not receive full credit.
This question involves pieces of candy in a box. The Candy class represents a single piece of candy.
The BoxOfCandy class represents a candy box where the candy is arranged in a rectangular grid. The instance
variable of the class, box, is a rectangular two-dimensional array of Candy objects. A location in the candy
box may contain a piece of candy or may be empty. A piece of candy is represented by a Candy object. An
empty location is represented by null.
/**
* Moves one piece of candy in column col, if necessary and
possible, so that the box
* element in row 0 of column col contains a piece of candy, as
described in part (a).
* Returns false if there is no piece of candy in column col and
returns true otherwise.
* Precondition: col is a valid column index in box.
*/
public boolean moveCandyToFirstRow(int col)
{ /* to be implemented in part (a) */ }
AP-CSA-Mock-FRQ
/**
* Removes from box and returns a piece of candy with flavor
specified by the parameter, or
* returns null if no such piece is found, as described in part (b)
*/
public Candy removeNextByFlavor(String flavor)
{ /* to be implemented in part (b) */ }
Write the moveCandyToFirstRow method, which attempts to ensure that the box element at row 0 and
column col contains a piece of candy, using the following steps.
If the element at row 0 and column col already contains a piece of candy, then box is unchanged and the
method returns true. If the element at row 0 and column col does not contain a piece of candy, then the
method searches the remaining rows of column col for a piece of candy. If a piece of candy can be found in
column col, it is moved to row 0, its previous location is set to null, and the method
returns true; otherwise, the method returns false.
In the following example, the grid represents the contents of box. An empty square in the grid
is null in box. A non-empty square in the grid represents a box element that contains a Candy object.
The string in the square of the grid indicates the flavor of the piece of candy.
The method call moveCandyToFirstRow(0) returns false because the box element at row 0 and
column 0 does not contain a piece of candy and there are no pieces of candy in column 0 that can be moved to
row 0. The contents of box are unchanged.
AP-CSA-Mock-FRQ
The method call moveCandyToFirstRow(1) returns true because the box element at row 0 and
column 1 already contains a piece of candy. The contents of box are unchanged.
The method call moveCandyToFirstRow(2) moves one of the two pieces of candy in column 2 to
row 0 of column 2, sets the previous location of the piece of candy that was moved to null, and
returns true. The new contents of box could be either of the following.
/**
* Moves one piece of candy in column col, if necessary and possible, so
that the box
* element in row 0 of column col contains a piece of candy, as described
in part (a).
* Returns false if there is no piece of candy in column col and returns
true otherwise.
* Precondition
Precondition: col is a valid column index in box.
*/
public boolean moveCandyToFirstRow(int col)
Write the removeNextByFlavor method, which attempts to remove and return one piece of candy from the
box. The piece of candy to be removed is the first piece of candy with a flavor equal to the
parameter flavor that is encountered while traversing the candy box in the following order: the last row of the
box is traversed from left to right, then the next-to-last row of the box is traversed from left to right, etc., until
either a piece of candy with the desired flavor is found or until the entire candy box has been searched.
If the removeNextByFlavor method finds a Candy object with the desired flavor, the
corresponding box element is assigned null, all other box elements are unchanged, and the
removed Candy object is returned. Otherwise, box is unchanged and the method returns null.
The following examples show three consecutive calls to the removeNextByFlavor method. The traversal of
the candy box always begins in the last row and first column of the box.
The following grid shows the contents of box before any of the removeNextByFlavor method calls.
AP-CSA-Mock-FRQ
The method call removeNextByFlavor("cherry") removes and returns the Candy object located in
row 2 and column 0. The following grid shows the updated contents of box.
The method call removeNextByFlavor("lime") removes and returns the Candy object located in
row 1 and column 3. The following grid shows the updated contents of box.
AP-CSA-Mock-FRQ
/**
* Removes from box and returns a piece of candy with flavor specified by
the parameter, or
* returns null if no such piece is found, as described in part (b)
*/
public Candy removeNextByFlavor(String flavor)
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. indicates a point
earned and indicates a general or question-specific penalty.
[Skill 3.E] Accesses all necessary elements of column of (no bounds errors)
[Skill 3.C] Compares candy box element at row end code and column to
AP-CSA-Mock-FRQ
• make the comparison inside the loop or at some incorrect point in the code
[Skill 3.E] Identifies and moves appropriate object to first row if necessary (algorithm)
• return early, as long as the identify-and-move are inside a loop and would work if the loop got that far
[Skill 3.C] Returns when non-empty square is identified and if non-empty square is not identified in the
context of a loop (algorithm)
• fail to replace the moved object with incorrectly identify a non-empty square
• return early
Canonical Solution:
AP-CSA-Mock-FRQ
0 1 2 3 4
Accesses all necessary elements of column of (no bounds errors) (Points Earned)
Compares candy box element at row end code and column to (Points Earned)
Identifies and moves appropriate object to first row if necessary (algorithm) (Points
Earned)
Returns when non-empty square is identified and if non-empty square is not identified in
the context of a loop (algorithm) (Points Earned)
Canonical Solution:
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. indicates a point
earned and indicates a general or question-specific penalty.
[Skill 3.E] Traverses in specified order (bottom to top, left to right) (no bounds errors)
AP-CSA-Mock-FRQ
[Skill 3.C] Guards against a method call on a element of the candy box (in the context of an statement)
• include parameters
• fail to use
[Skill 3.E] Replaces first matching object with and returns replaced object (algorithm)
• fail to replace the identified object within the 2D array with before returning
Canonical Solution:
AP-CSA-Mock-FRQ
= 0; row--) Line 4: { Line 5: for (int col = 0; col < box[0].length; col++) Line 6: { Line 7: if (box[row][col] != null &&
box[row][col].getFlavor().equals(flavor)) Line 8: { Line 9: Candy taken = box[row][col]; Line 10: box[row][col] = null;
Line 11: return taken; Line 12: } Line 13: } Line 14: } Line 15: return null; Line 16: } end code">
0 1 2 3 4 5
Traverses in specified order (bottom to top, left to right) (no bounds errors) (Points Earned)
Guards against a method call on a element of the candy box (in the context of an statement)
(Points Earned)
Calls on a object (Points Earned)
Compares a object’s flavor with parameter (Points Earned)
Replaces first matching object with and returns replaced object (algorithm) (Points
Earned)
Canonical Solution:
AP-CSA-Mock-FRQ
= 0; row--) Line 4: { Line 5: for (int col = 0; col < box[0].length; col++) Line 6: { Line 7: if (box[row][col] != null &&
box[row][col].getFlavor().equals(flavor)) Line 8: { Line 9: Candy taken = box[row][col]; Line 10: box[row][col] = null;
Line 11: return taken; Line 12: } Line 13: } Line 14: } Line 15: return null; Line 16: } end code">