Advertisement
TheEpicKiller

Assignment 2 Part 2 Completed

Feb 1st, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.69 KB | None | 0 0
  1. /*
  2. * AP CS MOOC
  3. * Term 2 - Assignment 2, Part 2: Strand
  4. * A class which represents a strand of lights.
  5. */
  6.  
  7. import java.util.ArrayList;
  8.  
  9. public class Strand
  10. {
  11. // An ArrayList that stores a strand of lights
  12. private ArrayList<Light> strand = new ArrayList<Light>();
  13.  
  14. // Default constructor that sets strand to an ArrayList holding one
  15. // turned on white bulb, that is not burnt out.
  16. public Strand()
  17. {
  18. this(1);
  19. }
  20.  
  21. // A constructor that sets strand to an ArrayList of size n, holding
  22. // n white bulbs, that are all turned on and not burnt out. If n <= 0,
  23. // then the strand should be set to size one, with a white bulb, on
  24. // and not burnt out.
  25. public Strand(int n)
  26. {
  27. if(n <= 0)
  28. strand.add(new Light());
  29. else
  30. for(int i=0; i < n; i++)
  31. {
  32. strand.add(new Light());
  33. }
  34. }
  35.  
  36. // This method returns a String representation of the
  37. // Light objects in the ArrayList, one per line. For example,
  38. // here is the String returned when toString is called on a
  39. // Strand with 5 lights:
  40. //
  41. // on green not burnt out
  42. // off red not burnt out
  43. // off green burnt out
  44. // on blue not burnt out
  45. // on red not burnt out
  46. //
  47. // Note: there is one space between "off"/"on" and the value for
  48. // color, and a tab before the "burnt out" or "not burnt out".
  49. public String toString()
  50. {
  51. String temp = "";
  52. for(Light s: strand)
  53. {
  54. temp += s.toString();
  55. }
  56. return temp;
  57.  
  58. }
  59.  
  60. // This method sets the color of all the light bulbs in the entire Strand.
  61. public void setColor(String c)
  62. {
  63. for(Light s: strand)
  64. s.setColor(c);
  65. }
  66.  
  67. // This method sets the light bulbs to the pattern "red", "green", "blue",
  68. // "red", "green", "blue",... until the end of the strand.
  69. public void setMulti()
  70. {
  71. int count = 0;
  72. for(Light s: strand)
  73. {
  74. if(count%3 == 0)
  75. s.setColor("red");
  76. if(count%3 == 1)
  77. s.setColor("green");
  78. if(count%3 == 2)
  79. s.setColor("blue");
  80. count++;
  81. }
  82. }
  83.  
  84. // This method turns on all the lights in the strand. Each individual bulb
  85. // can only be turned on if it's burntOut variable is false.
  86. public void turnOn()
  87. {
  88. for(Light s: strand)
  89. {
  90. if(!s.isOn())
  91. {
  92. s.flip();
  93. }
  94. }
  95. }
  96.  
  97. // This method turns off all the lights in the strand.
  98. public void turnOff()
  99. {
  100. for(Light s: strand)
  101. {
  102. if(s.isOn())
  103. {
  104. s.flip();
  105. }
  106. }
  107. }
  108.  
  109. // This method sets the Light at location i's burntOut variable to true.
  110. public void burnOut(int i)
  111. {
  112. strand.get(i).burnOut();
  113. }
  114.  
  115. public static void main(String[] args)
  116. {
  117. // *************************************************************************
  118. // 1. Test Strand()
  119. // *************************************************************************
  120. System.out.println("1. Test the default constructor Strand()");
  121. Strand strand1 = new Strand();
  122. if (strand1.strand.size() == 1)
  123. System.out.println("*** PASS: Strand() creates a list of size 1");
  124. else
  125. System.out.println("*** FAIL: Strand() creates a list of size "
  126. + strand1.strand.size()
  127. + ", when a list of size 1 is expected.");
  128.  
  129. // ***********************************
  130. // 2. Test Strand(n)
  131. // ***********************************
  132. System.out.println("\n2. Test the constructor Strand(n)");
  133. // Try to create a strand of lights with 0 bulbs
  134. Strand emptyStrand = new Strand(0);
  135. if (emptyStrand.strand.size() == 1)
  136. System.out.println("*** PASS: Strand(0) creates a list of size 1");
  137. else
  138. System.out.println("*** FAIL: Strand(0) creates a list of size "
  139. + emptyStrand.strand.size()
  140. + ", when a list of size 1 is expected.");
  141. // Try to create a strand of lights with a negative number
  142. Strand negativeStrand = new Strand(-7);
  143. if (negativeStrand.strand.size() == 1)
  144. System.out.println("*** PASS: Strand(-7) creates a list of size 1");
  145. else
  146. System.out.println("*** FAIL: Strand(-7) creates a list of size "
  147. + negativeStrand.strand.size()
  148. + ", when a list of size 1 is expected.");
  149. // Try to create a strand of lights with a positive number
  150. Strand strandWithFiveBulbs = new Strand(5);
  151. if (strandWithFiveBulbs.strand.size() == 5)
  152. System.out.println("*** PASS: Strand(5) creates a list of size 5");
  153. else
  154. System.out.println("*** FAIL: Strand(5) creates a list of size "
  155. + strandWithFiveBulbs.strand.size()
  156. + ", when a list of size 5 is expected.");
  157. // Verify that all the light bulbs are initialized properly
  158. boolean success = true;
  159. for (Light bulb : strandWithFiveBulbs.strand)
  160. {
  161. if (!(bulb.isOn() && bulb.getColor().equals("white")))
  162. {
  163. success = false;
  164. }
  165. }
  166. if (strandWithFiveBulbs.strand.size() > 0 && success)
  167. {
  168. System.out.println("*** PASS: Strand(5) initialized bulbs correctly");
  169. }
  170. else
  171. {
  172. System.out.println("*** FAIL: Strand(5) did not initialize bulb(s) correctly");
  173. }
  174.  
  175.  
  176. // ***********************************
  177. // 3. Test setColor(String)
  178. // ***********************************
  179. System.out.println("\n3. Test setColor(String)");
  180. // All of the bulbs in our strandWithFiveBulbs are white. Set them to
  181. // green.
  182. strandWithFiveBulbs.setColor("green");
  183. success = true;
  184. for (Light light : strandWithFiveBulbs.strand)
  185. {
  186. if (!light.getColor().equals("green"))
  187. success = false;
  188. }
  189. if (strandWithFiveBulbs.strand.size() > 0 && success)
  190. System.out.println("*** PASS: setColor worked as expected (green test)");
  191. else
  192. System.out.println("*** FAIL: setColor did not work as expected (green test)");
  193. // Now try to set them to a color that is not supported. This should
  194. // cause all the bulbs to be set back to white.
  195. strandWithFiveBulbs.setColor("pink");
  196. success = true;
  197. for (Light light : strandWithFiveBulbs.strand)
  198. {
  199. if (!light.getColor().equals("white"))
  200. success = false;
  201. }
  202. if (strandWithFiveBulbs.strand.size() > 0 && success)
  203. System.out.println("*** PASS: setColor worked as expected (pink test)");
  204. else
  205. System.out.println("*** FAIL: setColor did not work as expected (pink test)");
  206.  
  207. // ***********************************
  208. // 4. Test turnOff()
  209. // ***********************************
  210. System.out.println("\n4. Test turnOff()");
  211. strand1.turnOff();
  212. if (strand1.strand.size() > 0 && !strand1.strand.get(0).isOn())
  213. {
  214. System.out.println("*** PASS: turnOff() worked as expected");
  215. }
  216. else
  217. {
  218. System.out.println("*** FAIL: turnOff() did not work as expected");
  219. }
  220.  
  221. // ***********************************
  222. // 5. Test turnOn()
  223. // ***********************************
  224. System.out.println("\n5. Test turnOn()");
  225. strand1.turnOn();
  226. if (strand1.strand.size() > 0 && strand1.strand.get(0).isOn())
  227. {
  228. System.out.println("*** PASS: turnOn() worked as expected");
  229. }
  230. else
  231. {
  232. System.out.println("*** FAIL: turnOn() did not work as expected");
  233. }
  234.  
  235. // ***********************************
  236. // 6. Test burnOut(int)
  237. // ***********************************
  238. System.out.println("\n6. Test burnOut(n)");
  239. strand1.burnOut(0);
  240. if (strand1.toString().equals("off white\tburnt out\n"))
  241. {
  242. System.out.println("*** PASS: burnOut(1) works as expected.");
  243. }
  244. else
  245. {
  246. System.out.println("*** FAIL: burnOut(1) does not work as expected.");
  247. }
  248. }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement