For this assignment, you will be writing a class called Light that represents a
ID: 3783671 • Letter: F
Question
For this assignment, you will be writing a class called Light that represents a single light bulb. Download the template file, Light.java, as a starting point.
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 do not need to change any other code in the file.
In previous assignments, we had a requirement that your class be named Main. In this assignment, the class is required to be named Light.
Light.java includes a main method that will help test your code. In order to fully test the Light class, you will need to add more test cases to the main method. At a minimum, run the main to make sure your implementation output matches the sample run listed below. We will test your code using our own main method that is similar to the one provided.
When you are done coding and testing, copy and paste your entire Light class (including the main method) into the Code Runner and press "Submit" in order for your assignment to count as turned in.
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:
red off burnt out
green on not burnt out
Notice there is a tab between the value for color and "off"/"on", and one space 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 (white on 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 (green off 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 (white off 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 (white on 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 (white off 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 (white on 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 (white off 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)
NOTE: You MUST use the class name "Light" for this assignment. REMEMBER: you must SUBMIT your answer. Your assignment doesn't count as complete unless it has been submitted.
Explanation / Answer
/*
* Light.java
* The java class Light that implements the constructors
* and methods of the Light class and reports the results
* to console.
* A class which represents a single light bulb.
*/
public class Light
{
// Variables that will be initialized in the Light constructors.
private boolean on;
private boolean burntOut;
private String color = "";
// Default constructor that sets the bulb to on,
//not burnt out, and "white".
public Light()
{
on=true;
burntOut=false;
color="white";
}
// 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".
public Light(boolean o, boolean b, String c)
{
on=o;
burntOut=o;
if(burntOut)
on=false;
setColor(c);
}
// 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 before the "burnt out" or "not burnt out".
public String toString()
{
//create a string description variable
String description="";
//check on is true then set description
//on otherwise set off
if(on)
description+="on";
else
description+="off";
//set color with space
description+=" "+color;
//check if burntOut is true
//set tab and burnt out
if(burntOut)
description+=" "+"burnt out";
//set tab and not burnt out
else
description+=" "+"not burnt out";
//return desciption
return description;
}
// 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.
public void flip()
{
//Check if on is true
//then set on to false
if(on)
on=false;
//Check if on is false
//then set on to true
else if(!on)
on=true;
//Check if the burntOut is true
//est on to false
if(burntOut)
on=false;
}
// The getColor method returns the color of the bulb.
public String getColor()
{
//return color
return color;
}
// 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".
public void setColor(String c)
{
//Check if the color, c is red
if(c.equalsIgnoreCase("red"))
//set color red
color="red";
else if(c.equalsIgnoreCase("green"))
//set color green
color="green";
else if(c.equalsIgnoreCase("blue"))
//set color blue
color="blue";
else
//otherwise set color white
color="white";
}
// The isOn method returns true if on, false otherwise.
public boolean isOn()
{
if(on)
return true;
else
return false;
}
// The burnOut method sets the variable burntOut to true.
public void burnOut()
{
burntOut=true;
}
//main method that tests Light class
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()
//Create an instance of Light
Light light1 = new Light();
System.out.println("1. Test Light()");
testLight(light1, true, false, "white", "on white not burnt out");
// 2. Test Light(boolean b, boolean o, String c)
//Create an instance of Light with parameters
System.out.println(" 2. 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 burnt out");
// 3. Test burnOut()
System.out.println(" 3. 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 burnt out");
// 4. Test flip()
System.out.println(" 4. 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 not burnt out");
light3.flip();
System.out.println("now light3 should be off");
testLight(light3, false, false, "white", "off white not burnt out");
light3.flip();
System.out.println("now light3 should be back on");
testLight(light3, true, false, "white", "on white not 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 burnt out");
// 5. Test isOn()
System.out.println(" 5. 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(" 6. 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(" 7. 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() + ")";
}
}//end of Light
------------------------------------------------------------------------------------------------------------------------
Sample output:
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()
*** FAIL: on is not set correctly. on should be set to false, but it is set to true.
*** PASS: burntOut is set correctly (true)
*** PASS: color is set correctly (white)
*** FAIL: toString does not work as expected (on 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)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.