homeworkstudyhelp

Our Services

Get 15% Discount on your First Order

/* * Part 1: Light * A class which represents a single light bulb….

/*
* 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 white\tnot 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 green\tburnt 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 white\tburnt 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 white\tnot burnt out”);
light3.flip();
System.out.println(“now light3 should be off”);
testLight(light3, false, false, “white”, “off white\tnot burnt out”);
light3.flip();
System.out.println(“now light3 should be back on”);
testLight(light3, true, false, “white”, “on white\tnot 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 white\tburnt 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() + “)”;
}
}

 

 

 

 

 

*
* 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 white\tburnt out\n”))
{
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”);
}
}
}

 

 

 

 

 

NOTE:- write a missing code  and also with Main method.

Share This Post

Email
WhatsApp
Facebook
Twitter
LinkedIn
Pinterest
Reddit

Order a Similar Paper and get 15% Discount on your First Order

Related Questions

Retail Price Calculator using C#   Create an application that lets…

Retail Price Calculator using C#   Create an application that lets the user enter an item’s wholesale cost and its markup percentage. It should then display the items retail price. For example: If an item’s wholesale cost is $5.00 and its markup percentage is 100 percent, then the item’s retail

CSC521 Programming Project 3 A Stack-based Calculator With Postfix…

CSC521 Programming Project 3 A Stack-based Calculator With Postfix Expression Using Java Purpose • To make a simple integer calculator for 8-bit signed numbers (i.e. accept 12*4*(5-3) = and produce an answer). • To see how the stack can be used to support arithmetic operations. • Note: Groups of at

  Javascript Program writing Task A   1.1 As a JavaScript…

  Javascript Program writing Task A   1.1 As a JavaScript  programming student ,I want to establish  code with   a variable `isHappy` and assign it a boolean value based on the value of the given String variable `action`, whether it is `Smile` or not.    1.2 As a JavaScript student,

import java.util.Arrays; import java.util.Random; public class…

import java.util.Arrays; import java.util.Random; public class Board { private boolean[][] board /** * Construct a puzzle board by beginning with a solved board and then * making a number of random moves. That some of the possible moves * don’t actually change what the board looks like. * * @param

Implement the  programs in ARM assembly. implement them first in…

Implement the  programs in ARM assembly. implement them first in JAVA and translate the logic into assembly Implement a program to calculate multiplication using successive addition with recursion. For example, 5×4 is 5+5+5+5.  This can be defined recursively as:             Mult(m, n) = if n is 1,  return m                               

file named A10.java. Place all your code in this file. Create…

file named A10.java. Place all your code in this file. Create public static ArrayList of integers named intList. Define public static method addNumber (int i) that adds i to your list. Create public static HashMap of integer, integers named int Map. Define public static method add Pair (int i, int

Traversing 2D Arrays Traditional For Loop assignment 1 When…

Traversing 2D Arrays Traditional For Loop assignment 1 When traversing a 2D array a nested loop is required. The first or outside For loop will traverse the rows and the second or inside For loop will traverse the columns.     We say “for every row, for every column of

3.1.2 – Using nslookup for Passive Reconnaissance What is NSLookup?…

3.1.2 – Using nslookup for Passive Reconnaissance What is NSLookup? Try to do NSLookup on a different website and record your answer. How is Nslookup beneficial? Which part of the ethical hacking methodology does this relate to? 3.1.3 – Using ipconfig for passive reconnaissance What is ipconfig used for? After

Help me with Java   MovieStack Your are tired of asking your…

Help me with Java   MovieStack Your are tired of asking your “friend” to watch the movies you like. So you want to give your friend a stack of movies to watch, representing a watchlist. Let’s automate this process by creating a new data structure class called MovieStack, which should

Question 1: The difference between head and tail recursion? Does…

Question 1: The difference between head and tail recursion? Does JAVA support it? What are the advantages and disadvantages of each?Question 2: Describe, in detail, what are the “Traversal of Binary Trees” (hint: In-order Traversal, Pre-order Traversal, Post-order Traversal):

I need help writing a Java class StringSet .  A StringSet object…

I need help writing a Java class StringSet.  A StringSet object is given a series of up to 15 String objects.  It stores these Strings (or a reference to them, to be precise) and can perform limited calculations on the entire series.   The StringSet class has the following specification:

What is the purpose of comments and whitespaces in Java? What are…

What is the purpose of comments and whitespaces in Java? What are the Java statements and logic necessary to produce a Java program that correctly prompts users to enter a username and password? Consider that the username is not case-sensitive, but the password is. The user needs to correctly type