Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 KB | None | 0 0
  1. /*
  2. * AP CS MOOC
  3. * Term 2 - Assignment 2, Part 1: Boxcar
  4. * A class which represents a single car on a freight train.
  5. */
  6.  
  7. public class Boxcar
  8. {
  9. // Variables that will be initialized in the Boxcar constructors.
  10. private String cargo = "";
  11. private int numUnits;
  12. private boolean repair;
  13.  
  14. // Default constructor that sets the boxcar to "gizmos", 5, and false.
  15. public Boxcar()
  16. {
  17. cargo = "gizmos";
  18. numUnits = 5;
  19. repair = false;
  20. }
  21.  
  22.  
  23. // This constructor sets the cargo variable to the parameter c, but only if
  24. // c is "gizmos", "gadgets", "widgets", or "wadgets". The constructor ignores
  25. // the case of of the value in c. If c holds any value other than
  26. // "gizmos", "gadgets", "widgets", or "wadgets", the constructor sets cargo
  27. // to "gizmos". The numUnits variable is set to the parameter u. If u is less than
  28. // 0 or higher than the maximum capacity of 10 units, numUnits is set to 0. The repair
  29. // variable is set to the parameter r. If repair is true, numUnits is set to 0
  30. // no matter what value is stored in the u parameter.
  31. public Boxcar(String c, int u, boolean r)
  32. {
  33. if (c.equalsIgnoreCase("gizmos") || c.equalsIgnoreCase("gadgets") || c.equalsIgnoreCase("widgets") || c.equalsIgnoreCase("wadgets"))
  34. cargo = c.toLowerCase();
  35. else
  36. cargo = "gizmos";
  37.  
  38. if (u > 0 && u < 10)
  39. numUnits = u;
  40. else
  41. numUnits = 0;
  42.  
  43. repair = r;
  44. if (repair)
  45. numUnits = 0;
  46. }
  47.  
  48. // The toString method returns a String with the Boxcar in the format:
  49. // 5 gizmos in service
  50. // 10 widgets in service
  51. // 0 gadgets in repair
  52. //
  53. // Notice there is one space in between the number of units and the cargo
  54. // and a tab between the value for cargo and "in repair"/"in service"
  55. public String toString()
  56. {
  57. String sr = "";
  58. if (repair)
  59. sr = "in repair";
  60. else
  61. sr = "in service";
  62.  
  63. return numUnits + " " + cargo + "\t" + sr;
  64. }
  65.  
  66. // This method adds 1 to the number of units in the BoxCar. The maximum
  67. // capacity of a boxcar is 10 units. If increasing the number of units
  68. // would go beyond the maximum, keep numUnits at the max capacity.
  69. // If the repair variable is true, then numUnits may only be set to 0.
  70. public void loadCargo() {
  71. if (numUnits < 10)
  72. numUnits++;
  73.  
  74. if (repair)
  75. numUnits = 0;
  76. }
  77.  
  78. // The getCargo method returns the cargo of the boxcar.
  79. public String getCargo()
  80. {
  81. /* missing code (don't forget to update the return statement) */
  82. return cargo;
  83. }
  84.  
  85. // The setCargo method sets the cargo type of the boxcar. The cargo variable is
  86. // set to c only if c is "gizmos", "gadgets", "widgets", or "wadgets".
  87. // The method ignores the case of of the value in c. If c holds any value other than
  88. // "gizmos", "gadgets", "widgets", or "wadgets" (ignoring case), the method sets cargo
  89. // to "gizmos".
  90. public void setCargo(String c)
  91. {
  92. if (c.equalsIgnoreCase("gizmos") || c.equalsIgnoreCase("gadgets") || c.equalsIgnoreCase("widgets") || c.equalsIgnoreCase("wadgets"))
  93. cargo = c.toLowerCase();
  94. else
  95. cargo = "gizmos";
  96. }
  97.  
  98. // The isFull method returns true if numUnits is equal to 10, false otherwise.
  99. public boolean isFull()
  100. {
  101. if (numUnits == 10)
  102. return true;
  103. else
  104. return false;
  105. }
  106.  
  107. // The callForRepair method sets the variable repair to true, and numUnits to 0.
  108. public void callForRepair()
  109. {
  110. repair = true;
  111. numUnits = 0;
  112. }
  113. }
  114. -----------------------------------------------------
  115. /*
  116. * AP CS MOOC
  117. * Term 2 - Assignment 2, Part 2: FreightTrain
  118. * A class which represents a freight train with multiple boxcars.
  119. */
  120.  
  121. import java.util.ArrayList;
  122.  
  123. public class FreightTrain
  124. {
  125. // An ArrayList that stores a train with multiple boxcars
  126. private ArrayList<Boxcar> train = new ArrayList<Boxcar>();
  127.  
  128. // Default constructor that sets train to an ArrayList holding one
  129. // boxcar containing 5 gizmos, that is not in repair.
  130. public FreightTrain()
  131. {
  132.  
  133. train.add(new Boxcar());
  134. }
  135.  
  136. // A constructor that sets train to an ArrayList of size n, holding
  137. // n boxcars, that are all hold 5 gizmos and are not in repair. If n <= 0,
  138. // then the train should be set to size one, with a single boxcar containing
  139. // 5 gizmos and not in repair.
  140. public FreightTrain(int n)
  141. {
  142. if (n > 0)
  143. for (int i = 0; i < n; i++)
  144. {
  145. train.add(i, new Boxcar());
  146. }
  147. else
  148. {
  149. train.add (new Boxcar());
  150. }
  151. }
  152.  
  153. // This method returns a String representation of the
  154. // Boxcar objects in the ArrayList, one per line. For example,
  155. // here is the String returned when toString is called on a
  156. // FreightTrain with 5 boxcars:
  157. //
  158. // 3 gadgets in service
  159. // 2 wadgets in service
  160. // 0 gizmos in repair
  161. // 7 gadgets in service
  162. // 0 gadgets in repair
  163. //
  164. // Note: there is one space between the number of units and
  165. // the cargo type, and a tab between the cargo type and
  166. // "in repair"/"in service".
  167. public String toString()
  168. {
  169. String r = "";
  170. String s = "";
  171. for (Boxcar x: train)
  172. {
  173.  
  174. s = s + x.toString() + "\n";
  175. }
  176. return s;
  177. }
  178.  
  179. // This method sets the cargo type of all the boxcars in the entire train.
  180. public void setCargo(String c)
  181. {
  182. for (Boxcar x: train)
  183. x.setCargo(c);
  184. }
  185.  
  186. // This method sets the boxcars to the pattern "gizmos", "gadgets", "widgets",
  187. // "wadgets", "gizmos", "gadgets", "widgets", "wadgets", ...
  188. // until the end of the train.
  189. public void setMultiCargo()
  190. {
  191. int counter = 0;
  192. ArrayList<String> s = new ArrayList<String>();
  193. s.add("gizmos"); s.add("gadgets"); s.add("widgets"); s.add("wadgets");
  194. for (Boxcar x: train)
  195. {
  196. x.setCargo(s.get(counter));
  197.  
  198. counter++;
  199. if(counter == 4)
  200. counter = 0;
  201. }
  202. }
  203.  
  204. // This method fills every boxcar in the train to max capacity of 10.
  205. // Each individual boxcar can only hold cargo if its
  206. // repair variable is false.
  207. public void fillTrain()
  208. {
  209. for (Boxcar x: train)
  210. {
  211. while (!x.isFull())
  212. x.loadCargo();
  213. }
  214. }
  215.  
  216. // This method sets the Boxcar at location i’s repair variable to true.
  217. public void callForRepair(int i)
  218. {
  219. train.get(i).callForRepair();
  220. }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement