Implement the “LightController” class with necessary properties to produce the given…
- User
Implement the “LightController” class with necessary properties to produce the given output for the provided driver code.
Driver Class public class LightControllerTester{ public static void main(String args []){ LightController c1 = new LightController(); c1.showLightStatus(); System.out.println("1---------------"); c1.adjustBrightness(4); c1.switchLight(); System.out.println("2---------------"); c1.showLightStatus(); System.out.println("3---------------"); c1.adjustBrightness(4); System.out.println("4---------------"); c1.showLightStatus(); System.out.println("5---------------"); c1.adjustBrightness(-2); c1.adjustBrightness(9); System.out.println("6---------------"); c1.showLightStatus(); System.out.println("7---------------"); System.out.println(c1.resetSettings()); c1.showLightStatus(); System.out.println("8---------------"); c1.switchLight(); System.out.println("9---------------"); c1.showLightStatus(); } }
Output Light status: OFF Brightness Level: 0 1--------------- Please turn on the light first! Lights are now ON. 2--------------- Light status: ON Brightness Level: 1 3--------------- Brightness adjusted. 4--------------- Light status: ON Brightness Level: 5 5--------------- Brightness adjusted. Brightness out of range. Set between 0 to 10. 6--------------- Light status: ON Brightness Level: 3 7--------------- Light settings have been reset. Light status: ON Brightness Level: 1 8--------------- Lights are now OFF. 9--------------- Light status: OFF Brightness Level: 0
- Assistant
- User
do this line in details another way
public void showLightStatus() { String status = isOn ? "ON": "OFF";
- Assistant
Certainly! The line you're referring to uses the ternary operator to determine the status of the light. Let's rewrite it using a more verbose
if-elsestatement to achieve the same result:javapublic void showLightStatus() { String status; if (isOn) { status = "ON"; } else { status = "OFF"; } System.out.println("Light status: " + status); System.out.println("Brightness Level: " + brightness); }Detailed Explanation
-
Variable Declaration:
String status;: Here, aStringvariable named is declared but not initialized.
-