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:

  • isOn field (boolean type, initial value false)
  • turnOn() method: sets isOn to true and outputs Lamp is ON
  • turnOff() method: sets isOn to false and outputs Lamp 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