Learn Extracted exam questions AP Computer Science A 2022 Free Response
2022 Free Response
Source PDF on the left, extracted YAML on the right. Compare numbering, marks, options and text.
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
*/
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 scored 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.
}
The following table shows some examples of game score calculations.
| goalReached Return Value: (Level One, Level Two, Level Three) | getPoints Return Value: (Level One, Level Two, Level Three) | isBonus Return Value | Score Calculation |
|---|---|---|---|
| true, true, true | 200, 100, 500 | true | $(200 + 100 + 500) \times 3 = 2{,}400$ — The recorded points for levels one, two, and three are added because the goals were reached in all three levels. The earned points are multiplied by 3 because isBonus returns true. |
| true, true, false | 200, 100, 500 | false | $200 + 100 = 300$ — The recorded points for level one and level two are added because the goal was reached in levels one and two. The recorded points for level three are not earned because the goal was not reached in level three. |
| true, false, true | 200, 100, 500 | true | $200 \times 3 = 600$ — The recorded points only for level one are earned because level two was not reached in level two. The earned points are multiplied by 3 because isBonus returns true. |
| false, true, false | 200, 100, 500 | false | 0 — Because the goal was not reached in level one, no points are earned for any level. |
[Table columns are headed "goalReached Return Value:", "getPoints Return Value:", "isBonus Return Value", and "Score Calculation"; each data row shows the Level One/Level Two/Level Three values stacked in the goalReached and getPoints columns.]
Class information for this question:
public class Level
public boolean goalReached()
public int getPoints()
public class Game
private Level levelOne
private Level levelTwo
private Level levelThree
public Game()
public boolean isBonus()
public void play()
public int getScore()
public int playManyTimes(int num)
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
isBonusmethod of theGameclass returnstrueif this is a bonus game and returnsfalseotherwise. - The
goalReachedmethod of theLevelclass returnstrueif the goal has been reached on a particular level and returnsfalseotherwise. - The
getPointsmethod of theLevelclass returns the number of points recorded on a particular level. Whether or not points are recorded (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.
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 plays num games and returns the highest score earned. For example, if the call playManyTimes(4) results in the simulated play of four games with score results of $75$, $50$, $90$, and $20$, then the method call playManyTimes(4) would return $90$.
Play of the game is simulated by calling the helper method play. Note that if play is called one time using consecutive calls to getScore, each call to getScore will return the score simulated by that call to play.
Complete the playManyTimes method that returns the highest score earned. 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 scored earned, as
* described in part (b)
* Precondition: num > 0
*/
public int playManyTimes(int num)
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 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.
| Statement | Value Returned (blank if no value) | Class Specification |
|---|---|---|
Textbook bio2015 = new Textbook("Biology", 49.75, 2); |
bio2015 is a Textbook with a title of "Biology", a price of 49.75, and an edition of 2. |
|
Textbook bio2019 = new Textbook("Biology", 39.75, 3); |
bio2019 is a Textbook with a title of "Biology", a price of 39.75, and an edition of 3. |
|
bio2019.getEdition(); |
3 |
The edition is returned. |
bio2019.getBookInfo(); |
"Biology-39.75-3" |
The formatted string containing the title, price, and edition of bio2019 is returned. |
bio2019.canSubstituteFor(bio2015); |
true |
bio2019 is a valid substitute for bio2015, since their titles are the same and the edition of bio2019 is greater than or equal to the edition of bio2015. |
bio2015.canSubstituteFor(bio2019); |
false |
bio2015 is not a valid substitute for bio2019, since the edition of bio2015 is less than the edition of bio2019. |
Textbook math = new Textbook("Calculus", 45.25, 1); |
math is a Textbook with a title of "Calculus", a price of 45.25, and an edition of 1. |
|
bio2015.canSubstituteFor(math); |
false |
bio2015 is not a valid substitute for math, since the title of bio2015 is not the same as math. |
Write the complete Textbook class. Your implementation must meet all specifications and conform to the examples shown in the preceding table.
Users of a website are asked to provide a review of the website at the end of each visit. Each review, represented by an object of the Review class, consists of an integer indicating the user's rating of the website and an optional String comment field. The comment field in a Review object ends with a period ("."), exclamation point ("!"), or letter, or is a String of length $0$ if the user did not enter a comment.
public class Review
{
private int rating;
private String comment;
/** Precondition: r >= 0
* c is not null.
*/
public Review(int r, String c)
{
rating = r;
comment = c;
}
public int getRating()
{
return rating;
}
public String getComment()
{
return comment;
}
// There may be instance variables, constructors, and methods that are not shown.
}
The ReviewAnalysis class contains methods used to analyze the reviews provided by users. You will write two methods of the ReviewAnalysis class.
public class ReviewAnalysis
{
/** All user reviews to be included in this analysis */
private Review[] allReviews;
/** Initializes allReviews to contain all the Review objects to be analyzed */
public ReviewAnalysis()
{ /* implementation not shown */ }
/** Returns a double representing the average rating of all the Review objects to be
* analyzed, as described in part (a)
* Precondition: allReviews contains at least one Review.
* No element of allReviews is null.
*/
public double getAverageRating()
{ /* to be implemented in part (a) */ }
/** Returns an ArrayList of String objects containing formatted versions of
* selected user comments, as described in part (b)
* Precondition: allReviews contains at least one Review.
* No element of allReviews is null.
* Postcondition: allReviews is unchanged.
*/
public ArrayList<String> collectComments()
{ /* to be implemented in part (b) */ }
}
Class information for this question:
public class Review
private int rating
private String comment
public Review(int r, String c)
public int getRating()
public String getComment()
public class ReviewAnalysis
private Review[] allReviews
public ReviewAnalysis()
public double getAverageRating()
public ArrayList<String> collectComments()
Write the ReviewAnalysis method getAverageRating, which returns the average rating (arithmetic mean) of all elements of allReviews. For example, getAverageRating would return $3.4$ if allReviews contained the following five Review objects.
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| rating | 4 | 3 | 5 | 2 | 3 |
| comment | "Good! Thx" | "OK site" | "Great!" | "Poor! Bad." | "" |
Complete method getAverageRating.
/** Returns a double representing the average rating of all the Review objects to be
* analyzed, as described in part (a)
* Precondition: allReviews contains at least one Review.
* No element of allReviews is null.
*/
public double getAverageRating()
Write the ReviewAnalysis method collectComments, which creates and formats only comments that contain an exclamation point. The method returns an ArrayList of String objects containing copies of user comments from allReviews that contain an exclamation point, formatted as follows. An empty ArrayList is returned if no element of allReviews contains an exclamation point.
- The
Stringinserted into theArrayListto be returned begins with the index of theReviewinallReviews. - The index is immediately followed by a hyphen ("-").
- The hyphen is followed by a copy of the original comment.
- The original comment from
allReviewsdoes not end with either a period or an exclamation point, a period is added.
The following example of allReviews is repeated from part (a).
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| rating | 4 | 3 | 5 | 2 | 3 |
| comment | "Good! Thx" | "OK site" | "Great!" | "Poor! Bad." | "" |
The following ArrayList would be returned by a call to collectComments with the given contents of allReviews. The reviews at index 1 and index 4 are not included in the ArrayList to return since neither review contains an exclamation point.
"0-Good! Thx.", "2-Great!", "3-Poor! Bad."
Complete method collectComments.
/** Returns an ArrayList of String objects containing formatted versions of
* selected user comments, as described in part (b)
* Precondition: allReviews contains at least one Review.
* No element of allReviews is null.
* Postcondition: allReviews is unchanged.
*/
public ArrayList<String> collectComments()
This question involves a two-dimensional array of integers that represents a collection of randomly generated data. A partial declaration of the Data class is shown. You will write two methods of the Data class.
public class Data
{
public static final int MAX = /* value not shown */;
private int[][] grid;
/** Fills all elements of grid with randomly generated values, as described in part (a)
* Precondition: grid is not null.
* grid has at least one element.
*/
public void repopulate()
{ /* to be implemented in part (a) */ }
/** Returns the number of columns in grid that are in increasing order, as described
* in part (b)
* Precondition: grid is not null.
* grid has at least one element.
*/
public int countIncreasingCols()
{ /* to be implemented in part (b) */ }
// There may be instance variables, constructors, and methods that are not shown.
}
Class information for this question:
public static final int MAX = /* value not shown */
private int[][] grid
public void repopulate()
public int countIncreasingCols()
Write the repopulate method, which assigns a newly generated random value to each element of grid. Each value is computed to meet all of the following criteria, and all valid values must have an equal chance of being generated.
- The value is between $1$ and
MAX, inclusive. - The value is divisible by $10$.
- The value is not divisible by $100$.
Complete the repopulate method.
/** Fills all elements of grid with randomly generated values, as described in part (a)
* Precondition: grid is not null.
* grid has at least one element.
*/
public void repopulate()
Write the countIncreasingCols method, which returns the number of columns in grid that are in increasing order. A column is considered to be in increasing order if the element in each row after the first row is greater than or equal to the element in the previous row. A column with only one row is considered to be increasing order.
The following examples show the countIncreasingCols return values for possible contents of grid.
The return value for the following contents of grid is $1$, since the first column is in increasing order but the second and third columns are not.
| 10 | 50 | 40 |
|---|---|---|
| 20 | 40 | 20 |
| 30 | 50 | 30 |
The return value for the following contents of grid is $2$, since the first and third columns are in increasing order but the second and fourth columns are not.
| 10 | 540 | 440 | 440 |
|---|---|---|---|
| 220 | 450 | 440 | 190 |
Complete the countIncreasingCols method.
/** Returns the number of columns in grid that are in increasing order, as described
* in part (b)
* Precondition: grid is not null.
* grid has at least one element.
*/
public int countIncreasingCols()