007-003-009
Lamp Controller Program
Medium
Problem Description
Lamp Controller Program
In this problem, you will create a program that instantiates a Lamp class and calls turnOn() and turnOff() methods in sequence to toggle the lamp's ON/OFF state, displaying the result to standard output.
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
Lampclass:isOnfield (boolean type, initial value false)turnOn()method: sets isOn to true and outputsLamp is ONturnOff()method: sets isOn to false and outputsLamp is OFF
- Create a Lamp instance in the
Mainclass - Call
turnOn(), then callturnOff()
Output Format
Lamp is ON
Lamp is OFF
Hints
- Fields are declared in 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