C#C
C#3y ago
moshimoshi

❔ Classes and objects

Hi guys, I need help solving this problem: Write a class to model a Lamp.
Provide the following methods in your class:
a) turnOn – turn ON the lamp
b) turnOff – turn OFF the lamp
c) showCurrentColor – outputs current color of lamp
Each time the lamp is turned ON, it shows a different color. The changing
color-sequence is Red, Green, Blue, then loops back to Red and so on.
The state of the lamp is initially OFF. When the lamp is turned ON for the
first time, its color should be Red.
Note that the lamp cannot be turned ON if it is already ON. Its state
needs to be OFF before a call to turnOn() has any effect.
Test that your lamp exhibits the correct color-changing order by turning
it ON and OFF 10 consecutive times.

I am stuck here, not sure how to proceed...

namespace ColourLamp;

public class Lamp
{
    //Attributes
    public bool IsOn;
    public string Color; 

    //Initializing 
    public Lamp(string Color)
    {
        IsOn = false;
        this.Color = Color;
    }

    //Methods 
    public string ShowCurrentColor()
    {
        
        for (int i = 0; i <= 10; i++)
        {
            for (int j = 1; j <= 2; j++)
            {
                if (SwitchArray[i][0])
                    return "Red";
                if (SwitchArray[i][1])
                    return "Green";
                if (SwitchArray[i][2])
                    return "Blue";
            }
        }
    }

    public bool IsLampOn()
    {
        return IsOn;
    }

    public void TurnOn()
    {
        if (! IsLampOn())
            IsOn = true;
    }

    public void TurnOff()
    {
        if (IsLampOn())
            IsOn = false;
    }
}
Was this page helpful?