Advertisement
TheEpicKiller

Assignment 2 WIP 7/10

Feb 1st, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.19 KB | None | 0 0
  1. /*
  2. * AP CS MOOC
  3. * Term 2 - Assignment 2, Part 1: Light
  4. * A class which represents a single light bulb.
  5. */
  6.  
  7. public class Light
  8. {
  9. // Variables that will be initialized in the Light constructors.
  10. private boolean on;
  11. private boolean burntOut;
  12. private String color = "";
  13.  
  14. // Default constructor that sets the bulb to on, not burnt out, and "white".
  15. public Light()
  16. {
  17. on = true;
  18. burntOut = false;
  19. color = "white";
  20. }
  21.  
  22. // This constructor sets the variable "on" to the parameter o. The burntOut
  23. // variable is set to the parameter b. If burntOut
  24. // is true, on is set to false, no matter what value is stored in o.
  25. // The color variable is set to the parameter c only if c is "red", "green"
  26. // or "blue". The constructor ignores the case of the value in c. If c holds
  27. // any value other than "red", "green" or "blue", the constructor sets
  28. // color to "white".
  29. public Light(boolean o, boolean b, String c)
  30. {
  31. if (o == true)
  32. on = true;
  33. if (o == false)
  34. on = false;
  35. if (b == true)
  36. burntOut = true;
  37. else
  38. burntOut = false;
  39. if (burntOut = true)
  40. {
  41. on = false;
  42. }
  43. String lowercolor = c.toLowerCase();
  44. if (lowercolor.equals("red") || lowercolor.equals("green") || lowercolor.equals("blue"))
  45. {
  46. color = c;
  47. }
  48. else
  49. color = "white";
  50. }
  51.  
  52. // The toString method returns a String with the Light in the format:
  53. // off red burnt out
  54. // on green not burnt out
  55. //
  56. // Notice there is one space between "off"/"on" and the value for color,
  57. // and a tab before the "burnt out" or "not burnt out".
  58. public String toString()
  59. {
  60. String light = "";
  61. String onoff = "";
  62. String outburn = "";
  63. if (burntOut == true)
  64. {
  65. outburn = "burnt out";
  66. onoff = "off";
  67. }
  68. else
  69. {
  70. outburn = "not burnt out";
  71. }
  72. if (on == true)
  73. onoff = "on";
  74. else
  75. onoff = "off";
  76. light = ""+onoff+" "+color+" "+outburn;
  77. return light;
  78. }
  79.  
  80. // This method changes the bulb from on to off, or visa versa. If the
  81. // burntOut variable is true, then the on variable may only be set to false.
  82. public void flip()
  83. {
  84. if (on == true)
  85. on = false;
  86. else if (on == false)
  87. on = true;
  88. if (burntOut == true)
  89. on = false;
  90. }
  91.  
  92. // The getColor method returns the color of the bulb.
  93. public String getColor()
  94. {
  95. String colorimeter = "";
  96. String lowercolor = color.toLowerCase();
  97. if (lowercolor.equals("red"))
  98. colorimeter = "red";
  99. else if (lowercolor.equals("blue"))
  100. colorimeter = "blue";
  101. else if (lowercolor.equals("green"))
  102. colorimeter = "green";
  103. else
  104. colorimeter = "white";
  105. return colorimeter;
  106. }
  107.  
  108. // The setColor method sets the color of the Light. The color variable is
  109. // set to c only if c is "red", "green" or "blue". The method ignore the
  110. // case of the value in c. If c holds any value other than "red", "green"
  111. // or "blue", color will be set to "white".
  112. public void setColor(String c)
  113. {
  114. String lowercolor = c.toLowerCase();
  115. if (lowercolor.equals("red") || lowercolor.equals("green") || lowercolor.equals("blue"))
  116. color = c;
  117. else
  118. color = "white";
  119. }
  120.  
  121. // The isOn method returns true if on, false otherwise.
  122. public boolean isOn()
  123. {
  124. if (on == true)
  125. return true;
  126. else
  127. return false;
  128. }
  129.  
  130. // The burnOut method sets the variable burntOut to true.
  131. public void burnOut()
  132. {
  133. burntOut = true;
  134. on = false;
  135. }
  136.  
  137. public static void main(String[] args)
  138. {
  139.  
  140. /* The main method allows you to run Light on its own, with a built-in tester. */
  141.  
  142. //*************************************************************************
  143. // 1. Test Light()
  144. //*************************************************************************
  145. Light light1 = new Light();
  146. System.out.println("1. Test Light()");
  147. testLight(light1, true, false, "white", "on white\tnot burnt out");
  148.  
  149. //*************************************************************************
  150. // 2. Test Light(boolean b, boolean o, String c)
  151. //*************************************************************************
  152. System.out.println("\n2. Test Light(boolean b, boolean o, String c)");
  153. Light light2 = new Light(true, true, "GreeN");
  154. // Notice that since the light bulb is "burnt out", the value of "on"
  155. // gets set to false. Also, the color should get saved in all lower case
  156. // as "green", not "GreeN".
  157. testLight(light2, false, true, "green", "off green\tburnt out");
  158.  
  159.  
  160. //*************************************************************************
  161. // 3. Test burnOut()
  162. //*************************************************************************
  163. System.out.println("\n3. Test burnOut()");
  164. // light1 is not burnt out. Lets call burnOut on light1 and make sure it gets burnt out and turned off
  165. light1.burnOut();
  166. testLight(light1, false, true, "white", "off white\tburnt out");
  167.  
  168.  
  169. //*************************************************************************
  170. // 4. Test flip()
  171. //*************************************************************************
  172. System.out.println("\n4. Test flip()");
  173. Light light3 = new Light();
  174. // light3 is currently on and not burnt out. Lets flip the light off and on and see if it works properly.
  175. System.out.println("light3 is on");
  176. testLight(light3, true, false, "white", "on white\tnot burnt out");
  177. light3.flip();
  178. System.out.println("now light3 should be off");
  179. testLight(light3, false, false, "white", "off white\tnot burnt out");
  180. light3.flip();
  181. System.out.println("now light3 should be back on");
  182. testLight(light3, true, false, "white", "on white\tnot burnt out");
  183. // Try to flip light1 on - this should fail since light1 is burnt out. light1 should stay off
  184. System.out.println("light1 is burned out and off, we can't flip it on");
  185. light1.flip();
  186. testLight(light1, false, true, "white", "off white\tburnt out");
  187.  
  188. //*************************************************************************
  189. // 5. Test isOn()
  190. //*************************************************************************
  191. System.out.println("\n5. Test isOn()");
  192. // We know light1 is off, and light3 is on. Verify that the method isOn reports this correctly.
  193. if (!light1.isOn())
  194. {
  195. System.out.println("*** PASS: isOn() working properly");
  196. }
  197. else
  198. {
  199. System.out.println("*** FAIL: isOn() not working properly");
  200. }
  201. if (light3.isOn())
  202. {
  203. System.out.println("*** PASS: isOn() working properly");
  204. }
  205. else
  206. {
  207. System.out.println("*** FAIL: isOn() not working properly");
  208. }
  209.  
  210. //*************************************************************************
  211. // 6. Test getColor()
  212. //*************************************************************************
  213. System.out.println("\n6. Test getColor()");
  214. if (light1.getColor().equals("white"))
  215. {
  216. System.out.println("*** PASS: getColor() working properly");
  217. }
  218. else
  219. {
  220. System.out.println("*** FAIL: problem with getColor()");
  221. }
  222.  
  223. //*************************************************************************
  224. // 7. Test setColor(String)
  225. //*************************************************************************
  226. System.out.println("\n7. Test setColor(String)");
  227. light1.setColor("red");
  228. System.out.println("*** " + testLightColor(light1, "red"));
  229. light1.setColor("BLUE"); // should set light to blue
  230. System.out.println("*** " + testLightColor(light1, "blue"));
  231. light1.setColor("yellow"); // yellow is not allowed, should set light to white
  232. System.out.println("*** " + testLightColor(light1, "white"));
  233. }
  234.  
  235. // Private helper methods
  236.  
  237. private static void testLight(Light light, boolean o, boolean b, String c,
  238. String string)
  239. {
  240. System.out.println("*** " + testLightOn(light, o));
  241. System.out.println("*** " + testLightburntOut(light, b));
  242. System.out.println("*** " + testLightColor(light, c));
  243. System.out.println("*** " + testLightToString(light, string));
  244. }
  245.  
  246. private static String testLightOn(Light bulb, boolean o)
  247. {
  248. if ((bulb.on && !o) || (!bulb.on && o))
  249. {
  250. return "FAIL: on is not set correctly. on should be set to "
  251. + o + ", but it is set to " + bulb.on + ".";
  252. }
  253. else
  254. {
  255. return "PASS: on is set correctly (" + bulb.on + ")";
  256. }
  257. }
  258.  
  259. private static String testLightburntOut(Light light, boolean b)
  260. {
  261. if ((light.burntOut && !b) || (!light.burntOut && b))
  262. {
  263. return "FAIL: burntOut is not set correctly (burntOut should be set to "
  264. + b + ", but it is set to " + light.burntOut + ")";
  265. }
  266. else
  267. {
  268. return "PASS: burntOut is set correctly (" + light.burntOut + ")";
  269. }
  270. }
  271.  
  272. private static String testLightColor(Light light, String c)
  273. {
  274. if (!light.color.equals(c))
  275. {
  276. return "FAIL: color is not set correctly (color should be set to "
  277. + c + ", but it is set to " + light.color + ")";
  278. }
  279. else
  280. {
  281. return "PASS: color is set correctly (" + light.color + ")";
  282. }
  283. }
  284.  
  285. private static String testLightToString(Light light, String string)
  286. {
  287. String output;
  288.  
  289. if (light.toString().equals(string))
  290. {
  291. output = "PASS: toString produced the correct output";
  292. }
  293. else
  294. {
  295. output = "FAIL: toString does not work as expected";
  296. }
  297. return output + " (" + light.toString() + ")";
  298. }
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement