LogicGates/eu.heili/LogicGate.java

35 lines
843 B
Java
Raw Normal View History

2020-04-13 09:14:15 +00:00
import java.util.ArrayList;
import java.util.Arrays;
public abstract class LogicGate {
private final ArrayList<DigitalInput> inputs = new ArrayList<>();
2020-04-13 09:23:57 +00:00
/**
* validate gate output.
* @return validated output
*/
public abstract boolean getOutput();
2020-04-13 09:14:15 +00:00
public LogicGate(int inputnr) {
2020-04-13 09:23:57 +00:00
for (int i = 0; i < inputnr; i++) {
2020-04-13 09:14:15 +00:00
inputs.add(new DigitalInput()); // generate new objects for new inputs
}
}
2020-04-13 09:23:57 +00:00
public LogicGate(boolean... inputvals) {
for (boolean i:inputvals) {
inputs.add(new DigitalInput(i));
}
2020-04-13 09:14:15 +00:00
}
public DigitalInput getInput(int nr) {
2020-04-13 09:23:57 +00:00
if (nr >= getInputNumber())
throw (new InputOutOfBoundsException());
2020-04-13 09:14:15 +00:00
return inputs.get(nr);
}
2020-04-13 09:23:57 +00:00
public int getInputNumber() {
2020-04-13 09:14:15 +00:00
return inputs.size();
}
}