The third FRQ on the AP CSA Exam as of 2026 will be on Data Analysis with ArrayList and worth 5 points. Students will be provided with a scenario and its associated class(es). Students will write one method of a given class based on provided specifications and examples. The method requires students to use, analyze,and manipulate data in an ArrayList structure. (Arrays will not be tested in an FRQ.)
public class ItemInfo
{
/**
* Returns the name of the item
*/
public String getName()
{ /* implementation not shown */ }
/**
* Returns a value greater than 0.0 that represents the
* cost of a single unit of the item, in dollars
*/
public double getCost()
{ /* implementation not shown */ }
/**
* Returns true if the item is currently available and
* returns false otherwise
*/
public boolean isAvailable()
{ /* implementation not shown */ }
/* There may be instance variables, constructors, and
methods that are not shown. */
}
The ItemInventory class maintains an ArrayList named inventory that contains all items at the store. A partial declaration of the ItemInventory class is shown below. (Note that it contains an ArrayList of ItemInfo objects.)
public class ItemInventory
{
/** The list of all items at the store */
private ArrayList<ItemInfo> inventory;
/**
* Returns the average cost of the available items
* whose cost is between lower and upper, inclusive
* Precondition: lower <= upper
* At least one available element of
* inventory has a cost between
* lower and upper, inclusive.
* No elements of inventory are null.
*/
public double averageWithinRange(double lower, double upper)
{ /* to be implemented */}
/* There may be instance variables, constructors, and methods
that are not shown. */
}
Write the ItemInventory method averageWithinRange. The method should return the average cost of the available items in inventory whose cost is between the parameters lower and upper, inclusive.
For the inventory shown, averageWithinRange(10.0, 50.0) should return 25.0, which is equal to the average cost of the available items within the specified range (a $20 action figure, a $45 frying pan, and a $10 coffee mug). Although the watch is within the specified range, it is not available.
/**
* Returns the average cost of the available items
* whose cost is between lower and upper, inclusive
* Precondition: lower <= upper
* At least one available element of
* inventory has a cost between
* lower and upper, inclusive.
* No elements of inventory are null.
*/
public double averageWithinRange(double lower, double upper)
Which of the following loops is the most appropriate for iterating through inventory (an ArrayList of ItemInfo objects) to access each ItemInfo object when the index is not directly needed?
This loop is incorrect because ArrayList does not have a length field.
for (ItemInfo item : inventory)
Yes, the enhanced for-each loop is the most appropriate and concise way to iterate through an ArrayList when you only need to access each element and not its index.
while (inventory.hasNext())
This loop is incorrect because ArrayList does not have a hasNext() method.
for (int i = 0; i <= inventory.size(); i++)
This loop has an off-by-one error. It should use i < inventory.size() instead of i <= inventory.size(). The enhanced for-each loop is still a simpler choice than a for loop with an index.
You are iterating through the inventoryArrayList using an enhanced for-each loop, and currentItem represents the ItemInfo object in the current iteration:
Accessing cost directly like this would only be possible if cost were a public instance variable. However, according to the ItemInfo class declaration, the cost is accessed via a method.
currentItem.getCost()
Yes, the ItemInfo class provides a public double getCost() method to return the cost of the item.
getCost(currentItem)
This syntax suggests a static method call, but getCost() is an instance method that must be called on an ItemInfo object.
currentItem.getCost
Methods in Java require parentheses after their name to indicate a method call, even if they take no arguments.
You are iterating through the inventoryArrayList, and currentItem is the ItemInfo object in the current iteration. Which of the following condition correctly checks if currentItem is available AND its cost is between lower and upper, inclusive?
This condition correctly checks if the item is available, but the cost range (> lower and < upper) is exclusive. The problem requires the range to be inclusive, meaning it should include lower and upper.
This condition uses the logical OR operator (||), which means the code block would execute if the item is available OR if its cost is within range, but the problem requires both conditions to be true.
Yes, this statement correctly uses the logical AND operator (&&) to combine the availability check with the cost range check. It also correctly uses >= and <= to ensure the range is inclusive of lower and upper.
The method averageWithinRange should iterate through the inventory list, checking each ItemInfo object to see if it is available and if its cost is within the specified range. If both conditions are met, the cost of the item should be added to a running total and counted. Remember that to compute the average, we need to add up the costs of all items that meet the criteria and then divide by the number of such items.
The following AP rubric is used to grade the averageWithinRange method in the ItemInventory class. Each item is worth 1 point, for a total of 5 points. Did you receive all of the points? In class, your teacher may have you grade each othersβ code.