Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Computer Science 1- Java Please include all the code for my reference. This is my set up. PLEASE add comments for easier understanding. Paste the

Computer Science 1- Java

Please include all the code for my reference. This is my set up. PLEASE add comments for easier understanding. Paste the whole code as plain text as the answer. Only make changes to file "Light.java" and "Strand.java". PLEASE don't import anything new and keep it simple. The output of the program has to be the same as the sample runs in the rubric.

---------------------------------------------------------------------------------

public class Light { private boolean on; private boolean burntOut; private String color = ""; public Light() { } public Light(boolean o, boolean b, String c) { } public String toString() { } public void flip() { } public String getColor() { } public void setColor(String c) { } public boolean isOn() { } public void burnOut() { } public boolean isBurntOut() { } }

--------------------------------------------------------------------------------------

import java.util.ArrayList; public class Strand { private ArrayList strand; public Strand() { } public Strand(int n) { } public String toString() { } public void setColor(String c) { } public void setMulti() { } public void turnOn() { } public void turnOff() { } public void burnOut(int i) { } public int getSize() { } }

-------------------------------------------------------------------------------------------------

public class LightStrandTester { public static void main(String[] args) { //Tests for Light class: //************************************************************************* // 1. Test Light() //************************************************************************* Light light1 = new Light(); System.out.println("1. Create light one with default constructor"); System.out.println(light1); System.out.println(" Test burnOut on light 1"); light1.burnOut(); System.out.println(light1); //************************************************************************* // 2. Test Light(boolean b, boolean o, String c) //************************************************************************* System.out.println(" 2. Create Light2 with other constructor"); Light light2 = new Light(true, true, "GreeN"); System.out.println(light2); //************************************************************************* // 3. Test flip() //************************************************************************* // light3 is currently on and not burnt out. Lets flip the light off and on and see if it works properly.System.out.println(" 4. Test flip()"); Light light3 = new Light(); System.out.println(" 3. Create Light3 with default constructor"); System.out.println(light3); light3.flip(); System.out.println("Flip switch to turn Light3 off"); System.out.println(light3); light3.flip(); System.out.println("Flip switch to turn Light3 on"); System.out.println(light3); // Try to flip light1 on - this should fail since light1 is burnt out. light1 should stay off System.out.println("Try to flip light1 on - this should fail since light1 is burnt out."); light1.flip(); System.out.println(light1); //************************************************************************* // 4. Test isOn() //************************************************************************* System.out.println(" 4. 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"); } //************************************************************************* // 5. Test getColor() //************************************************************************* System.out.println(" 5. Test getColor()"); if (light1.getColor().equals("white")) { System.out.println("*** PASS: getColor() working properly"); } else { System.out.println("*** FAIL: problem with getColor()"); } //************************************************************************* // 6. Test setColor(String) //************************************************************************* System.out.println(" 6. 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")); System.out.println(" below are tests for Strand class"); // ************************************************************************* // 7. Test Strand() // ************************************************************************* System.out.println("7. Test the default constructor Strand()"); Strand strand1 = new Strand(); if (strand1.getSize() == 1) System.out.println("*** PASS: Strand() creates a strand of size 1"); else System.out.println("*** FAIL: Strand() creates a strand of size " + strand1.getSize() + ", when a strand of size 1 is expected."); // *********************************** // 8. Test Strand(n) // *********************************** System.out.println(" 8. Test the constructor Strand(n)"); // Try to create a strand of lights with 0 bulbs Strand emptyStrand = new Strand(0); if (emptyStrand.getSize() == 1) System.out.println("*** PASS: Strand(0) creates a strand of size 1"); else System.out.println("*** FAIL: Strand(0) creates a strand of size " + emptyStrand.getSize() + ", when a strand of size 1 is expected."); // Try to create a strand of lights with a negative number Strand negativeStrand = new Strand(-7); if (negativeStrand.getSize() == 1) System.out.println("*** PASS: Strand(-7) creates a strand of size 1"); else System.out.println("*** FAIL: Strand(-7) creates a strand of size " + negativeStrand.getSize() + ", when a strand of size 1 is expected."); // Try to create a strand of lights with a positive number Strand strandWithFiveBulbs = new Strand(5); if (strandWithFiveBulbs.getSize() == 5) System.out.println("*** PASS: Strand(5) creates a strand of size 5"); else System.out.println("*** FAIL: Strand(5) creates a strand of size " + strandWithFiveBulbs.getSize() + ", when a strand of size 5 is expected."); // Verify that all the light bulbs are initialized properly System.out.println(" Strand(5) should be all on and white"); System.out.println(strandWithFiveBulbs); // *********************************** // 9. Test setColor(String) // *********************************** System.out.println(" 9. Test setColor(String)"); // All of the bulbs in our strandWithFiveBulbs are white. Set them to green. strandWithFiveBulbs.setColor("green"); System.out.println("Strand(5) should be all on and green"); System.out.println(strandWithFiveBulbs); // All of the bulbs in our strandWithFiveBulbs are white. Set them to multi. strandWithFiveBulbs.setMulti(); System.out.println("Strand(5) should be all on and multi - red, green, blue, red, green"); System.out.println(strandWithFiveBulbs); // 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"); System.out.println("Strand(5) should be all on and white again"); System.out.println(strandWithFiveBulbs); // *********************************** // 10. Test turnOff() // *********************************** System.out.println(" 10. Test turnOff()"); strandWithFiveBulbs.turnOff(); System.out.println("Strand(5) should be all off and white"); System.out.println(strandWithFiveBulbs); // *********************************** // 11. Test turnOn() // *********************************** System.out.println(" 11. Test turnOn()"); strandWithFiveBulbs.turnOn(); System.out.println("Strand(5) should be all on and white"); System.out.println(strandWithFiveBulbs); // *********************************** // 12. Test burnOut(int) // *********************************** System.out.println(" 12. Test burnOut(n)"); strandWithFiveBulbs.burnOut(0); System.out.println("the first bulb in Strand(5) should be burnt out and off"); System.out.println(strandWithFiveBulbs); } // Private helper methods private static String testLightColor(Light light, String c) { if (!light.getColor().equals(c)) { return "FAIL: color is not set correctly (color should be set to " + c + ", but it is set to " + light.getColor() + ")"; } else { return "PASS: color is set correctly (" + light.getColor() + ")"; } } }

-------------------------------------------------------------------------------------------------------------

image text in transcribed

image text in transcribedimage text in transcribed

AP Computer Science Coding Assignment 9-Light/Strand Name Due Date: Sunday March 10 This assignment creates two class fles: one that represents a single colored ight bulb and the second that represents a strand of colored lights (think of lights people use to decorate First write class ca led Light that represents a single ght bulb. Make sure that your variables are PRIVATE and your methods PUBLIC. Make sure ALL METHOD NAMES MATCH the names below so they will work with the runner program. Also make BOTH files are commented at the top with the standard name, period& disclaimer AND that you comment the body of your code. Then write a class called Strand that represents a strand of lights. The Strand dass needs the Light dass to work properly. Test both files using LightStrandTester.java from Blackboard. Grading considerations, out of 50 points: Code is commented for every method in BOTH files: 10 points (S points each constructors in Light S points toString in Light: S points other methods in Light: 10 points 2 constructors in Strand: 5 points toString in Strand: S points other methods in Strand: 10 points Light ava Variables: boolean on- Represents whether the light bulb is on or off. Set to true if the light bub is on, false if off .boolean buntOut 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 LightD-Default constructor that sets the bulb to on, not burnt out, and "white Light boolean a, boolean b, String c)- This constructor sets the on variable to the parameter o. The burntOut variable is set to the parameter b. If buntOut is true, on is set to false, no matter what value is stored in o. The color variable is set to conly 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 .vold 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. vold setColor String c)-The color variable is set to c only if c is 'red, "green or "blue", ignoning case. The value stored for color should be lower case. If c holds any value ather than "red", "green or blue ignoring casel the method sets the color to "white boolean isOn)-Returns true if on, false otherwise . vold bumOut)-Sets burntOut to true and on to false. .boolean isBurntOuti) Returns true if burntout, false atherwise ArrayList strand-An ArrayList that stores a strand of ights. Strand)-Default constructor that creates the strand to an ArrayList holding one turned on white bulb, that is not burnt out Strandlint n) A constructor that sets creates AnrayList of size n, holding n white bu bs, that are all turned on and not burnt out. If nsO, 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 ine. 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 vold setColor(String c) This method sets the color of al variable is set to c only ifc is "red green" or blue". It should ignore the case of the value in c. Ifc holds any value other than "red", "green" or "blue", color wilbe set to "white the ight bulbs in the entire Strand. The color . . void setMultiD-This method sets the color of a l the light bulbs in the entire Strand to the pattern "red green", "blue, "red","green", "blue", until the end of the Strand. .void turnOnD-This method turns on all the lights in the strand. Each individual bulb can only be turned on if it's buntOut variable is false vold turnoff)-This method turns off all the lights in the strand. .void bumOut int i-This method sets the Light at location i's burntOut variable to true int getsize)-returns the length of strand Sample Run 1 Czeate light ane with default eonatrueto est burnout on light1 2. Create Light2 with other conatrueto ofE greebunt out 3. Create Light3 with dafault construetor Flip awLtch to turn Lighe3 off Flip awitch to turn Lighe3 on te not burnt on wht oL burnt out try to Elip lightl on-this ahould ail sinee lightl is buent out PASS: isonI working pzopezly PASS: isonI working pzopezly 5. Teat getcolor PASS: getcelor working properly 6. Teat setcolor (Stringl PASs: eoloz is set corzeetly blue) below ests Eor Strand elaaa 7. Teat the default construetoz Strand PASS: St and ( ] er.atea a strand of size 1 8. Teat the conatruetoz Strand( PASs: Strand (O) ereatea a atrand of aize 1 PASS: Strand (-71 ezeatea strand of size 1 PASS: Strand(S) ereatea a atrand of aize 5 Serand(S) shouldbe all o and whEte on white ot burnt out on white ot burnt out 9.Test setcolor (String) Serand(S) should be all on and green on gree not burnt out on geeen not burnt out on gree not burnt out on gree not burnt out on geeen not burnt out Strand (5) should be all en andaulti - rea, qteen, blue, *d, qeeen on zed ot burnt out on blue ot burnt out on zed ot burnt out on geeen not burnt out Serand(S) should e 1 on white ot burnt out and white again 10. Test turnOEE se and(5) aboald be -11 off and white off whte ot burnt out off whte ot burnt out 11. Test turnon Serand (S) should be all e and whEte on white ot burnt out on white ot burnt out 12. Test burnout () the Elrat bulb in Strand (S) ahould be burnt out and oEE on whEte not burnt out

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M. Ricardo, Susan D. Urban, Karen C. Davis

4th Edition

1284231585, 978-1284231588

More Books

Students also viewed these Databases questions