007-003-009
Lamp Controller Program
Medium
Problem Description
Lamp Controller Program
In this problem, you will create a Lamp class that uses turnOn() and turnOff() methods to toggle the lamp's ON/OFF state.
Learning Objective: Understand how to manage object state using class fields and methods
Overview
Create a Lamp class to manage the ON/OFF state of a lamp. Learn how to use methods that change object state.
Specifications
Define the following in the Lamp class:
isOnfield (boolean type, initial valuefalse)turnOn()method: setsisOntotrueand outputsLamp is ONturnOff()method: setsisOntofalseand outputsLamp is OFF
Usage Example
Lamp lamp = new Lamp();
lamp.turnOn(); // => Lamp is ON
lamp.turnOff(); // => Lamp is OFF
Hints
- Fields are declared inside the class and can be accessed from methods
- Access fields with
this.isOn
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up