FuelDepot free response answer

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

Review the FuelDepot free response problem with AP CS Tutor Brandon Horn.

FuelDepot Part (a): nextTankToFill

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

  for (int i = 0; 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();
 }

FuelDepot Part (b): moveToLocation

 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()));
 }

Get AP CS Help

2011 AP CS Exam Free Response Solutions

Recommended Practice Problems

Leave a Reply