2024 AP CSA FRQ 4 – GridPath 2D Array Java Path Problem
Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA. You will only earn credit for what you write in the separate Free Response booklet.
- Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
- Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied.
- In writing solutions for each question, you may use any of the methods of the standard Java library that are included in the Java Quick Reference.
Location class represents a row and column position in the 2D array.
public class Location
{
private int theRow;
private int theCol;
public Location(int r, int c)
{
theRow = r;
theCol = c;
}
public int getRow()
{ return theRow; }
public int getCol()
{ return theCol; }
}
The following GridPath class contains the 2D array and methods to use to determine a path through the array. You will write two methods of the GridPath class.
public class GridPath
{
/** Initialized in the constructor with distinct values that never change */
private int[][] grid;
/**
* Returns the Location representing a neighbor of the grid element at row and col,
* as described in part (a)
* Preconditions: row is a valid row index and col is a valid column index in grid.
* row and col do not specify the element in the last row and last column of grid.
*/
public Location getNextLoc(int row, int col)
{ /* to be implemented in part (a) */ }
/**
* Computes and returns the sum of all values on a path through grid, as described in
* part (b)
* Preconditions: row is a valid row index and col is a valid column index in grid.
* row and col do not specify the element in the last row and last column of grid.
*/
public int sumPath(int row, int col)
{ /* to be implemented in part (b) */ }
// There may be instance variables, constructors, and methods that are not shown.
}
(a) Write the getNextLoc method, which returns a Location object that represents the smaller of two neighbors of the grid element at row and col, according to the following rules.
- The two neighbors that are considered are the element below the given element and the element to the right of the given element, if they exist.
- If both neighbors exist, the
Locationof the neighbor with the smaller value is returned. - If only one neighbor exists, the
Locationof the existing neighbor is returned.
For example, assume that grid contains the following values:

public Location getNextLoc(int row, int col)
(b) Write the sumPath method, which returns the sum of all values on a path in grid. The path begins with the element at row and col and is determined by successive calls to getNextLoc. The path ends when the element in the last row and the last column of grid is reached.
For example:
public int sumPath(int row, int col)
Class information for this question
public class Location
private int theRow
private int theCol
public Location(int r, int c)
public int getRow()
public int getCol()
public class GridPath
private int[][] grid
public Location getNextLoc(int row, int col)
public int sumPath(int row, int col)