Unit 4B - Free Response Question (FRQ) 3 - ItemInventory

From CS Awesome 2CC BY-NC-SA 4.0Original page

Java1 class period1 exercise2 graded tests

Files

README.mdDownload

Unit 4B - Free Response Question (FRQ) 3 - ItemInventory

Each coding exercise has its own folder. Open its .java file and press Run to try it. Run Tests checks every graded exercise in this lesson at once.

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

Here are two common patterns for ArrayList traversal loops that you will need to use in this FRQ:

for (Type obj : list)
{
    if (obj ....)
        ...
}

for(int i=0; i < list.size(); i++)
{
   if (list.get(i) ....)
       ...
}

The following problem is a good example FRQ 3 from the 2025-2026 AP CSA Course and Exam Description (CED).

FRQ ItemInventory Description

The ItemInfo class is used to store information about an item at a store. A partial declaration of the ItemInfo class is shown below.

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.

Suppose inventory contains the following seven ItemInfo objects.

ItemInventory with ArrayList of 7 ItemInfo objects

ItemInventory with ArrayList of 7 ItemInfo objects

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.

Complete method averageWithinRange.

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

The Algorithm for averageWithinRange

The following exercises will help you to develop the algorithm for the method averageWithinRange.

Check your understanding: 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? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ3ArrayList.html#inventory-loop-mcq

Check your understanding: You are iterating through the inventory ArrayList using an enhanced for-each loop, and currentItem represents the ItemInfo object in the current iteration:

for (ItemInfo currentItem : inventory)
{
    // ...
}

Inside this loop, how would you get the cost of the currentItem?

Answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ3ArrayList.html#iteminfo-get-cost

Check your understanding: You are iterating through the inventory ArrayList, 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? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ3ArrayList.html#iteminventory-conditions

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.

Solve FRQ ItemInventory

Exercise 1

Write the averageWithinRange method for the ItemInventory class.

Open the 01-frq-average-inventory folder and edit ItemInventory.java. Press Run to try it; Run Tests checks it (2 tests).

Grading Rubric for FRQ 3

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.

Rubric for averageWithinRange

Rubric for averageWithinRange

Rubric for averageWithinRange

For teachers

Unit 4B - Free Response Question (FRQ) 3 - ItemInventory

Students read CSAwesome2's “Unit 4B - Free Response Question (FRQ) 3 - ItemInventory” in CS Room and work through its 1 Java coding exercise.

  • Time: 1 class period
  • Tests: 2 in 1 graded exercise, set up for you when you import the lesson

Goals

  • Practice for Unit 4: Data Collections.

Before class

In class

  1. Students read README.md. Questions that need CS Awesome's interactive widgets link back to it.
  2. Students do exercise 01 in its folder, pressing Run to try their code.
  3. Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.

Exercises

FolderFileGradingTests
01-frq-average-inventoryItemInventory.javaAutograded2

Teachers can download a reference solution after signing in.