So, this is for school, I don't know when it is due or any of that, just that I want it to work a certain way and it isn't.
Here is my code:
SwitchRunner.java:
CODE
public class SwitchRunner
{
public static void main(String[] args)
{
SwitchX mySwitch = new SwitchX(false);
SwitchX mySwitch2 = new SwitchX(false);
System.out.print(mySwitch.getState());
System.out.print("\n");
mySwitch.setState(true);
System.out.print(mySwitch.getState());
System.out.print("\n");
mySwitch.toggle();
System.out.print(mySwitch.getState());
System.out.print("\n");
mySwitch.toggle();
System.out.print(mySwitch.getState());
System.out.print("\n");
Bulb myBulb = new Bulb(mySwitch, mySwitch2);
System.out.println(myBulb.getState());
}
}
SwitchX.java:
CODE
public class SwitchX
{
private boolean isOn;
public SwitchX(boolean startOn)
{
isOn = startOn;
}
public void toggle(){
if(isOn){
isOn = false;
}
else{
isOn = true;
}
}
public boolean getState(){
return isOn;
}
public void setState(boolean state){
isOn = state;
}
public String toString(){
return "The switch is on = "+isOn;
}
}
Bulb.java:
CODE
public class Bulb
{
private boolean isOn;
public Bulb(switch1, switch2)
{
if((switch1.getState() == false && switch2.getState() == true) || (switch1.getState() == true && switch2.getState() == false)){
isOn = true;
}
else{
isOn = false;
}
}
public boolean getState(){
return isOn;
}
}
The problem I am having is sending the pointers to the SwitchX class to Bulb. It doesn't like the attempt, and when I place SwitchX in front of switch1 and switch2 it doesn't like it (this is the type of thing you do in C++). So I am nondering how you get things to work by passing in the pointers to an object? Or is this even possible in Java?
Additionally I am wondering if there is any way to add multiple classes into a single file without getting compilation errors?
This post has been edited by BetaWar: 25 Aug, 2008 - 03:21 PM