I need help making a CircuitTester in java for the following code, needing an ac
ID: 3681584 • Letter: I
Question
I need help making a CircuitTester in java for the following code, needing an actual and expected states :public class Circuit { private int firstSwitchState = 0; private int secondSwitchState = 0; private int lampState = 0; // 0 for down and 1 for up public int getFirstSwitchState() { return firstSwitchState; } public int getSecondSwitchState() { return secondSwitchState; } // 0 for off and 1 for on public int getLampState() { return lampState; } public void toggleFirstSwitch() { firstSwitchState = -1 * firstSwitchState + 1; toggleLampState(); } public void toggleSecondSwitch() { secondSwitchState = -1 * secondSwitchState + 1; toggleLampState(); } private void toggleLampState() { lampState = -1 * lampState + 1; } }
Explanation / Answer
class Circuit
{
private int firstSwitchState = 0;
private int secondSwitchState = 0;
private int lampState = 0; // 0 for down and 1 for up
public int getFirstSwitchState()
{
return firstSwitchState;
}
public int getSecondSwitchState()
{
return secondSwitchState;
} // 0 for off and 1 for on
public int getLampState()
{
return lampState;
}
public void toggleFirstSwitch()
{
firstSwitchState = -1 * firstSwitchState + 1;
toggleLampState();
}
public void toggleSecondSwitch()
{
secondSwitchState = -1 * secondSwitchState +1; toggleLampState();
}
private void toggleLampState()
{
lampState = -1 * lampState + 1;
}
}
class CircuitTester
{
public static void main(String args[])
{
Circuit o=new Circuit();
int a=o.getFirstSwitchState();
if(a==0)
System.out.println("Switch is Off");
else
System.out.println("Switch is Off");
int b=o.getFirstSwitchState();
if(b==0)
System.out.println("Switch is Off");
else
System.out.println("Switch is Off");
int c=o.getLampState();
if(c==0)
System.out.println("Button is Down");
else
System.out.println("Button is Up");
o.toggleFirstSwitch();
o.toggleSecondSwitch();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.