# FuelDepot free response answer 2011

The `FuelDepot` problem from the 2011 AP Computer Science Exam is typical of free response problems that test lists.

`FuelDepot` is #3 from the from the 2011 AP Computer Science A Free Response problems.

https://secure-media.collegeboard.org/apc/ap11_frq_comp_sci_a.pdf

## Part (a) `nextTankToFill` method

``````public int nextTankToFill(int threshold)
{
int tankWithLeast = 0;

for (int i = 1; i < tanks.size(); i++)
if (tanks.get(i).getFuelLevel() < tanks.get(tankWithLeast).getFuelLevel())
tankWithLeast = i;

if (tanks.get(tankWithLeast).getFuelLevel() <= threshold)
return tankWithLeast;
else
return filler.getCurrentIndex();
}
``````

This is a standard find the min problem with a minor twist (the threshold). The code above handles finding the minimum first then handles the comparison with `threshold` at the end.

This problem can also be solved by initializing `tankWithLeast` to `filler.getCurrentIndex()` and checking the threshold as part of finding the minimum. (The alternate approach has the benefit of working even with 0 tanks, though it is not clear where the robot would be if there were 0 tanks.)

## Part (b) `moveToLocation` method

``````public void moveToLocation(int locIndex)
{
if (locIndex == filler.getCurrentIndex())
return;

if ((locIndex < filler.getCurrentIndex() && filler.isFacingRight())
|| (locIndex > filler.getCurrentIndex() && !filler.isFacingRight()))
filler.changeDirection();

filler.moveForward(Math.abs(locIndex - filler.getCurrentIndex()));
}
``````