new InputOutOfBoundsException

This commit is contained in:
lukas-heiligenbrunner 2020-04-13 11:23:57 +02:00
parent 5b271a57b3
commit ed29c393e0
5 changed files with 27 additions and 14 deletions

View File

@ -1,12 +1,11 @@
public class AndGate extends LogicGate { public class AndGate extends LogicGate {
public AndGate(int inputnr) { public AndGate(int inputnr) {
super(inputnr); super(inputnr);
if(inputnr>8){ if (inputnr > 8)
throw (new TooManyInputsException()); throw (new TooManyInputsException());
} }
}
public AndGate(DigitalInput... inputvals) { public AndGate(boolean... inputvals) {
super(inputvals); super(inputvals);
} }

View File

@ -0,0 +1,5 @@
public class InputOutOfBoundsException extends RuntimeException{
public InputOutOfBoundsException() {
super("Input out of bounds!");
}
}

View File

@ -4,20 +4,27 @@ import java.util.Arrays;
public abstract class LogicGate { public abstract class LogicGate {
private final ArrayList<DigitalInput> inputs = new ArrayList<>(); private final ArrayList<DigitalInput> inputs = new ArrayList<>();
/**
* validate gate output.
* @return validated output
*/
public abstract boolean getOutput();
public LogicGate(int inputnr) { public LogicGate(int inputnr) {
for (int i = 0; i < inputnr; i++) { for (int i = 0; i < inputnr; i++) {
inputs.add(new DigitalInput()); // generate new objects for new inputs inputs.add(new DigitalInput()); // generate new objects for new inputs
} }
} }
public LogicGate(DigitalInput... inputvals) { public LogicGate(boolean... inputvals) {
inputs.addAll(Arrays.asList(inputvals)); for (boolean i:inputvals) {
inputs.add(new DigitalInput(i));
}
} }
public abstract boolean getOutput();
public DigitalInput getInput(int nr) { public DigitalInput getInput(int nr) {
// todo validate existance of nr --> indexoutofbounds exception if (nr >= getInputNumber())
throw (new InputOutOfBoundsException());
return inputs.get(nr); return inputs.get(nr);
} }

View File

@ -13,6 +13,8 @@ public class Main {
// Setze Eingang 1 auf 1. // Setze Eingang 1 auf 1.
o1.getInput(1).setValue(0); o1.getInput(1).setValue(0);
AndGate gt = new AndGate(false,true,false);
System.out.println(a2.getOutput()); System.out.println(a2.getOutput());
} }
} }

View File

@ -3,7 +3,7 @@ public class OrGate extends LogicGate{
super(inputnr); super(inputnr);
} }
public OrGate(DigitalInput... inputvals) { public OrGate(boolean... inputvals) {
super(inputvals); super(inputvals);
} }