Advertisement
StefanTobler

TERM 2 Assignment 2 Light

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