Advertisement
Crenox

Fuel Depot Java Program

Mar 13th, 2015
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.34 KB | None | 0 0
  1. // Sammy Samkough
  2. // Fuel Depot
  3. // Spec: Exercise the code as per AP specification and verify that it is working properly
  4.  
  5. public class Robot implements FuelRobot
  6. {
  7. private int currentIndex;
  8. private boolean facingRight;
  9.  
  10. /** Defaults to location 0 and facing to the right */
  11. public Robot()
  12. {
  13. currentIndex = 0;
  14. facingRight = true;
  15. }
  16.  
  17. /** Sets this Robot to the index and isRight parameters indicated */
  18. public Robot(int index, boolean isRight)
  19. {
  20. currentIndex = index;
  21. facingRight = isRight;
  22. }
  23.  
  24. /** @return the index of the current location of the robot */
  25. public int getCurrentIndex()
  26. {
  27. return currentIndex;
  28. }
  29.  
  30. /** Determine whether the robot is currently facing to the right
  31. * @return true if the robot is facing to the right (toward tanks with larger indexes)
  32. * false if the robot is facing to the left (toward tanks with smaller indexes)
  33. */
  34. public boolean isFacingRight()
  35. {
  36. if (facingRight == true)
  37. {
  38. return facingRight;
  39. }
  40. else
  41. {
  42. return false;
  43. }
  44. }
  45.  
  46. /** Changes the current direction of the robot */
  47. public void changeDirection()
  48. {
  49. facingRight = !facingRight;
  50. }
  51.  
  52. /** Moves the robot in its current direction by the number of locations specified.
  53. * @param numLocs the number of locations to move. A value of 1 moves
  54. * the robot to the next location in the current direction.
  55. * Precondition: numLocs > 0
  56. */
  57. public void moveForward(int numLocs)
  58. {
  59. int d = (facingRight ? 1 : -1 );
  60. currentIndex = currentIndex + (d * numLocs);
  61. }
  62. }
  63. -------------------------------------------------------------------------------------------------------------------------------
  64. // Sammy Samkough
  65. // Fuel Depot
  66. // Spec: Exercise the code as per AP specification and verify that it is working properly
  67.  
  68. public interface FuelTank
  69. {
  70. /** @return an integer value that ranges from 0 (empty) to 100 (full) */
  71. int getFuelLevel();
  72. }
  73. -------------------------------------------------------------------------------------------------------------------------------
  74. // Sammy Samkough
  75. // Fuel Depot
  76. // Spec: Exercise the code as per AP specification and verify that it is working properly
  77.  
  78. import java.util.Collections;
  79. import java.util.List;
  80. import java.util.ArrayList;
  81.  
  82. public class FuelRunner
  83. {
  84. public static void main(String[] args)
  85. {
  86. Tanker zero = new Tanker(20);
  87. Tanker one = new Tanker(30);
  88. Tanker two = new Tanker(80);
  89. Tanker three = new Tanker(55);
  90. Tanker four = new Tanker(50);
  91. Tanker five = new Tanker(75);
  92. Tanker six = new Tanker(20);
  93.  
  94. ArrayList<Tanker> tanks = new ArrayList<Tanker>();
  95. tanks.add(zero);
  96. tanks.add(one);
  97. tanks.add(two);
  98. tanks.add(three);
  99. tanks.add(four);
  100. tanks.add(five);
  101. tanks.add(six);
  102.  
  103. FuelDepot depot = new FuelDepot(tanks, 2, true);
  104.  
  105. System.out.println("Here's our Fuel Depot: " + depot);
  106. System.out.println("\nNext tank to fill <= 50 is located at index " + depot.nextTankToFill(50));
  107. System.out.println("\nNext tank to fill <= 15 is located at index " + depot.nextTankToFill(15));
  108. System.out.println("\nMoving the robot to space 5...");
  109. depot.moveToLocation(5);
  110. System.out.println("\nHere's our Fuel Depot and Robot: " + depot);
  111. System.out.println("\nNow let's move him to space 2...");
  112. depot.moveToLocation(2);
  113. System.out.println("\nHere's our Fuel Depot and Robot: " + depot + "\n");
  114. }
  115. }
  116. /*
  117. */
  118.  
  119. -------------------------------------------------------------------------------------------------------------------------------
  120. // Sammy Samkough
  121. // Fuel Depot
  122. // Spec: Exercise the code as per AP specification and verify that it is working properly
  123.  
  124. public interface FuelRobot
  125. {
  126. /** @return the index of the current location of the robot */
  127. int getCurrentIndex();
  128.  
  129. /** Determine whether the robot is currently facing to the right
  130. * @return true if the robot is facing to the right (toward tanks with larger indexes)
  131. * false if the robot is facing to the left (toward tanks with smaller indexes)
  132. */
  133. boolean isFacingRight();
  134.  
  135. /** Changes the current direction of the robot */
  136. void changeDirection();
  137.  
  138. /** Moves the robot in its current direction by the number of locations specified.
  139. * @param numLocs the number of locations to move. A value of 1 moves
  140. * the robot to the next location in the current direction.
  141. * Precondition: numLocs > 0
  142. */
  143. void moveForward(int numLocs);
  144. }
  145. -------------------------------------------------------------------------------------------------------------------------------
  146. // Sammy Samkough
  147. // Fuel Depot
  148. // Spec: Exercise the code as per AP specification and verify that it is working properly
  149.  
  150. import java.util.Collections;
  151. import java.util.List;
  152. import java.util.ArrayList;
  153.  
  154. public class FuelDepot
  155. {
  156. /** The robot used to move the filling mechanism */
  157. private FuelRobot filler;
  158.  
  159. /** The list of fuel tanks */
  160. private List<Tanker> tanks;
  161.  
  162. /** Instantiate filler and tanks */
  163. public FuelDepot()
  164. {
  165. filler = new Robot();
  166. tanks = new ArrayList<Tanker>();
  167. }
  168.  
  169. /** Instantiate robot to default location and set tanks to point to the tankerList */
  170. public FuelDepot(ArrayList<Tanker> tankerList)
  171. {
  172. filler = new Robot();
  173. tanks = tankerList;
  174. }
  175.  
  176. /** Instantiate robot to roboCurrentIndex and isRightFacing parameters and set tanks to point to the tankerList */
  177. public FuelDepot(ArrayList<Tanker> tankerList, int roboCurrentIndex, boolean isRightFacing)
  178. {
  179. filler = new Robot(roboCurrentIndex, isRightFacing);
  180. tanks = tankerList;
  181. }
  182.  
  183. /** Determines and returns the index of the next tank to be filled.
  184. * @param threshold fuel tanks with a fuel level £ threshold may be filled
  185. * @return index of the location of the next tank to be filled
  186. * Postcondition: the state of the robot has not changed
  187. */
  188. public int nextTankToFill(int threshold)
  189. {
  190. int minLevel = this.tanks.get(0).getFuelLevel();
  191. int minTankIndex = 0;
  192.  
  193. for (int i = 1; i < this.tanks.size(); i++)
  194. {
  195. if (this.tanks.get(i).getFuelLevel() < minLevel)
  196. {
  197. minLevel = this.tanks.get(i).getFuelLevel();
  198. minTankIndex = i;
  199. }
  200. }
  201.  
  202. if (minLevel <= threshold)
  203. {
  204. return minTankIndex;
  205. }
  206. else
  207. {
  208. return this.filler.getCurrentIndex();
  209. }
  210. }
  211.  
  212. /** Moves the robot to location locIndex.
  213. * @param locIndex the index of the location of the tank to move to
  214. * Precondition: 0 £ locIndex < tanks.size()
  215. * Postcondition: the current location of the robot is locIndex
  216. */
  217. public void moveToLocation(int locIndex)
  218. {
  219. if (this.filler.getCurrentIndex() > locIndex)
  220. {
  221. if (this.filler.isFacingRight())
  222. {
  223. this.filler.changeDirection();
  224. }
  225. this.filler.moveForward(this.filler.getCurrentIndex() - locIndex);
  226. }
  227.  
  228. if (this.filler.getCurrentIndex() < locIndex)
  229. {
  230. if (!this.filler.isFacingRight())
  231. {
  232. this.filler.changeDirection();
  233. }
  234. this.filler.moveForward(locIndex - this.filler.getCurrentIndex());
  235. }
  236. }
  237.  
  238. /** @return a list of the elements (fuel levels) in tanks
  239. * The robot should appear after the index in which it is located with either
  240. * *R>* if it is facing right or
  241. * *R<* if it is facing left
  242. * Hint: step thru the tanks array and use the .toString() method for the tanks
  243. * compare the robots current index to your location in the array
  244. * when you get a match... look to see which way the robot is facing
  245. */
  246. public String toString()
  247. {
  248. String result = "";
  249.  
  250. for (int i = 0; i <= tanks.size(); i++)
  251. {
  252. result += tanks.toString();
  253.  
  254. if (!filler.isFacingRight())
  255. {
  256. result += "Tank is not facing right.";
  257. }
  258. else
  259. {
  260. result += "Tank is facing right.";
  261. }
  262. }
  263.  
  264. return result;
  265. }
  266.  
  267. }
  268. -------------------------------------------------------------------------------------------------------------------------------
  269. // Sammy Samkough
  270. // Fuel Depot
  271. // Spec: Exercise the code as per AP specification and verify that it is working properly
  272.  
  273. public class Tanker implements FuelTank
  274. {
  275. private int fuelLevel;
  276.  
  277. /** sets this tanker to empty */
  278. public Tanker()
  279. {
  280. fuelLevel = 0;
  281. }
  282.  
  283. /** sets this tanker to the level indicated */
  284. public Tanker(int level)
  285. {
  286. fuelLevel = level;
  287. }
  288.  
  289. /** @ return the fuelLevel of this FuelTank */
  290. public int getFuelLevel()
  291. {
  292. return fuelLevel;
  293. }
  294.  
  295. /** @return the fuelLevel with a " " after it */
  296. public String toString()
  297. {
  298. return fuelLevel + "";
  299. }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement