IDNLearn.com: Your trusted source for accurate and reliable answers. Ask any question and get a detailed, reliable answer from our community of experts.

Write a JAVA program that simulates a circuit for controlling a hallway light that has switches at both ends of the hallway. Each switch can be up or down, and the light can be on or off. Toggling each switch turns the light on or off. (This is called a 3-way switch).

Create a class called ThreeWaySwitch which simulates this situation. Provide two instance variables for the states of the two switches at each end of the hallway:

private int firstSwitchState = 0,
secondSwitchState= 0 ;
Provide a constructor with the following heading:

public ThreeWaySwitch(int initialFirstSwitch,
int initialSecondSwitch)
and the following methods:

public int getFirstSwitchState() // 0 = down, 1 = up
public int getSecondSwitchState()
public int getLightState() // 0 = off, 1 = on
public void toggleFirstSwitch()
public void toggleSecondSwitch()
Do not:

provide a third instance variable for the light's state (on or off)
use the ternary operator '?', or any if or switch statements in this class
instead use the '+' and '%' operators (hint: (1 + 1) % 2 = 0)
Write a class named CircuitTester that tests all four switch combinations, printing the status of the two switches and the light for each combination of switches (down/down, up/down, down/up, and up/up).


Sagot :

I used to study this but I forgot how