The TileGame
problem from the 2009 AP Computer Science Exam is typical of free response problems that test lists. The problem requires you to manipulate a List
and the objects inside.
TileGame
is #4 from the 2009 AP Computer Science Free Response.
https://secure-media.collegeboard.org/apc/ap09_frq_computer_science_a.pdf
Part (a) getIndexForFit
method
private int getIndexForFit(NumberTile tile)
{
if (board.size() == 0 || tile.getRight() == board.get(0).getLeft())
return 0;
if (tile.getLeft() == board.get(board.size() - 1).getRight())
return board.size();
for(int i = 1; i < board.size(); i++)
if(board.get(i - 1).getRight() == tile.getLeft() &&
board.get(i).getLeft() == tile.getRight())
return i;
return -1;
}
Part (b) insertTile
method
public boolean insertTile(NumberTile tile)
{
for (int r = 1; r <= 4; r++)
{
int index = getIndexForFit(tile);
if (index != -1)
{
board.add(index, tile);
return true;
}
tile.rotate();
}
return false;
}
2009 AP CS Exam Free Response Solutions
- NumberCube Free Response Solution
- StockpileCritter Free Response Solution
- BatteryCharger Free Response Solution
Help & comments
Get help from AP CS Tutor Brandon Horn