Question 3: Data Analysis with ArrayList
Merging duplicate inventory entries
- Unit 4
- 5 points
- About 18 minutes
You write one method that traverses, analyzes or changes data stored in an ArrayList of objects, using the classes described in the question. New since the course was revised for 2025–26 (first tested in May 2026): it's always an ArrayList now (it used to be an array or an ArrayList), it's worth 5 points instead of 9, and points are no longer taken off as penalties. On the exam: Question 3 of 4, 5 points. Section II has 4 free-response questions in 90 minutes (45% of the score), all assessing Practice 2 (Develop Code). The exam is fully digital in Bluebook: you type your Java, and the Java Quick Reference is provided. No calculator. Suggested time is our split of the 90 minutes by points; College Board gives no per-question timing.
The question and its sources
A school store's inventory has one Item object for each entry on its stock sheet. Sometimes the same product was entered more than once. The Inventory class keeps the entries in an ArrayList.
The Item class
public class Item
{
/** Returns the name of the product */
public String getName()
{ /* implementation not shown */ }
/** Returns the number of units of this product in stock */
public int getCount()
{ /* implementation not shown */ }
/** Adds amount to the number of units in stock */
public void addCount(int amount)
{ /* implementation not shown */ }
// There may be instance variables, constructors, and methods that are not shown.
}The Inventory class
public class Inventory
{
/** The entries on the stock sheet; contains no null elements */
private ArrayList<Item> items;
/**
* Combines entries that have the same product name, as described
* in the question.
*/
public void mergeDuplicates()
{ /* to be implemented */ }
// There may be instance variables, constructors, and methods that are not shown.
}Example: items before the call mergeDuplicates()
| Index | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| getName() | "pen" | "ink" | "pen" | "pad" | "ink" | "pen" |
| getCount() | 4 | 2 | 3 | 5 | 1 | 2 |
Source: Hypothetical example
The same list after the call
| Index | 0 | 1 | 2 |
|---|---|---|---|
| getName() | "pen" | "ink" | "pad" |
| getCount() | 9 | 3 | 5 |
Source: Hypothetical example
Suggested time: 18 minutes
Your answers are saved in this browser as you type.
Part (a)
5 pointsWrite the Inventory method mergeDuplicates. For each product name, the first entry with that name stays in the list, and the count of every later entry with the same name is added to it with addCount. The later entries are then removed from the list. Entries that stay in the list keep their original order.
Complete method mergeDuplicates.public void mergeDuplicates()
Tab indents. Press Esc, then Tab, to move on.
0 lines · 0 / 2,500 characters
Checking scoring…
Scoring it yourself shows you the rubric, examples and a model answer. Try writing your answer first.