I need to create a method for my OOP lab, the details are as the following:
A ThreeWayLamp class models the behavior of a lamp that uses a three-way bulb. These bulbs have four possible states: off, low light, medium light, and high light. Each time the switch is activated, the bulb goes to the next state (from high, the next state is off, from off to low etc). The ThreeWayLamp class has a single method called switch() which takes a single int parameter indicating how many times the switch is activated. (you need to throw an exception if its negative). The Switch() method should simply print out to System.out a message indicating the state of the bulb after it has changed.
public class ThreeWayLamp {
public String[] States = {"Off","LowLifght", "MediumLifght", "HighLight"}; // an array of the 4 states
public void Switch(int switchState){
//used an if condition to determine what to print based on the parameter switchState
if ((switchState <= States.length) && (switchState >= 0)){
System.out.println(States[switchState]);
}else if (switchState < 0 ){
System.out.println("Wrong input, try again with diffrent number");
}else if (switchState >= States.length){
} //This condition is the issue, how to form a condition that will solve this problem
}
If the parameter is larger than the array's length, an error will occur, so the issue is how to form a condition that will make the array loop again around itself when it reaches its last index. For example, if the input was 5, then the method should print LowLight. Is there a possible condition or function that could solve this issue, or should I change the entire structure of the code?