Question 4: 2D Array
Crop totals for each column of a garden
- Unit 4
- 6 points
- About 22 minutes
You write one method that traverses, analyzes or changes data in a 2D array, often 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 worth 6 points instead of 9, and points are no longer taken off as penalties. On the exam: Question 4 of 4, 6 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 garden is divided into a grid of plots. Each plot is represented by a Plot object that knows its crop and how many kilograms it produced. The Garden class stores the plots in a two-dimensional array. When an element of a two-dimensional array is accessed, the first index is the row and the second index is the column.
The Plot class
public class Plot
{
/** Returns the name of the crop in this plot, such as "beans" */
public String getCrop()
{ /* implementation not shown */ }
/** Returns the harvest from this plot, in whole kilograms (at least 0) */
public int getYield()
{ /* implementation not shown */ }
// There may be instance variables, constructors, and methods that are not shown.
}The Garden class
public class Garden
{
/**
* The plots. plots has at least one row and at least one column,
* every row has the same length, and there are no null elements.
*/
private Plot[][] plots;
/**
* Returns an array with one element for each column of plots, as
* described in the question.
* Postcondition: plots is unchanged.
*/
public int[] columnTotals(String crop)
{ /* to be implemented */ }
// There may be instance variables, constructors, and methods that are not shown.
}Example: plots, written as crop/yield
| Row | Column 0 | Column 1 | Column 2 | Column 3 |
|---|---|---|---|---|
| 0 | beans/4 | kale/2 | beans/1 | corn/9 |
| 1 | kale/3 | beans/5 | beans/2 | kale/1 |
| 2 | beans/6 | corn/8 | kale/4 | kale/2 |
Source: Hypothetical example
Example calls on the plots above
| Call | Value returned |
|---|---|
columnTotals("beans") | {10, 5, 3, 0} |
columnTotals("kale") | {3, 2, 4, 3} |
columnTotals("peas") | {0, 0, 0, 0} |
Source: Hypothetical example
Suggested time: 22 minutes
Your answers are saved in this browser as you type.
Part (a)
6 pointsWrite the Garden method columnTotals. It returns a new int array with one element for each column of plots. Element c of the returned array is the total yield of the plots in column c whose crop is crop, or 0 if no plot in that column has that crop.
For example, element 0 of columnTotals("beans") is 10, because column 0 has beans plots with yields 4 and 6.
Complete method columnTotals.public int[] columnTotals(String crop)
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.