0% found this document useful (0 votes)
128 views34 pages

Ap-CSA-Mock-FRQ

The document is a scoring guide for an AP Computer Science A mock free-response question involving Java programming. It outlines the structure of classes for a video game simulation and a textbook, detailing methods to be implemented for scoring and substitution logic. The guide includes specific criteria for scoring responses, emphasizing the importance of method calls, logical conditions, and proper class structure.

Uploaded by

seed2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views34 pages

Ap-CSA-Mock-FRQ

The document is a scoring guide for an AP Computer Science A mock free-response question involving Java programming. It outlines the structure of classes for a video game simulation and a textbook, detailing methods to be implemented for scoring and substitution logic. The guide includes specific criteria for scoring responses, emphasizing the importance of method calls, logical conditions, and proper class structure.

Uploaded by

seed2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

AP COMPUTER SCIENCE A Scoring Guide

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.

public class Level


{
/** Returns true if the player reached the goal on this level and
returns false otherwise */
public boolean goalReached()
{ /* implementation not shown */ }

/** Returns the number of points (a positive integer) recorded for


this level */
public int getPoints()
{ /* implementation not shown */ }

// There may be instance variables, constructors, and methods that


are not shown.
}

Play of the game is represented by the Game class. You will write two methods of the Game class.

public class Game


{
private Level levelOne;
private Level levelTwo;
private Level levelThree;

/** Postcondition: All instance variables have been initialized. */


public Game()
{ /* implementation not shown */ }

/** 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 Computer Science A Page 1 of 34


Scoring Guide

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) */ }

// There may be instance variables, constructors, and methods that


are not shown.
}

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.

The following table shows some examples of game score calculations.

Page 2 of 34 AP Computer Science A


Scoring Guide

AP-CSA-Mock-FRQ

Complete the getScore method.

/** 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)

Part A – getScore (4 points)

AP Computer Science A Page 3 of 34


Scoring Guide

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.

[Skill 3.A] Calls , , and

Responses will not earn the point if they

· fail to call or on a object

· call on an object other than (use of is optional)

· include parameters

[Skill 3.C] Determines if points are earned based on return values

Responses can still earn the point even if they

· calculate the score total incorrectly

· call incorrectly

· fail to distinguish all cases correctly

Responses will not earn the point if they

· fail to use a nested statement or equivalent

[Skill 3.C] Guards update of score for bonus game based on return value

Responses can still earn the point even if they

· triple the calculated score incorrectly

· update the score with something other than tripling

· call incorrectly

Responses will not earn the point if they

· use the return value incorrectly

[Skill 3.C] Initializes and accumulates appropriate score (algorithm)

Responses can still earn the point even if they

· call methods incorrectly, as long as method calls are attempted

· fail to return the score (return is not assessed)

Responses will not earn the point if they

· calculate the score total incorrectly

· triple the calculated score incorrectly

Page 4 of 34 AP Computer Science A


Scoring Guide

AP-CSA-Mock-FRQ

(w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)

(x) Local variables used but none declared

Canonical Solution:

0 1 2 3 4

Total number of points earned (minus penalties) is equal to 4.

Calls , , and (Points Earned)


Determines if points are earned based on return values (Points Earned)
Guards update of score for bonus game based on return value (Points Earned)
Initializes and accumulates appropriate score (algorithm) (Points Earned)
[penalty] (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition
check) (General Penalties)

AP Computer Science A Page 5 of 34


Scoring Guide

AP-CSA-Mock-FRQ

[penalty] (x) Local variables used but none declared (General Penalties)

Canonical Solution:

Part B – playManyTimes (5 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.C] Loops times

Responses can still the point even if they

· return early

[Skill 3.A] Calls and

Responses will not earn the point if they

· call either method on an object other than (use of is optional)

· include parameters

Page 6 of 34 AP Computer Science A


Scoring Guide

AP-CSA-Mock-FRQ

[Skill 3.C] Compares a score to an identified max or to another score

Responses can still earn the point even if they

· make the comparison outside the loop

· call incorrectly

· fail to call between calls to

[Skill 3.C] Identifies the maximum score (algorithm)

Responses will not earn the point if they

· fail to initialize the result variable

· compare a score to an identified max or to another score outside the loop

· fail to call exactly once each time through the loop

[Skill 3.B] Returns identified maximum score

Responses can still earn the point even if they

· calculate the maximum score incorrectly

Responses will not earn the point if they

· 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)

(x) Local variables used but none declared

Canonical Solution:

AP Computer Science A Page 7 of 34


Scoring Guide

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

Total number of points earned (minus penalties) is equal to 5.

Loops times (Points Earned)


Calls and (Points Earned)
Compares a score to an identified max or to another score (Points Earned)
Identifies the maximum score (algorithm) (Points Earned)
Returns identified maximum score (Points Earned)
[penalty] (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition
check) (General Penalties)
[penalty] (x) Local variables used but none declared (General Penalties)

Canonical Solution:

Page 8 of 34 AP Computer Science A


Scoring Guide

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 Computer Science A Page 9 of 34


Scoring Guide

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.

public class Book


{
/** The title of the book */
private String title;

/** The price of the book */


private double price;

/** Creates a new Book with given title and price */


public Book(String bookTitle, double bookPrice)
{ /* implementation not shown */ }

/** Returns the title of the book */


public String getTitle()
{ return title; }

/** Returns a string containing the title and price of the Book */
public String getBookInfo()
{
return title + "-" + price;
}

// There may be instance variables, constructors, and methods that


are not shown.
}

You will write a class Textbook, which is a subclass of Book.

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

Page 10 of 34 AP Computer Science A


Scoring Guide

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 Computer Science A Page 11 of 34


Scoring Guide

AP-CSA-Mock-FRQ

Value Returned

Statement Class Specification

(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.

Page 12 of 34 AP Computer Science A


Scoring Guide

AP-CSA-Mock-FRQ

bio2015 is not a valid


substitute for math, since
bio2015.
false the title of bio2015 is not
canSubstituteFor(math);
the same as the title
of math.

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] Declares class header (must not be ):

[Skill 3.B] Declares constructor header:

[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

Responses will not earn the point if they

· omit the keyword

· declare the variable outside the class, or in the class within a method or constructor

· redeclare and use the instance variables of the superclass

[Skill 3.B] Declares at least one required method and all declared headers are correct:

Responses will not earn the point if they

· exclude

[Skill 3.B] returns value of instance variable

Responses will not earn the point if they call

· fail to create an instance variable for the edition

[Skill 3.C] determines whether or should be returned based on comparison of book


titles and editions (algorithm)

AP Computer Science A Page 13 of 34


Scoring Guide

AP-CSA-Mock-FRQ

Responses can still earn the point even if they

· fail to return (return is not assessed for this method)

· access the edition without calling

· redeclare and use the variable of the superclass instead of calling

Responses will not earn the point if they

· fail to use

· call incorrectly in either case

[Skill 3.A] calls

Responses can still earn the point even if they

· redeclare and use the instance variables of the superclass

Responses will not earn the point if they

· include parameters

[Skill 3.C] Constructs information string

Responses can still earn the point even if they

· call incorrectly

· fail to call and access and directly

· fail to return (return is not assessed for this method)

Responses will not earn the point if they

· omit the literal hyphen(s) in the constructed string

· omit the edition in the constructed string

· concatenate strings incorrectly

(w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)

(x) Local variables used but none declared

(y) Destruction of persistent data (e.g., changing value referenced by parameter)

(z) Void method or constructor that returns a value

Canonical Solution:

Page 14 of 34 AP Computer Science A


Scoring Guide

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

Total number of points earned (minus penalties) is equal to 9.

Declares class header (must not be ): (Points Earned)


Declares constructor header: (Points
Earned)
Constructor calls as the first line with the appropriate parameters (Points Earned)
Declares appropriate instance variable and uses appropriate parameter to initialize (Points
Earned)
Declares at least one required method and all declared headers are correct: (Points Earned)

AP Computer Science A Page 15 of 34


Scoring Guide

AP-CSA-Mock-FRQ

returns value of instance variable


determines whether or should be returned based on comparison of
book titles and editions (algorithm) (Points Earned)
calls (Points Earned)
Constructs information string (Points Earned)
[penalty] (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition
check) (General Penalties)
[penalty] (x) Local variables used but none declared (General Penalties)
[penalty] (y) Destruction of persistent data (e.g., changing value referenced by parameter) (General
Penalties)
[penalty] (z) Void method or constructor that returns a value (General Penalties)

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">

Page 16 of 34 AP Computer Science A


Scoring Guide

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.

public class ProductReview


{
private String name;
private String review;
/** Constructs a ProductReview object and initializes the instance
variables. */
public ProductReview(String pName, String pReview)
{
name = pName;
review = pReview;
}
/** Returns the name of the product. */
public String getName()
{ return name; }
/** Returns the review of the product. */
public String getReview()
{ return review; }
}

The ReviewCollector class, shown below, is used to represent a collection of reviews to be analyzed.

public class ReviewCollector


{
private ArrayList<ProductReview> reviewList;
private ArrayList<String> productList;
/** Constructs a ReviewCollector object and initializes the instance
variables. */
public ReviewCollector()
{
reviewList = new ArrayList<ProductReview>();
productList = new ArrayList<String>();
}
/** Adds a new review to the collection of reviews, as described in part
(a). */
public void addReview(ProductReview prodReview)
{ /* to be implemented in part (a) */ }
/** Returns the number of good reviews for a given product name, as
described in part (b). */
public int getNumGoodReviews(String prodName)
{ /* to be implemented in part (b) */ }

AP Computer Science A Page 17 of 34


Scoring Guide

AP-CSA-Mock-FRQ

// There may be instance variables, constructors, and methods not shown.


}

Page 18 of 34 AP Computer Science A


Scoring Guide

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.

• The ProductReview object is added to the reviewList instance variable.


• The product name from the ProductReview object is added to the productList instance
variable if the product name is not already found in productList.

Elements may be added to reviewList and productList in any order.

Complete method addReview.

/** Adds a new review to the collection of reviews, as described in part


(a). */
public void addReview(ProductReview prodReview)

(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).

Complete method getNumGoodReviews.

/** Returns the number of good reviews for a given product name, as
described in part (b). */
public int getNumGoodReviews(String prodName)

Class information for this question

public class ProductReview


private String name
private String review
public ProductReview(String pName, String pReview)
public String getName()
public String getReview()
public class ReviewCollector
private ArrayList<ProductReview> reviewList
private ArrayList<String> productList
public ReviewCollector()
public void addReview(ProductReview prodReview)
public int getNumGoodReviews(String prodName)

Part A - addReview method

Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point

AP Computer Science A Page 19 of 34


Scoring Guide

AP-CSA-Mock-FRQ

earned and -1 indicates a general or question-specific penalty.

+1 [Skill 3.D] Adds a object to

Responses still earn the point even if they...

· add a object other than the one referenced by the parameter .

+1 [Skill 3.A] Gets product name of review to be added

+1 [Skill 3.D] Traverses (no bounds errors)

Responses earn the point if they...

· use a , an enhanced , or a loop.

+1 [Skill 3.C] Compares name in with name from review to be added

Responses still earn the point even if they...

· use an incorrectly accessed value for either name.

+1 [Skill 3.D] Adds new product name to

Responses still earn the point even if they...

· add the new product name under the wrong conditions; or

· add an incorrectly accessed value for the new product name.

+1 [Skill 3.D] Correctly adds product name to if and only if the product name is not already in

-1 (v) Array/collection access confusion ([] get)

-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)

-1 (x) Local variables used but none declared

-1 (y) Destruction of persistent data (e.g., changing value referenced by parameter)

-1 (z) Void method or constructor that returns a value

Canonical Solution:

Page 20 of 34 AP Computer Science A


Scoring Guide

AP-CSA-Mock-FRQ

0 1 2 3 4 5 6

Total number of points earned (minus penalties) is equal to 6.

+1 Adds a object to (Points earned)


+1 Gets product name of review to be added (Points earned)
+1 Traverses (no bounds errors) (Points earned)
+1 Compares name in with name from review to be added (Points earned)
+1 Adds new product name to (Points earned)
+1 Correctly adds product name to if and only if the product name is not already in
(Points earned)
-1 [penalty] (v) Array/collection access confusion ([] get) (General penalties)
-1 [penalty] (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition
check) (General penalties)
-1 [penalty] (x) Local variables used but none declared (General penalties)
-1 [penalty] (y) Destruction of persistent data (e.g., changing value referenced by parameter) (General
penalties)
-1 [penalty] (z) Void method or constructor that returns a value (General penalties)

Canonical Solution:

AP Computer Science A Page 21 of 34


Scoring Guide

AP-CSA-Mock-FRQ

Part B – getNumGoodReviews method

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.D] Traverses (no bounds errors)

Responses earn the point if they...

· use a , an enhanced , or a loop.

+1 [Skill 3.C] Selects all and only reviews with matching product names that contain

+1 [Skill 3.D] Returns correct count of good reviews

-1 (v) Array/collection access confusion ([] get)

-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)

-1 (x) Local variables used but none declared

-1 (y) Destruction of persistent data (e.g., changing value referenced by parameter)

Canonical Solution:

Page 22 of 34 AP Computer Science A


Scoring Guide

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

Total number of points earned (minus penalties) is equal to 3.

+1 Traverses (no bounds errors) (Points earned)


+1 Selects all and only reviews with matching product names that contain (Points earned)
+1 Returns correct count of good reviews (Points earned)
-1 [penalty] (v) Array/collection access confusion ([] get) (General penalties)
-1 [penalty] (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition
check) (General penalties)
-1 [penalty] (x) Local variables used but none declared (General penalties)
-1 [penalty] (y) Destruction of persistent data (e.g., changing value referenced by parameter) (General
penalties)

Canonical Solution:

AP Computer Science A Page 23 of 34


Scoring Guide

AP-CSA-Mock-FRQ

= 0) Line 10: { Line 11: numGoodReviews++; Line 12: } Line 13: } Line 14: } Line 15: return numGoodReviews; Line
16: }">

Page 24 of 34 AP Computer Science A


Scoring Guide

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.

public class Candy


{
/** Returns a String representing the flavor of this piece of candy
*/
public String getFlavor()
{ /* implementation not shown */ }

// There may be instance variables, constructors, and methods that


are not shown.
}

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.

You will write two methods of the BoxOfCandy class.

public class BoxOfCandy


{
/** box contains at least one row and is initialized in the
constructor. */
private Candy[][] box;

/**
* 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 Computer Science A Page 25 of 34


Scoring Guide

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) */ }

// There may be instance variables, constructors, and methods that


are not shown.
}

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.

Page 26 of 34 AP Computer Science A


Scoring Guide

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.

Complete the moveCandyToFirstRow method.

/**
* 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 Computer Science A Page 27 of 34


Scoring Guide

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.

Page 28 of 34 AP Computer Science A


Scoring Guide

AP-CSA-Mock-FRQ

The method call removeNextByFlavor("grape") returns null because no grape-flavored candy is


found. The contents of box are unchanged.

Complete the removeNextByFlavor method.

/**
* 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)

Part A – moveCandyToFirstRow (4 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.E] Accesses all necessary elements of column of (no bounds errors)

Responses can still earn the point even if they

• return early, as long as the loop bounds are appropriate

Responses will not earn the point if they

• fail to access an element of in the loop

• access the elements of incorrectly

[Skill 3.C] Compares candy box element at row end code and column to

Responses can still earn the point even if they

AP Computer Science A Page 29 of 34


Scoring Guide

AP-CSA-Mock-FRQ

• make the comparison inside the loop or at some incorrect point in the code

Responses will not earn the point if they

• fail to use or equivalent

[Skill 3.E] Identifies and moves appropriate object to first row if necessary (algorithm)

Responses can still earn the point even if they

• return early, as long as the identify-and-move are inside a loop and would work if the loop got that far

Responses will not earn the point if they

• fail to replace the moved object with

• move or swap objects when the first row is already occupied

[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)

Responses can still earn the point even if they

• fail to replace the moved object with incorrectly identify a non-empty square

Responses will not earn the point if they

• return early

Canonical Solution:

Page 30 of 34 AP Computer Science A


Scoring Guide

AP-CSA-Mock-FRQ

0 1 2 3 4

Total number of points earned (minus penalties) is equal to 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:

Part B – removeNextByFlavor (5 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.E] Traverses in specified order (bottom to top, left to right) (no bounds errors)

Responses will not earn the point if they

• fail to access an element of in the loop

• access the elements of incorrectly

AP Computer Science A Page 31 of 34


Scoring Guide

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)

Responses will not earn the point if they

• fail to use or equivalent

[Skill 3.A] Calls on a object

Responses can still earn the point even if they

• access the element of the candy box incorrectly

• call on the incorrect object

Responses will not earn the point if they

• call on an object of a different type or on the class

• attempt to create a object

• include parameters

[Skill 3.C] Compares a object’s flavor with parameter

Responses will not earn the point if they

• fail to use

[Skill 3.E] Replaces first matching object with and returns replaced object (algorithm)

Responses can still earn the point even if they

• access elements of the candy box incorrectly

• call incorrectly or on a object

• compare a object’s flavor to the parameter using

Responses will not earn the point if they

• fail to replace the identified object within the 2D array with before returning

• fail to return when no matching object is found

• set multiple elements to

Canonical Solution:

Page 32 of 34 AP Computer Science A


Scoring Guide

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

Total number of points earned (minus penalties) is equal to 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 Computer Science A Page 33 of 34


Scoring Guide

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">

Page 34 of 34 AP Computer Science A

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy