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

  1. 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
  2. Create a Lamp instance in the Main class
  3. Call turnOn(), then call turnOff()

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