Advertisement
StefanTobler

TERM 2 Assignment 2 Strand

Feb 2nd, 2017
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.88 KB | None | 0 0
  1. /*
  2.  
  3.  * AP CS MOOC
  4.  
  5.  * Term 2 - Assignment 2, Part 2: Strand
  6.  
  7.  * A class which represents a strand of lights.
  8.  
  9.  */
  10.  
  11.  
  12.  
  13. import java.util.ArrayList;
  14.  
  15.  
  16.  
  17. public class Strand
  18.  
  19. {
  20.  
  21.  // An ArrayList that stores a strand of lights
  22.  
  23.  private ArrayList<Light> strand = new ArrayList<Light>();
  24.  
  25.  
  26.  
  27.  // Default constructor that sets strand to an ArrayList holding one
  28.  
  29.  // turned on white bulb, that is not burnt out.
  30.  
  31.  public Strand()
  32.  
  33.  {
  34.  
  35.   this (1);
  36.  
  37.  }
  38.  
  39.  
  40.  
  41.  // A constructor that sets strand to an ArrayList of size n, holding
  42.  
  43.  // n white bulbs, that are all turned on and not burnt out. If n <= 0,
  44.  
  45.  // then the strand should be set to size one, with a white bulb, on
  46.  
  47.  // and not burnt out.
  48.  
  49.  public Strand(int n)
  50.  
  51.  {
  52.  
  53.   if (n <= 0)
  54.  
  55.   {
  56.  
  57.     n = 1;
  58.  
  59.   }
  60.  
  61.  
  62.  
  63.   for (int i = 1; i <= n; i++)
  64.  
  65.   {
  66.  
  67.    strand.add (new Light (true, false, "white"));
  68.  
  69.   }
  70.  
  71.    
  72.  
  73.  }
  74.  
  75.  
  76.  
  77.  // This method returns a String representation of the
  78.  
  79.  // Light objects in the ArrayList, one per line. For example,
  80.  
  81.  // here is the String returned when toString is called on a
  82.  
  83.  // Strand with 5 lights:
  84.  
  85.  //
  86.  
  87.  // green    on not burnt out
  88.  
  89.  // red    off not burnt out
  90.  
  91.  // green    off burnt out
  92.  
  93.  // blue    on not burnt out
  94.  
  95.  // red    on not burnt out
  96.  
  97.  //
  98.  
  99.  // Note: there is a tab between the value for color and "off"/"on,"
  100.  
  101.  // and one space before the "burnt out" or "not burnt out".
  102.  
  103.  public String toString()
  104.  
  105.  {
  106.  
  107.   String t = "";
  108.  
  109.  
  110.  
  111.    for (Light l: strand)
  112.  
  113.    {
  114.  
  115.      t += l.toString() + "\n";  
  116.  
  117.    }
  118.  
  119.   return t;
  120.  
  121.  }
  122.  
  123.  
  124.  
  125.  // This method sets the color of all the light bulbs in the entire Strand.
  126.  
  127.  public void setColor(String c)
  128.  
  129.  {
  130.  
  131.    for (Light l: strand)
  132.  
  133.    {
  134.  
  135.      l.setColor(c);
  136.  
  137.    }
  138.  
  139.  }
  140.  
  141.  
  142.  
  143.  // This method sets the light bulbs to the pattern "blue", "red", "green",
  144.  
  145.  // "blue", "red", "green", ... until the end of the strand.
  146.  
  147.  public void setMulti()
  148.  
  149.  {
  150.  
  151.   for (int i = 0; i < strand.size(); i++)
  152.  
  153.   {
  154.  
  155.     if (i % 3 == 0)
  156.  
  157.     {
  158.  
  159.       strand.get(i).setColor ("blue");
  160.  
  161.     }
  162.  
  163.     if (i % 3 == 1)
  164.  
  165.     {
  166.  
  167.       strand.get(i).setColor ("red");
  168.  
  169.     }
  170.  
  171.     if (i % 3 == 2)
  172.  
  173.     {
  174.  
  175.       strand.get(i).setColor ("green");
  176.  
  177.     }
  178.  
  179.   }
  180.  
  181.  }
  182.  
  183.  // This method turns on all the lights in the strand. Each individual bulb
  184.  
  185.  // can only be turned on if it's burntOut variable is false.
  186.  
  187.  public void turnOn()
  188.  
  189.  {
  190.  
  191.    for (Light l: strand)
  192.  
  193.    {
  194.  
  195.      if (!l.isOn())
  196.  
  197.      {
  198.  
  199.        l.flip();
  200.  
  201.      }
  202.  
  203.    }
  204.  
  205.  }
  206.  
  207.  // This method turns off all the lights in the strand.
  208.  
  209.  public void turnOff()
  210.  
  211.  {
  212.  
  213.   for (Light l: strand)
  214.  
  215.    {
  216.  
  217.      if (l.isOn())
  218.  
  219.      {
  220.  
  221.        l.flip();
  222.  
  223.      }
  224.  
  225.    }
  226.  
  227.  }
  228.  
  229.  
  230.  
  231.  // This method sets the Light at location i’s burntOut variable to true.
  232.  
  233.  public void burnOut(int i)
  234.  
  235.  {
  236.  
  237.   strand.get(i).burnOut();
  238.  
  239.  }
  240.  
  241.  
  242.  
  243.  public static void main(String[] args)
  244.  
  245.  {
  246.  
  247.   // *************************************************************************
  248.  
  249.   // 1. Test Strand()
  250.  
  251.   // *************************************************************************
  252.  
  253.   System.out.println("1. Test the default constructor Strand()");
  254.  
  255.   Strand strand1 = new Strand();
  256.  
  257.   if (strand1.strand.size() == 1)
  258.  
  259.    System.out.println("*** PASS: Strand() creates a list of size 1");
  260.  
  261.   else
  262.  
  263.    System.out.println("*** FAIL: Strand() creates a list of size "
  264.  
  265.      + strand1.strand.size()
  266.  
  267.      + ", when a list of size 1 is expected.");
  268.  
  269.  
  270.  
  271.   // ***********************************
  272.  
  273.   // 2. Test Strand(n)
  274.  
  275.   // ***********************************
  276.  
  277.   System.out.println("\n2. Test the constructor Strand(n)");
  278.  
  279.   // Try to create a strand of lights with 0 bulbs
  280.  
  281.   Strand emptyStrand = new Strand(0);
  282.  
  283.   if (emptyStrand.strand.size() == 1)
  284.  
  285.    System.out.println("*** PASS: Strand(0) creates a list of size 1");
  286.  
  287.   else
  288.  
  289.    System.out.println("*** FAIL: Strand(0)  creates a list of size "
  290.  
  291.      + emptyStrand.strand.size()
  292.  
  293.      + ", when a list of size 1 is expected.");
  294.  
  295.   // Try to create a strand of lights with a negative number
  296.  
  297.   Strand negativeStrand = new Strand(-7);
  298.  
  299.   if (negativeStrand.strand.size() == 1)
  300.  
  301.    System.out.println("*** PASS: Strand(-7) creates a list of size 1");
  302.  
  303.   else
  304.  
  305.    System.out.println("*** FAIL: Strand(-7) creates a list of size "
  306.  
  307.      + negativeStrand.strand.size()
  308.  
  309.      + ", when a list of size 1 is expected.");
  310.  
  311.   // Try to create a strand of lights with a positive number
  312.  
  313.   Strand strandWithFiveBulbs = new Strand(5);
  314.  
  315.   if (strandWithFiveBulbs.strand.size() == 5)
  316.  
  317.    System.out.println("*** PASS: Strand(5) creates a list of size 5");
  318.  
  319.   else
  320.  
  321.    System.out.println("*** FAIL: Strand(5) creates a list of size "
  322.  
  323.      + strandWithFiveBulbs.strand.size()
  324.  
  325.      + ", when a list of size 5 is expected.");
  326.  
  327.   // Verify that all the light bulbs are initialized properly
  328.  
  329.   boolean success = true;
  330.  
  331.   for (Light bulb : strandWithFiveBulbs.strand)
  332.  
  333.   {
  334.  
  335.    if (!(bulb.isOn() && bulb.getColor().equals("white")))
  336.  
  337.    {
  338.  
  339.     success = false;
  340.  
  341.    }
  342.  
  343.   }
  344.  
  345.   if (strandWithFiveBulbs.strand.size() > 0 && success)
  346.  
  347.   {
  348.  
  349.    System.out.println("*** PASS: Strand(5) initialized bulbs correctly");
  350.  
  351.   }
  352.  
  353.   else
  354.  
  355.   {
  356.  
  357.    System.out.println("*** FAIL: Strand(5) did not initialize bulb(s) correctly");
  358.  
  359.   }
  360.  
  361.  
  362.  
  363.  
  364.  
  365.   // ***********************************
  366.  
  367.   // 3. Test setColor(String)
  368.  
  369.   // ***********************************
  370.  
  371.   System.out.println("\n3. Test setColor(String)");
  372.  
  373.   // All of the bulbs in our strandWithFiveBulbs are white. Set them to
  374.  
  375.   // green.
  376.  
  377.   strandWithFiveBulbs.setColor("green");
  378.  
  379.   success = true;
  380.  
  381.   for (Light light : strandWithFiveBulbs.strand)
  382.  
  383.   {
  384.  
  385.    if (!light.getColor().equals("green"))
  386.  
  387.     success = false;
  388.  
  389.   }
  390.  
  391.   if (strandWithFiveBulbs.strand.size() > 0 && success)
  392.  
  393.    System.out.println("*** PASS: setColor worked as expected (green test)");
  394.  
  395.   else
  396.  
  397.    System.out.println("*** FAIL: setColor did not work as expected (green test)");
  398.  
  399.   // Now try to set them to a color that is not supported.  This should
  400.  
  401.   // cause all the bulbs to be set back to white.
  402.  
  403.   strandWithFiveBulbs.setColor("pink");
  404.  
  405.   success = true;
  406.  
  407.   for (Light light : strandWithFiveBulbs.strand)
  408.  
  409.   {
  410.  
  411.    if (!light.getColor().equals("white"))
  412.  
  413.     success = false;
  414.  
  415.   }
  416.  
  417.   if (strandWithFiveBulbs.strand.size() > 0 && success)
  418.  
  419.    System.out.println("*** PASS: setColor worked as expected (pink test)");
  420.  
  421.   else
  422.  
  423.    System.out.println("*** FAIL: setColor did not work as expected (pink test)");
  424.  
  425.  
  426.  
  427.  
  428.  
  429.   // ***********************************
  430.  
  431.   // 4. Test turnOff()
  432.  
  433.   // ***********************************
  434.  
  435.   System.out.println("\n4. Test turnOff()");
  436.  
  437.   strand1.turnOff();
  438.  
  439.   if (strand1.strand.size() > 0 && !strand1.strand.get(0).isOn())
  440.  
  441.   {
  442.  
  443.    System.out.println("*** PASS: turnOff() worked as expected");
  444.  
  445.   }
  446.  
  447.   else
  448.  
  449.   {
  450.  
  451.    System.out.println("*** FAIL: turnOff() did not work as expected");
  452.  
  453.   }
  454.  
  455.  
  456.  
  457.   // ***********************************
  458.  
  459.   // 5. Test turnOn()
  460.  
  461.   // ***********************************
  462.  
  463.   System.out.println("\n5. Test turnOn()");
  464.  
  465.   strand1.turnOn();
  466.  
  467.   if (strand1.strand.size() > 0 && strand1.strand.get(0).isOn())
  468.  
  469.   {
  470.  
  471.    System.out.println("*** PASS: turnOn() worked as expected");
  472.  
  473.   }
  474.  
  475.   else
  476.  
  477.   {
  478.  
  479.    System.out.println("*** FAIL: turnOn() did not work as expected");
  480.  
  481.   }
  482.  
  483.  
  484.  
  485.   // ***********************************
  486.  
  487.   // 6. Test burnOut(int)
  488.  
  489.   // ***********************************
  490.  
  491.   System.out.println("\n6. Test burnOut(n)");
  492.  
  493.   strand1.burnOut(0);
  494.  
  495.   if (strand1.toString().equals("white\toff burnt out\n"))
  496.  
  497.   {
  498.  
  499.    System.out.println("*** PASS: burnOut(1) works as expected.");
  500.  
  501.   }
  502.  
  503.   else
  504.  
  505.   {
  506.  
  507.    System.out.println("*** FAIL: burnOut(1) does not work as expected.");
  508.  
  509.   }
  510.  
  511.  }
  512.  
  513. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement