Mr.Mou @ ShiShi AP Center

CSA 2024年真考 FRQ

Question 1 — BallGame

This question involves a game in which a player tries to hit a target by throwing a ball. You will write two static methods of the following BallGame class.

public class BallGame
{
    /**
     * Simulates a player throwing the ball in the game and returns a score,
     * as described in part (a)
     */
    public static int ballThrow()
    {
        /* to be implemented in part (a) */
    }

    /**
     * Simulates a player throwing the ball numThrows times and returns the average
     * of all throws with scores greater than minScore, as described in part (b)
     * Precondition: numThrows > 0
     */
    public static double averageThrow(int numThrows, int minScore)
    {
        /* to be implemented in part (b) */
    }

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

Part (a)

Write the ballThrow method, which simulates a player throwing a ball one time. The method returns a score of 10, 20, 30, 40, or 50. The score is randomly generated, with each score having an equal chance of being returned.

Complete method ballThrow.

/**
 * Simulates a player throwing the ball in the game and returns a score,
 * as described in part (a)
 */
public static int ballThrow()
{
    
}

Part (b)

Write the averageThrow method, which simulates a player throwing the ball numThrows times and returns the average of all throws with scores greater than minScore.

If none of the throws have a score greater than minScore, then the averageThrow method returns 0.0.

The following table represents four calls to averageThrow and the possible results.

Call to averageThrow Possible Results Generated by ballThrow Value Returned by averageThrow Explanation
BallGame.averageThrow(3, 10) 30, 40, 50 40.0 The average of 30, 40, and 50
BallGame.averageThrow(3, 20) 50, 10, 40 45.0 The average of 50 and 40
BallGame.averageThrow(4, 0) 10, 20, 10, 10 12.5 The average of 10, 20, 10, and 10
BallGame.averageThrow(4, 30) 20, 20, 30, 10 0.0 Since none of the throws have a score greater than 30, the averageThrow method returns 0.0.

Complete method averageThrow. Assume that ballThrow works as specified, regardless of what you wrote in part (a). You must use ballThrow appropriately to receive full credit.

/**
 * Simulates a player throwing the ball numThrows times and returns the average
 * of all throws with scores greater than minScore, as described in part (b)
 * Precondition: numThrows > 0
 */
public static double averageThrow(int numThrows, int minScore)
{
    
}

Question 2 — TopSecretWord

The SecretWord class is used to store and transform a string.

public class SecretWord
{
    /** The secret word */
    private String original;

    /**
     * Constructs a SecretWord object with the string word
     * Precondition: word is not an empty String.
     */
    public SecretWord(String word)
    {
        /* implementation not shown */
    }

    /** Returns original */
    public String getOriginal()
    {
        /* implementation not shown */
    }

    /**
     * Returns a scrambled version of original, which has the same length as original
     * Postcondition: original is unchanged.
     */
    public String transformWord()
    {
        /* implementation not shown */
    }
}

You will write a class TopSecretWord, which is a subclass of SecretWord. The TopSecretWord class further transforms the scrambled version of the original string based on the following rules, in a way that may change the length of the string.

The TopSecretWord class contains an additional method, checkLength, which returns true if the length of the result of the TopSecretWord transformation is greater than 5 and returns false otherwise.

The following table contains a sample code execution sequence and the corresponding results. The code execution sequence appears in a class other than SecretWord or TopSecretWord.

Statement Return Value
(blank if none)
Class Specification
SecretWord w1 = new SecretWord("Programs");   w1 is a SecretWord created with the string "Programs".
w1.transformWord(); "oPmrarsg" This scrambled string is one possible return value. The return value is always a string containing the scrambled letters of the original string.
w1.getOriginal(); "Programs" The original string is returned.
TopSecretWord w2 = new TopSecretWord("pixelated");   w2 is a TopSecretWord created with the string "pixelated".
w2.transformWord(); "xtai***" If the SecretWord transformed string is "xtaipdele", the return value is a string containing the first half of "xtaipdele" followed by "***", because the original word has an odd length.
w2.transformWord(); "tale***" If the SecretWord transformed string is "talexipde", the return value is a string containing the first half of "talexipde" followed by "***", because the original word has an odd length.
w2.getOriginal(); "pixelated" The original string is returned.
TopSecretWord w3 = new TopSecretWord("bird");   w3 is a TopSecretWord created with the string "bird".
w3.transformWord(); "***di" If the SecretWord transformed string is "rbdi", the return value is a string containing "***" followed by the second half of "rbdi", because the original word has an even length.
w2.checkLength(); true The length of the transformed word, 7, is greater than 5.
w3.checkLength(); false The length of the transformed word, 5, is not greater than 5.

Write the complete TopSecretWord class. Your implementation must meet all specifications and conform to the examples.

public class TopSecretWord extends SecretWord
{
    
}

Question 3 — ArrayChecker

This question involves reasoning about an array of integers in which there are no duplicate values. An inversion is a pair of values in an array in which a larger value appears before a smaller value.

In an array arr, (arr[j], arr[k]) is an inversion when arr[j] > arr[k] and j < k.

For example, in the array

{-3, 4, 2, 7, 1, 6}

there are five inversion pairs:

(4, 2), (4, 1), (2, 1), (7, 1), and (7, 6)

You will write two static methods of the following ArrayChecker class.

public class ArrayChecker
{
    /**
     * Returns a list of integers representing the inversion pairs from the array numbers,
     * as described in part (a)
     * Precondition: numbers.length >= 2 and numbers contains no duplicate values.
     */
    public static ArrayList<Integer> allInversions(int[] numbers)
    {
        /* to be implemented in part (a) */
    }

    /**
     * Returns a value from the array numbers that appears in the greatest number of
     * inversion pairs, as described in part (b)
     * Precondition: There is at least one inversion pair in numbers.
     */
    public static int valueWithMostInversions(int[] numbers)
    {
        /* to be implemented in part (b) */
    }

    /** Returns the number of occurrences of value in nums */
    public static int countOccur(ArrayList<Integer> nums, int value)
    {
        /* implementation not shown */
    }
}

Part (a)

Write the ArrayChecker method allInversions, which returns a list of integers representing the inversion pairs from the array numbers.

The two values in each inversion pair must be adjacent entries in the returned list, with the larger value appearing before the smaller value. The inversion pairs may appear in any order in the returned list, and each inversion pair appears exactly once in the returned list.

For example, assume that the following code segment is executed.

int[] sequence = {-3, 4, 2, 7, 1, 6};
ArrayList<Integer> inversions = ArrayChecker.allInversions(sequence);

The five inversion pairs identified from the data in the array sequence may appear in any order in the list inversions.

The following shows two possible orderings of the five inversion pairs and the corresponding contents of the list inversions.

  Possible Ordering inversions List to Be Returned
Ordering 1 (4, 2), (4, 1), (2, 1), (7, 1), (7, 6) [4, 2, 4, 1, 2, 1, 7, 1, 7, 6]
Ordering 2 (7, 6), (7, 1), (2, 1), (4, 1), (4, 2) [7, 6, 7, 1, 2, 1, 4, 1, 4, 2]

Complete method allInversions.

/**
 * Returns a list of integers representing the inversion pairs from the array numbers,
 * as described in part (a)
 * Precondition: numbers.length >= 2 and numbers contains no duplicate values.
 */
public static ArrayList<Integer> allInversions(int[] numbers)
{
    
}

Part (b)

Write the ArrayChecker method valueWithMostInversions, which returns a value from the array numbers that appears in the greatest number of inversion pairs.

If more than one value appears in the maximum number of inversion pairs, any one of those values may be returned.

For example, assume that the following code segment is executed.

int[] sequence = {-3, 4, 2, 7, 1, 6};
ArrayList<Integer> inversions = ArrayChecker.allInversions(sequence);

After executing the code segment, inversions contains

[4, 2, 4, 1, 2, 1, 7, 1, 7, 6]

The method call

ArrayChecker.valueWithMostInversions(sequence)

would return the value 1, because 1 appears the greatest number of times in inversions.

A helper method

countOccur(ArrayList<Integer> nums, int value)

has been provided for you. The method returns the number of times value occurs in nums.

Complete method valueWithMostInversions. Assume that allInversions works correctly regardless of what you wrote for part (a). You must use allInversions and countOccur appropriately to receive full credit.

/**
 * Returns a value from the array numbers that appears in the greatest number of inversion pairs,
 * as described in part (b)
 * Precondition: There is at least one inversion pair in numbers.
 */
public static int valueWithMostInversions(int[] numbers)
{
    
}

Question 4 — TreasureMap

A rectangular two-dimensional array named map represents an area in which treasures are hidden, and map[r][c] represents the element at row r and column c.

For example, in the map array shown below, elements containing treasures have a picture of a stack of coins, each of which represents an object of type Treasure.

Treasure objects are defined by the partial class shown below.

public class Treasure
{
    /**
     * Creates a Treasure object with a positive random number of gold coins
     */
    public Treasure()
    {
        /* implementation not shown */
    }

    /**
     * Returns the number of gold coins in this Treasure object
     */
    public int getGold()
    {
        /* implementation not shown */
    }

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

The Location class shown below represents a row and column coordinate.

public class Location
{
    /**
     * Creates a Location object with the specified row and column
     */
    public Location(int row, int col)
    {
        /* implementation not shown */
    }

    /**
     * Returns the row index of this location
     */
    public int getRow()
    {
        /* implementation not shown */
    }

    /**
     * Returns the column index of this location
     */
    public int getCol()
    {
        /* implementation not shown */
    }

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

A treasure map is represented by the following TreasureMap class. You will write the constructor and the method totalGold in the TreasureMap class.

public class TreasureMap
{
    /** The map */
    private Treasure[][] map;

    /**
     * Constructs a treasure map, as described in part (a)
     * Precondition: r > 0, c > 0, and the size of locs is at least 1 and less than r * c.
     *     All locations in locs are valid on the map, and there are no duplicates.
     * Postcondition: map contains r rows and c columns.
     *     map contains Treasure objects at each location in locs.
     *     All other map elements are null.
     */
    public TreasureMap(int r, int c, ArrayList<Location> locs)
    {
        /* to be implemented in part (a) */
    }

    /**
     * Returns the total number of gold coins found in all map locations within the
     * rectangular region defined by start and end, as described in part (b)
     * Precondition: start and end are valid locations in map.
     *     The row index of start is less than or equal to the row index of end.
     *     The column index of start is less than or equal to the column index of end.
     */
    public int totalGold(Location start, Location end)
    {
        /* to be implemented in part (b) */
    }
}

Part (a)

Write the constructor for the TreasureMap class. The constructor must initialize the instance variable map. The instance variable map must have the given number of rows and columns and contain Treasure objects for only the locations in the ArrayList locs.

All locations in locs are valid and within the bounds of map, and there are no duplicates.

Complete the TreasureMap constructor.

/**
 * Constructs a treasure map, as described in part (a)
 * Precondition: r > 0, c > 0, and the size of locs is at least 1 and less than r * c.
 *     All locations in locs are valid on the map, and there are no duplicates.
 * Postcondition: map contains r rows and c columns.
 *     map contains Treasure objects at each location in locs.
 *     All other map elements are null.
 */
public TreasureMap(int r, int c, ArrayList<Location> locs)
{
    
}

Class information for this question

public class Treasure

public Treasure()
public int getGold()

public class Location

public Location(int row, int col)
public int getRow()
public int getCol()

public class TreasureMap

private Treasure[][] map

public TreasureMap(int r, int c, ArrayList<Location> locs)
public int totalGold(Location start, Location end)

Part (b)

Write the method totalGold. This method returns the total number of gold coins found in all map locations that are within the rectangular region defined by start and end, inclusive, where start is the upper left corner of the region and end is the lower right corner of the region.

A null empty position in map contains no Treasure object. Assume that start and end are valid locations in map, the row index of start is less than or equal to the row index of end, and the column index of start is less than or equal to the column index of end.

Each of the following examples shows a representation of map containing Treasure objects. Numbers indicate the amount of gold in the object at a given location, and blanks denote null values.

Example 1

If

start is Location(1, 1)
end is Location(3, 3)

then

totalGold(start, end)

should return

20

because:

2 + 3 + 10 + 5 = 20

Example 2

If

start is Location(1, 0)
end is Location(1, 2)

then

totalGold(start, end)

should return

6

because:

5 + 1 = 6

Complete the totalGold method.

/**
 * Returns the total number of gold coins found in all map locations within the
 * rectangular region defined by start and end, as described in part (b)
 * Precondition: start and end are valid locations in map.
 *     The row index of start is less than or equal to the row index of end.
 *     The column index of start is less than or equal to the column index of end.
 */
public int totalGold(Location start, Location end)
{
    
}