Chat with us, powered by LiveChat You are going to create a Light object through a constructor with three properties that allows you to instantiate a light using the Light class. The main method will then test your cons | Wridemy

You are going to create a Light object through a constructor with three properties that allows you to instantiate a light using the Light class. The main method will then test your cons

You are going to create a Light object through a constructor with three properties that allows you to instantiate a light using the Light class.

The main method will then test your constructor and methods for you. Make sure your output all is in Pass state and you are good. If anything is failing, be sure to check your outputs for correct formatting; does it need a n or space in the right place?

You will need to edit the Light object reference to reflect the Light+lastName that you will set your class and file name to.

Once you complete Light, it is time to program a Strand of Lights in the Strand project.  For part 2 of this project, you are to instantiate an array of Lights; therefore, the type used to form the array will be of type <Light> to store each of the properties for your Lights in the Strand array.  Strand also has a main pre-written for you that will test your Strand of Lights. You will only need to change the class object calls to Light+lastName and Strand+lastName in the main to match the changes you make to your Light and Strand naming.

You will find detailed instructions within each of the project files that you can download below.

/* * Part 2: Strand * A class which represents a strand of lights. * For this assignment, you will be writing a class called Strand that represents a strand of lights. * The lights in Strand are instances of the Light class that you created in Part 1 Light. *** Make sure you append your last name to Light to reflect what you created for your Light class *** * The Strand class needs the Light class in the same file folder to work properly. * As in Part 1 of this assignment, your job is to add code to Strand.java so that your implementation meets the requirements specified below. * To complete the assignment, replace all the /* missing code / comments in the file with your own code. * You will need to customize Light instantiations to reflect LightLastname and StrandLastname. * Please hand in Light ***AND*** Strand together so that I can test your creation. Variables ArrayList strand – An ArrayList that stores a strand of lights based on the Light class. ArrayList <LightLastName> creates the Light type to be store in each ArrayList element. Methods Strand() – Default constructor that sets strand to an ArrayList holding one turned on white bulb, that is not burnt out. Strand(int n) – A constructor that sets strand to an ArrayList of size n, holding n white bulbs, that are all turned on and not burnt out. If n <= 0, then the strand should be set to size one, with a white bulb, on and not burnt out. String toString() – This method returns a String representation of the Light objects in the ArrayList , one per line. For example: on green not burnt out off red not burnt out off green burnt out on blue not burnt out on red not burnt out void setColor(String c) – This method sets the color of all the light bulbs in the entire Strand. The color variable is set to c only if c is "red", "green" or "blue". It should ignore the case of the value in c. If c holds any value other than "red", "green" or "blue", color will be set to "white". void setMulti() – This method sets the light bulbs to the pattern "red", "green", "blue", "red", "green", "blue",… until the end of the strand. void turnOn() – This method turns on all the lights in the strand. Each individual bulb can only be turned on if it's burntOut variable is false. void turnOff() – This method turns off all the lights in the strand. void burnOut(int i) – This method sets the Light at location i's burntOut variable to true. Sample Run: 1. Test the default constructor Strand() *** PASS: Strand() creates a list of size 1 2. Test the constructor Strand(n) *** PASS: Strand(0) creates a list of size 1 *** PASS: Strand(-7) creates a list of size 1 *** PASS: Strand(5) creates a list of size 5 *** PASS: Strand(5) initialized bulbs correctly 3. Test setColor(String) *** PASS: setColor worked as expected (green test) *** PASS: setColor worked as expected (pink test) 4. Test turnOff() *** PASS: turnOff() worked as expected 5. Test turnOn() *** PASS: turnOn() worked as expected 6. Test burnOut(n) *** PASS: burnOut(1) works as expected. */ import java.util.ArrayList; public class Strand { // An ArrayList that stores a strand of lights based on the Light class, i.e. Light type private ArrayList<Light> strand = new ArrayList<Light>(); // Default constructor that sets strand to an ArrayList holding one // turned on white bulb, that is not burnt out. /* missing code */ // A constructor that sets strand to an ArrayList of size n, holding // n white bulbs, that are all turned on and not burnt out. If n <= 0, // then the strand should be set to size one, with a white bulb, on // and not burnt out. /* missing code */ // This method returns a String representation of the // Light objects in the ArrayList, one per line. For example, // here is the String returned when toString is called on a // Strand with 5 lights: // // on green not burnt out // off red not burnt out // off green burnt out // on blue not burnt out // on red not burnt out // // Note: there is one space between "off"/"on" and the value for // color, and a tab before the "burnt out" or "not burnt out". /* missing code */ // This method sets the color of all the light bulbs in the entire Strand. /* missing code */ // This method sets the light bulbs to the pattern "red", "green", "blue", // "red", "green", "blue",… until the end of the strand. /* missing code */ // This method turns on all the lights in the strand. Each individual bulb // can only be turned on if it's burntOut variable is false. /* missing code */ // This method turns off all the lights in the strand. /* missing code */ // This method sets the Light at location i's burntOut variable to true. /* missing code */ //**************** DO NOT EDIT BELOW HERE EXCEPT TO CHANGE Strand TO REFLECT CLASS/FILE NAME **************** public static void main(String[] args) { // ************************************************************************* // 1. Test Strand() // ************************************************************************* System.out.println("1. Test the default constructor Strand()"); Strand strand1 = new Strand(); if (strand1.strand.size() == 1) System.out.println("*** PASS: Strand() creates a list of size 1"); else System.out.println("*** FAIL: Strand() creates a list of size " + strand1.strand.size() + ", when a list of size 1 is expected."); // *********************************** // 2. Test Strand(n) // *********************************** System.out.println("n2. Test the constructor Strand(n)"); // Try to create a strand of lights with 0 bulbs Strand emptyStrand = new Strand(0); if (emptyStrand.strand.size() == 1) System.out.println("*** PASS: Strand(0) creates a list of size 1"); else System.out.println("*** FAIL: Strand(0) creates a list of size " + emptyStrand.strand.size() + ", when a list of size 1 is expected."); // Try to create a strand of lights with a negative number Strand negativeStrand = new Strand(-7); if (negativeStrand.strand.size() == 1) System.out.println("*** PASS: Strand(-7) creates a list of size 1"); else System.out.println("*** FAIL: Strand(-7) creates a list of size " + negativeStrand.strand.size() + ", when a list of size 1 is expected."); // Try to create a strand of lights with a positive number Strand strandWithFiveBulbs = new Strand(5); if (strandWithFiveBulbs.strand.size() == 5) System.out.println("*** PASS: Strand(5) creates a list of size 5"); else System.out.println("*** FAIL: Strand(5) creates a list of size " + strandWithFiveBulbs.strand.size() + ", when a list of size 5 is expected."); // Verify that all the light bulbs are initialized properly boolean success = true; for (Light bulb : strandWithFiveBulbs.strand) { if (!(bulb.isOn() && bulb.getColor().equals("white"))) { success = false; } } if (strandWithFiveBulbs.strand.size() > 0 && success) { System.out.println("*** PASS: Strand(5) initialized bulbs correctly"); } else { System.out.println("*** FAIL: Strand(5) did not initialize bulb(s) correctly"); } // *********************************** // 3. Test setColor(String) // *********************************** System.out.println("n3. Test setColor(String)"); // All of the bulbs in our strandWithFiveBulbs are white. Set them to // green. strandWithFiveBulbs.setColor("green"); success = true; for (Light light : strandWithFiveBulbs.strand) { if (!light.getColor().equals("green")) success = false; } if (strandWithFiveBulbs.strand.size() > 0 && success) System.out.println("*** PASS: setColor worked as expected (green test)"); else System.out.println("*** FAIL: setColor did not work as expected (green test)"); // Now try to set them to a color that is not supported. This should // cause all the bulbs to be set back to white. strandWithFiveBulbs.setColor("pink"); success = true; for (Light light : strandWithFiveBulbs.strand) { if (!light.getColor().equals("white")) success = false; } if (strandWithFiveBulbs.strand.size() > 0 && success) System.out.println("*** PASS: setColor worked as expected (pink test)"); else System.out.println("*** FAIL: setColor did not work as expected (pink test)"); // *********************************** // 4. Test turnOff() // *********************************** System.out.println("n4. Test turnOff()"); strand1.turnOff(); if (strand1.strand.size() > 0 && !strand1.strand.get(0).isOn()) { System.out.println("*** PASS: turnOff() worked as expected"); } else { System.out.println("*** FAIL: turnOff() did not work as expected"); } // *********************************** // 5. Test turnOn() // *********************************** System.out.println("n5. Test turnOn()"); strand1.turnOn(); if (strand1.strand.size() > 0 && strand1.strand.get(0).isOn()) { System.out.println("*** PASS: turnOn() worked as expected"); } else { System.out.println("*** FAIL: turnOn() did not work as expected"); } // *********************************** // 6. Test burnOut(int) // *********************************** System.out.println("n6. Test burnOut(n)"); strand1.burnOut(0); if (strand1.toString().equals("off whitetburnt outn")) { System.out.println("*** PASS: burnOut(1) works as expected."); } else { System.out.println("*** FAIL: burnOut(1) does not work as expected."); } // ************************************ // 7. Test setMulti() // ************************************ System.out.println("n7. Test setMulti(n)"); // Try to create a strand of lights with a positive number Strand sw12b = new Strand(12); //test if strand was created if (sw12b.strand.size() == 12) System.out.println("*** PASS: StrandAnswer(12) creates a list of size 12"); else System.out.println("*** FAIL: StrandAnswer(12) creates a list of size " + sw12b.strand.size() + ", when a list of size 12 is expected."); //change all bulbs colors to the multi color pattern sw12b.setMulti(); // Verify that all the light bulbs are initialized properly success = true; for (int i = 0; i < sw12b.strand.size(); i++){ if(i % 3 == 0){ if(!(sw12b.strand.get(i).isOn() && sw12b.strand.get(i).getColor().equals("red"))){ success = false; } } else if(i % 3 == 1){ if(!(sw12b.strand.get(i).isOn() && sw12b.strand.get(i).getColor().equals("green"))){ success = false; } } else{ if(!(sw12b.strand.get(i).isOn() && sw12b.strand.get(i).getColor().equals("blue"))){ success = false; } } } //print pass or fail based on logic if (sw12b.strand.size() > 0 && success){ System.out.println("*** PASS: StrandAnswer(12) initialized bulbs correctly"); } else{ System.out.println("*** FAIL: StrandAnswer(12) did not initialize bulb(s) correctly"); } } }

,

/* * Part 1: Light * A class which represents a single light bulb. For this assignment, you will be writing a class called Light that represents a single light bulb. Your job is to add code to Light.java so that your implementation meets the requirements specified below. To complete the assignment, replace all the /* missing code (*)/ comments in the file with your own code. *** You will need to customize Light to reflect LightLastname, e.g. LightIlkenhons as the file name, class name, and all instantiations in the test code at the bottom; use find and replace with capital sensitive. In this assignment, the class is required to be named Light<Lastname>. Light.java includes a main method that will help test your code. Run the main to make sure your implementation output matches the sample run listed below. Variables: boolean on – Represents whether the light bulb is on or off. Set to true if the light bulb is on, false if off. boolean burntOut – Represents whether the light is burnt out or working properly. Set to true if the light bulb is burnt out, false if it is working. String color – Represents the color of the light bulb with possible values of "red", "blue", "green" and "white". No other color values are allowed. Methods: Light() – Default constructor that sets the bulb to on, not burnt out, and "white". Light(boolean o, boolean b, String c) – This constructor sets the on variable to the parameter o. The burntOut variable is set to the parameter b. If burntOut is true, on is set to false, no matter what value is stored in o. The color variable is set to c only if c is "red", "green" or "blue". The constructor ignores the case of the value in c, and stores the value as a lower-case String. If c holds any value other than "red", "green" or "blue", color will be set to "white". String toString () – returns a String with the Light in the format: off red burnt out on green not burnt out Notice there is one space between "off"/"on" and the value for color, and a tab before the "burnt out" or "not burnt out". void flip() – This method changes the bulb from on to off, or visa versa. If the burntOut variable is true, then the on variable may only be set to false. String getColor() – This method returns the color of the light bulb. void setColor(String c) – The color variable is set to c only if c is "red", "green" or "blue", ignoring case. The value stored for color should be lower case. If c holds any value other than "red", "green" or "blue" (ignoring case), the method sets the color to "white". boolean isOn() – Returns true if on, false otherwise. void burnOut() – Sets burntOut to true. Sample Run: 1. Test Light() *** PASS: on is set correctly (true) *** PASS: burntOut is set correctly (false) *** PASS: color is set correctly (white) *** PASS: toString produced the correct output (on white not burnt out) 2. Test Light(boolean b, boolean o, String c) *** PASS: on is set correctly (false) *** PASS: burntOut is set correctly (true) *** PASS: color is set correctly (green) *** PASS: toString produced the correct output (off green burnt out) 3. Test burnOut() *** PASS: on is set correctly (false) *** PASS: burntOut is set correctly (true) *** PASS: color is set correctly (white) *** PASS: toString produced the correct output (off white burnt out) 4. Test flip() light3 is on *** PASS: on is set correctly (true) *** PASS: burntOut is set correctly (false) *** PASS: color is set correctly (white) *** PASS: toString produced the correct output (on white not burnt out) now light3 should be off *** PASS: on is set correctly (false) *** PASS: burntOut is set correctly (false) *** PASS: color is set correctly (white) *** PASS: toString produced the correct output (off white not burnt out) now light3 should be back on *** PASS: on is set correctly (true) *** PASS: burntOut is set correctly (false) *** PASS: color is set correctly (white) *** PASS: toString produced the correct output (on white not burnt out) light1 is burned out and off, we can't flip it on *** PASS: on is set correctly (false) *** PASS: burntOut is set correctly (true) *** PASS: color is set correctly (white) *** PASS: toString produced the correct output (off white burnt out) 5. Test isOn() *** PASS: isOn() working properly *** PASS: isOn() working properly 6. Test getColor() *** PASS: getColor() working properly 7. Test setColor(String) *** PASS: color is set correctly (red) *** PASS: color is set correctly (blue) *** PASS: color is set correctly (white) */ public class Light{ // Variables that will be initialized in the Light constructors. /* missing code */ // Default constructor that sets the bulb to on, not burnt out, and "white". /* missing code */ // This constructor sets the variable "on" to the parameter o. The burntOut variable is set to the parameter b. // If burntOut is true, on is set to false, no matter what value is stored in o. // The color variable is set to the parameter c only if c is "red", "green" or "blue". The constructor ignores the case of the value in c. // If c holds any value other than "red", "green" or "blue", the constructor sets color to "white". /* missing code */ // The toString method returns a String with the Light in the format: // off red burnt out // on green not burnt out // // Notice there is one space between "off"/"on" and the value for color, // and a tab (t, not tab key) before the "burnt out" or "not burnt out". /* missing code */ // This method changes the bulb from on to off, or visa versa. If the // burntOut variable is true, then the on variable may only be set to false. /* missing code */ // The getColor method returns the color of the bulb. /* missing code */ // The setColor method sets the color of the Light. The color variable is // set to c only if c is "red", "green" or "blue". The method ignore the // case of the value in c. If c holds any value other than "red", "green" // or "blue", color will be set to "white". /* missing code */ // The isOn method returns true if on, false otherwise. /* missing code */ // The burnOut method sets the variable burntOut to true, is there another consideration when true… /* missing code */ //****************DO NOT EDIT THE CODE BELOW, UNLESS YOU ARE CHANGING Light to Light<LastName>**************** public static void main(String[] args) { /* The main method allows you to run Light on its own, with a built-in tester. */ //************************************************************************* // 1. Test Light() //************************************************************************* Light light1 = new Light(); System.out.println("1. Test Light()"); testLight(light1, true, false, "white", "on whitetnot burnt out"); //************************************************************************* // 2. Test Light(boolean b, boolean o, String c) //************************************************************************* System.out.println("n2. Test Light(boolean b, boolean o, String c)"); Light light2 = new Light(true, true, "GreeN"); // Notice that since the light bulb is "burnt out", the value of "on" // gets set to false. Also, the color should get saved in all lower case // as "green", not "GreeN". testLight(light2, false, true, "green", "off greentburnt out"); //************************************************************************* // 3. Test burnOut() //************************************************************************* System.out.println("n3. Test burnOut()"); // light1 is not burnt out. Lets call burnOut on light1 and make sure it gets burnt out and turned off light1.burnOut(); testLight(light1, false, true, "white", "off whitetburnt out"); //************************************************************************* // 4. Test flip() //************************************************************************* System.out.println("n4. Test flip()"); Light light3 = new Light(); // light3 is currently on and not burnt out. Lets flip the light off and on and see if it works properly. System.out.println("light3 is on"); testLight(light3, true, false, "white", "on whitetnot burnt out"); light3.flip(); System.out.println("now light3 should be off"); testLight(light3, false, false, "white", "off whitetnot burnt out"); light3.flip(); System.out.println("now light3 should be back on"); testLight(light3, true, false, "white", "on whitetnot burnt out"); // Try to flip light1 on – this should fail since light1 is burnt out. light1 should stay off System.out.println("light1 is burned out and off, we can't flip it on"); light1.flip(); testLight(light1, false, true, "white", "off whitetburnt out"); //************************************************************************* // 5. Test isOn() //************************************************************************* System.out.println("n5. Test isOn()"); // We know light1 is off, and light3 is on. Verify that the method isOn reports this correctly. if (!light1.isOn()){ System.out.println("*** PASS: isOn() working properly"); } else { System.out.println("*** FAIL: isOn() not working properly"); } if (light3.isOn()){ System.out.println("*** PASS: isOn() working properly"); } else { System.out.println("*** FAIL: isOn() not working properly"); } //************************************************************************* // 6. Test getColor() //************************************************************************* System.out.println("n6. Test getColor()"); if (light1.getColor().equals("white")){ System.out.println("*** PASS: getColor() working properly"); } else { System.out.println("*** FAIL: problem with getColor()"); } //************************************************************************* // 7. Test setColor(String) //************************************************************************* System.out.println("n7. Test setColor(String)"); light1.setColor("red"); System.out.println("*** " + testLightColor(light1, "red")); light1.setColor("BLUE"); // should set light to blue System.out.println("*** " + testLightColor(light1, "blue")); light1.setColor("yellow"); // yellow is not allowed, should set light to white System.out.println("*** " + testLightColor(light1, "white")); } // Private helper methods private static void testLight(Light light, boolean o, boolean b, String c, String string) { System.out.println("*** " + testLightOn(light, o)); System.out.println("*** " + testLightburntOut(light, b)); System.out.println("*** " + testLightColor(light, c)); System.out.println("*** " + testLightToString(light, string)); } private static String testLightOn(Light bulb, boolean o){ if ((bulb.on && !o) || (!bulb.on && o)){ return "FAIL: on is not set correctly. on should be set to " + o + ", but it is set to " + bulb.on + "."; } else{ return "PASS: on is set correctly (" + bulb.on + ")"; } } private static String testLightburntOut(Light light, boolean b){ if ((light.burntOut && !b) || (!light.burntOut && b)){ return "FAIL: burntOut is not set correctly (burntOut should be set to " + b + ", but it is set to " + light.burntOut + ")"; } else{ return "PASS: burntOut is set correctly (" + light.burntOut + ")"; } } private static String testLightColor(Light light, String c){ if (!light.color.equals(c)){ return "FAIL: color is not set correctly (color should be set to " + c + ", but it is set to " + light.color + ")"; } else{ return "PASS: color is set correctly (" + light.color + ")"; } } private static String testLightToString(Light light, String string){ String output; if (light.toString().equals(string)){ output = "PASS: toString produced the correct output"; } else{ output = "FAIL: toString does not work as expected"; } return output + " (" + light.toString() + ")"; } }

Our website has a team of professional writers who can help you write any of your homework. They will write your papers from scratch. We also have a team of editors just to make sure all papers are of HIGH QUALITY & PLAGIARISM FREE. To make an Order you only need to click Ask A Question and we will direct you to our Order Page at WriteDemy. Then fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.

Fill in all the assignment paper details that are required in the order form with the standard information being the page count, deadline, academic level and type of paper. It is advisable to have this information at hand so that you can quickly fill in the necessary information needed in the form for the essay writer to be immediately assigned to your writing project. Make payment for the custom essay order to enable us to assign a suitable writer to your order. Payments are made through Paypal on a secured billing page. Finally, sit back and relax.

Do you need an answer to this or any other questions?

About Wridemy

We are a professional paper writing website. If you have searched a question and bumped into our website just know you are in the right place to get help in your coursework. We offer HIGH QUALITY & PLAGIARISM FREE Papers.

How It Works

To make an Order you only need to click on “Order Now” and we will direct you to our Order Page. Fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.

Are there Discounts?

All new clients are eligible for 20% off in their first Order. Our payment method is safe and secure.

Hire a tutor today CLICK HERE to make your first order

Related Tags

Academic APA Writing College Course Discussion Management English Finance General Graduate History Information Justify Literature MLA