2011 Fuel Tank
<< 2011 AttractiveCritter | APQuestionsTrailIndex | 2011 RouteCipher >>
Question 3 from 2011 asks you to write two methods of the FuelDepot
class, nextTankToFill
and moveToLocation
.
Click here to see the questions from 2011
FuelTank.java
public interface FuelTank { /** @return an integer value that ranges from 0 (empty) to 100 (full) */ int getFuelLevel(); }
GasTank implements FuelTank
/** * A Helper class to get a working version * @author Chris Thiel, OFMCap */ public class GasTank implements FuelTank { private int level; public GasTank(int level){ this.level=level; } public int getFuelLevel() { return level; } public void addFuel(int amount){ level+=amount; if (level>100) level=100; } public boolean dispenseFuel(int amount){ level-=amount; if (level>=0) return true; level=0; return false; } }
FuelRobot.java
public interface FuelRobot { /** @return the index of the current location of the robot */ int getCurrentIndex(); /** Determine whether the robot is currently facing to the right * @return true if the robot is facing to the right (toward tanks with larger indexes) * false if the robot is facing to the left (toward tanks with smaller indexes) */ boolean isFacingRight(); /** Changes the current direction of the robot */ void changeDirection(); /** Moves the robot in its current direction by the number of locations specified. * @param numLocs the number of locations to move. A value of 1 moves * the robot to the next location in the current direction. * Precondition: numLocs > 0 */ void moveForward(int numLocs); }
GasRobot implements FuelRobot
import java.util.List; /** * A class to get a working version * @author Chris Thiel, OFMCap * */ public class GasRobot implements FuelRobot { public static final int RIGHT=0; public static final int LEFT=1; private int robotLocation; private int direction; private List<FuelTank> tanks; public GasRobot(List<FuelTank> tanks){ this.tanks=tanks; direction=RIGHT; robotLocation=0; } @Override public int getCurrentIndex() { return robotLocation; } @Override public boolean isFacingRight() { return direction==RIGHT; } @Override public void changeDirection() { direction=(direction+1)%2; } @Override public void moveForward(int numLocs) { if (direction==RIGHT) robotLocation+=numLocs; else robotLocation-=numLocs; } }
FuelDepot
This is where you add your code for the methods you are asked to complete in parts (a) and (b)
import java.util.ArrayList; import java.util.List; public class FuelDepot { /** The robot used to move the filling mechanism */ private FuelRobot filler; /** The list of fuel tanks */ private List<FuelTank> tanks; /** Determines and returns the index of the next tank to be filled. * @param threshold fuel tanks with a fuel level <= threshold may be filled * @return index of the location of the next tank to be filled * Postcondition: the state of the robot has not changed */ public int nextTankToFill(int threshold) { /* to be implemented in part (a) */ } /** Moves the robot to location locIndex. * @param locIndex the index of the location of the tank to move to * Precondition: 0 <=locIndex < tanks.size() * Postcondition: the current location of the robot is locIndex */ public void moveToLocation(int locIndex) { /* to be implemented in part (b) */ } // There may be instance variables, constructors, and methods that are not shown. // In fact...here are some that Fr Chris wrote to make this thing work: public FuelDepot(int direction, int loc, int[] tankLevels){ tanks=new ArrayList<FuelTank>(); //ArrayList implements List for (int level:tankLevels) tanks.add(new GasTank(level)); // GasTank implements FuelTank filler = new GasRobot(tanks); //GasRobot implements FuelRobot if (filler.isFacingRight()&&direction==GasRobot.LEFT) filler.changeDirection(); while(filler.getCurrentIndex()!=loc) filler.moveForward(1); } public String toString(){ String line1="Index->", line2="Level->",line3="Robot->"; for (int i=0;i<tanks.size();i++){ line1+=String.format("[ %3d ]",i); line2+=String.format("[ %3d ]", tanks.get(i).getFuelLevel()); if (i!=filler.getCurrentIndex()) line3+="[ ]"; else if(filler.isFacingRight()) line3+="[ -> ]"; else line3+="[ <- ]"; } return line1+"\n"+line2+"\n"+line3; } }
FuelTankTester.java
public class FuelTankTester { public static void main(String[] args){ int [] theTanks1={80,70,20,45,50,25}; FuelDepot depot=new FuelDepot(GasRobot.RIGHT,2, theTanks1); System.out.println("First Example\n"+depot+"\n"); int [] theTanks2={20,30,80,55,50,75,20}; depot=new FuelDepot(GasRobot.RIGHT,2, theTanks2); System.out.println("Part A Example\n"+depot+"\n"); System.out.println("nextTankToFill(50) should return 0 or 6:"); System.out.println("your method returns "+depot.nextTankToFill(50)); System.out.println("nextTankToFill(15) should return 2:"); System.out.println("your method returns "+depot.nextTankToFill(15)); System.out.println("\nPart B Testing:"); System.out.println("Testing moving without changing direction:"); depot.moveToLocation(4); System.out.println("moveToLocation(4)\n"+depot+"\n"); depot.moveToLocation(6); System.out.println("moveToLocation(6)\n"+depot+"\n"); System.out.println("Testing moving beyond the last tank--make sure you still have an arrow showing:"); depot.moveToLocation(8); System.out.println("moveToLocation(8)\n"+depot+"\n"); System.out.println("Testing moving that requires changing direction--make sure arrow points left:"); depot.moveToLocation(4); System.out.println("moveToLocation(4)\n"+depot+"\n"); System.out.println("Testing moving beyond the first tank--make sure you still have an arrow showing:"); depot.moveToLocation(-1); System.out.println("moveToLocation(-1)\n"+depot+"\n"); } }
If everything is working well you should see this output to the Tester:
First Example Index->[ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] Level->[ 80 ][ 70 ][ 20 ][ 45 ][ 50 ][ 25 ] Robot->[ ][ ][ -> ][ ][ ][ ] Part A Example Index->[ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Level->[ 20 ][ 30 ][ 80 ][ 55 ][ 50 ][ 75 ][ 20 ] Robot->[ ][ ][ -> ][ ][ ][ ][ ] nextTankToFill(50) should return 0 or 6: your method returns 0 nextTankToFill(15) should return 2: your method returns 2 Part B Testing: Testing moving without changing direction: moveToLocation(4) Index->[ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Level->[ 20 ][ 30 ][ 80 ][ 55 ][ 50 ][ 75 ][ 20 ] Robot->[ ][ ][ ][ ][ -> ][ ][ ] moveToLocation(6) Index->[ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Level->[ 20 ][ 30 ][ 80 ][ 55 ][ 50 ][ 75 ][ 20 ] Robot->[ ][ ][ ][ ][ ][ ][ -> ] Testing moving beyond the last tank--make sure you still have an arrow showing: moveToLocation(8) Index->[ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Level->[ 20 ][ 30 ][ 80 ][ 55 ][ 50 ][ 75 ][ 20 ] Robot->[ ][ ][ ][ ][ ][ ][ -> ] Testing moving that requires changing direction--make sure arrow points left: moveToLocation(4) Index->[ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Level->[ 20 ][ 30 ][ 80 ][ 55 ][ 50 ][ 75 ][ 20 ] Robot->[ ][ ][ ][ ][ <- ][ ][ ] Testing moving beyond the first tank--make sure you still have an arrow showing: moveToLocation(-1) Index->[ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Level->[ 20 ][ 30 ][ 80 ][ 55 ][ 50 ][ 75 ][ 20 ] Robot->[ ][ ][ ][ ][ <- ][ ][ ]