C
C#2y 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;
}
}
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;
}
}
9 Replies
Denis
Denis2y ago
The class members you have described as Attributes are referred to as fields. They are usually private and have the following naming scheme: private bool _isOn; If you wish to have a publicly available field, use properties: public bool IsOn { get; set; } I'm not quite sure what the switch array is supposed to be I recommend storing the current color as a number After each on/off you increase the color number. 0 Red 1 Green 2 Blue Once you increase the number and it is equal to 3, change the value back to 0 to restart. Write a method that uses a switch to convert said number to a string representation of the color:
private string GetColorFromCode(int code)
{
switch (code)
{
case 0: return "Red";
case 1: return "Green";
case 2: return "Blue";
default: throw new OutOfRangeException("Color code not supported");
}
}
private string GetColorFromCode(int code)
{
switch (code)
{
case 0: return "Red";
case 1: return "Green";
case 2: return "Blue";
default: throw new OutOfRangeException("Color code not supported");
}
}
Pobiega
Pobiega2y ago
private static string GetColorFromCode(int code)
{
return code switch
{
0 => "Red",
1 => "Green",
2 => "Blue",
_ => throw new ArgumentOutOfRangeException(nameof(code), "Color code not supported")
};
}
private static string GetColorFromCode(int code)
{
return code switch
{
0 => "Red",
1 => "Green",
2 => "Blue",
_ => throw new ArgumentOutOfRangeException(nameof(code), "Color code not supported")
};
}
is the more modern way of writing that method 🙂 If you can be more specific about where you are stuck and why, that would help.
moshimoshi
moshimoshiOP2y ago
Thank you guys!! Would using a double for loop be overcomplicating things? haha
Pobiega
Pobiega2y ago
yes
Denis
Denis2y ago
public class Lamp
{
private int _currentColor = 0;

public bool IsOn { get; } // No set, since the lamp state is change via dedicated method
public string Color { get; private set; } // private because we don't want anyone outside this claas to change the value

public bool IsLampOn() // This method isn't really useful when you use a property
=> IsOn;

public void TurnOn()
{
IsOn = true;
}

public void TurnOff()
{
// If already off...
if (!IsOn)
// Exit method
return;

IsOn = false;
_currentColor++;
if (_currentColor > 2)
_currentColor = 0;

Color = GetColorFromCode(_currentColor);
}

private string GetColorFromCode(int code)
{
...
}
}
public class Lamp
{
private int _currentColor = 0;

public bool IsOn { get; } // No set, since the lamp state is change via dedicated method
public string Color { get; private set; } // private because we don't want anyone outside this claas to change the value

public bool IsLampOn() // This method isn't really useful when you use a property
=> IsOn;

public void TurnOn()
{
IsOn = true;
}

public void TurnOff()
{
// If already off...
if (!IsOn)
// Exit method
return;

IsOn = false;
_currentColor++;
if (_currentColor > 2)
_currentColor = 0;

Color = GetColorFromCode(_currentColor);
}

private string GetColorFromCode(int code)
{
...
}
}
Try this for now. There are additional improvements applicable to the code I've provided above; however, I don't want to overwhelm you too much
moshimoshi
moshimoshiOP2y ago
Thank you so much guys, it's really helpful 🥺 🫡 🙏 appreciate it loads
Pobiega
Pobiega2y ago
Good stuff, but try to spoonfeed less in the future.
Denis
Denis2y ago
Thanks for the feedback
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server