2

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?

Roy
  • 21
  • 4
  • "If the parameter is larger than the array's length, an error will occur" - not in the code you posted. Which line is throwing the error? The only array indexing you've got is within the body of the `if` statement that validates that `switchState` is valid for the array... – Jon Skeet Jan 05 '23 at 15:51
  • Use modulo. `5 % 4` is `1`, `6 % 4` is `2`, etc – Bentaye Jan 05 '23 at 15:55
  • The issue is with the last condition, how to deal with the array if the input was larger than the array length, I couldn't form a condition that doesn't cause an error – Roy Jan 05 '23 at 15:56
  • 1
    Your code is wrong so there is no need to "fix" a different issue when the code still remains wrong. Your parameter has a different meaning than the assignment's parameter description and your code assumes that the lamp has no start, that's why you assume an input of 5 is always "LowLight", which is wrong, it obviously depends on the previous state. – Tom Jan 05 '23 at 16:00
  • A good software developer will understand the requirements. Sometimes, requirements are open to interpretation. Sometimes, the misunderstanding can be with the developer, sometimes with the client. The software developer needs to ask questions to clarify the requirements. My interpretation is that the code should allow multiple turnings of the switch. For example, turn the switch 2x, come back, turn the switch 2x. To do that, `class ThreeWayLamp` needs an instance variable to retain the state between successive calls to 'switch (int)' – Old Dog Programmer Jan 06 '23 at 15:19
  • Also, printing an error message is not the same as throwing an exception. – Old Dog Programmer Jan 06 '23 at 15:20

3 Answers3

0

You can solve this problem by using a modulo operator:

System.out.println(States[switchState % States.length]);
Nickitiki
  • 16
  • 3
0

Instead of using the less than condition, you can try using mod to repeat the indexes of your String array.

Try below code:

public class ThreeWayLamp {

public String[] States = {"Off","LowLifght", "MediumLifght", "HighLight"}; // an array of the 4 states
    
    
    public void Switch(int switchState){

        
    if ((switchState > 0){
        System.out.println(States[switchState % States.length]); // states.length = 4 in your case
    } else {
        System.out.println("Wrong input, try again with diffrent number");
    }
    
           
    }
Fakipo
  • 182
  • 2
  • 17
0

Using modulo you can ensure the index will always be within the size of the array

4 % 4 = 0
5 % 4 = 1
6 % 4 = 2
7 % 4 = 3
8 % 4 = 0
9 % 4 = 1
...
public void Switch(int 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 {
        Switch(switchState % 4);
        // or
        // System.out.println(States[switchState % 4]);
    }
}

Also, variable and method names should start with a lower case

Bentaye
  • 9,403
  • 5
  • 32
  • 45