Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sammy Samkough
- // Fuel Depot
- // Spec: Exercise the code as per AP specification and verify that it is working properly
- public class Robot implements FuelRobot
- {
- private int currentIndex;
- private boolean facingRight;
- /** Defaults to location 0 and facing to the right */
- public Robot()
- {
- currentIndex = 0;
- facingRight = true;
- }
- /** Sets this Robot to the index and isRight parameters indicated */
- public Robot(int index, boolean isRight)
- {
- currentIndex = index;
- facingRight = isRight;
- }
- /** @return the index of the current location of the robot */
- public int getCurrentIndex()
- {
- return currentIndex;
- }
- /** 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)
- */
- public boolean isFacingRight()
- {
- if (facingRight == true)
- {
- return facingRight;
- }
- else
- {
- return false;
- }
- }
- /** Changes the current direction of the robot */
- public void changeDirection()
- {
- facingRight = !facingRight;
- }
- /** 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
- */
- public void moveForward(int numLocs)
- {
- int d = (facingRight ? 1 : -1 );
- currentIndex = currentIndex + (d * numLocs);
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Fuel Depot
- // Spec: Exercise the code as per AP specification and verify that it is working properly
- public interface FuelTank
- {
- /** @return an integer value that ranges from 0 (empty) to 100 (full) */
- int getFuelLevel();
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Fuel Depot
- // Spec: Exercise the code as per AP specification and verify that it is working properly
- import java.util.Collections;
- import java.util.List;
- import java.util.ArrayList;
- public class FuelRunner
- {
- public static void main(String[] args)
- {
- Tanker zero = new Tanker(20);
- Tanker one = new Tanker(30);
- Tanker two = new Tanker(80);
- Tanker three = new Tanker(55);
- Tanker four = new Tanker(50);
- Tanker five = new Tanker(75);
- Tanker six = new Tanker(20);
- ArrayList<Tanker> tanks = new ArrayList<Tanker>();
- tanks.add(zero);
- tanks.add(one);
- tanks.add(two);
- tanks.add(three);
- tanks.add(four);
- tanks.add(five);
- tanks.add(six);
- FuelDepot depot = new FuelDepot(tanks, 2, true);
- System.out.println("Here's our Fuel Depot: " + depot);
- System.out.println("\nNext tank to fill <= 50 is located at index " + depot.nextTankToFill(50));
- System.out.println("\nNext tank to fill <= 15 is located at index " + depot.nextTankToFill(15));
- System.out.println("\nMoving the robot to space 5...");
- depot.moveToLocation(5);
- System.out.println("\nHere's our Fuel Depot and Robot: " + depot);
- System.out.println("\nNow let's move him to space 2...");
- depot.moveToLocation(2);
- System.out.println("\nHere's our Fuel Depot and Robot: " + depot + "\n");
- }
- }
- /*
- */
- -------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Fuel Depot
- // Spec: Exercise the code as per AP specification and verify that it is working properly
- 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);
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Fuel Depot
- // Spec: Exercise the code as per AP specification and verify that it is working properly
- import java.util.Collections;
- import java.util.List;
- import java.util.ArrayList;
- public class FuelDepot
- {
- /** The robot used to move the filling mechanism */
- private FuelRobot filler;
- /** The list of fuel tanks */
- private List<Tanker> tanks;
- /** Instantiate filler and tanks */
- public FuelDepot()
- {
- filler = new Robot();
- tanks = new ArrayList<Tanker>();
- }
- /** Instantiate robot to default location and set tanks to point to the tankerList */
- public FuelDepot(ArrayList<Tanker> tankerList)
- {
- filler = new Robot();
- tanks = tankerList;
- }
- /** Instantiate robot to roboCurrentIndex and isRightFacing parameters and set tanks to point to the tankerList */
- public FuelDepot(ArrayList<Tanker> tankerList, int roboCurrentIndex, boolean isRightFacing)
- {
- filler = new Robot(roboCurrentIndex, isRightFacing);
- tanks = tankerList;
- }
- /** 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)
- {
- int minLevel = this.tanks.get(0).getFuelLevel();
- int minTankIndex = 0;
- for (int i = 1; i < this.tanks.size(); i++)
- {
- if (this.tanks.get(i).getFuelLevel() < minLevel)
- {
- minLevel = this.tanks.get(i).getFuelLevel();
- minTankIndex = i;
- }
- }
- if (minLevel <= threshold)
- {
- return minTankIndex;
- }
- else
- {
- return this.filler.getCurrentIndex();
- }
- }
- /** 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)
- {
- if (this.filler.getCurrentIndex() > locIndex)
- {
- if (this.filler.isFacingRight())
- {
- this.filler.changeDirection();
- }
- this.filler.moveForward(this.filler.getCurrentIndex() - locIndex);
- }
- if (this.filler.getCurrentIndex() < locIndex)
- {
- if (!this.filler.isFacingRight())
- {
- this.filler.changeDirection();
- }
- this.filler.moveForward(locIndex - this.filler.getCurrentIndex());
- }
- }
- /** @return a list of the elements (fuel levels) in tanks
- * The robot should appear after the index in which it is located with either
- * *R>* if it is facing right or
- * *R<* if it is facing left
- * Hint: step thru the tanks array and use the .toString() method for the tanks
- * compare the robots current index to your location in the array
- * when you get a match... look to see which way the robot is facing
- */
- public String toString()
- {
- String result = "";
- for (int i = 0; i <= tanks.size(); i++)
- {
- result += tanks.toString();
- if (!filler.isFacingRight())
- {
- result += "Tank is not facing right.";
- }
- else
- {
- result += "Tank is facing right.";
- }
- }
- return result;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Fuel Depot
- // Spec: Exercise the code as per AP specification and verify that it is working properly
- public class Tanker implements FuelTank
- {
- private int fuelLevel;
- /** sets this tanker to empty */
- public Tanker()
- {
- fuelLevel = 0;
- }
- /** sets this tanker to the level indicated */
- public Tanker(int level)
- {
- fuelLevel = level;
- }
- /** @ return the fuelLevel of this FuelTank */
- public int getFuelLevel()
- {
- return fuelLevel;
- }
- /** @return the fuelLevel with a " " after it */
- public String toString()
- {
- return fuelLevel + "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement