LogicGates/eu.heili/LogicGate.java

38 lines
912 B
Java
Raw Normal View History

2020-04-13 09:14:15 +00:00
import java.util.ArrayList;
import java.util.Arrays;
2020-04-13 09:14:15 +00:00
public abstract class LogicGate {
private final ArrayList<DigitalInput> inputs = new ArrayList<>();
2020-04-13 12:36:06 +00:00
public LogicGate(final 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 12:36:06 +00:00
public LogicGate(final DigitalInput... inputvals) {
inputs.addAll(Arrays.asList(inputvals));
2020-04-13 09:14:15 +00:00
}
/**
* validate gate output.
*
* @return validated output
*/
public abstract boolean getOutput();
2020-04-13 12:36:06 +00:00
public DigitalInput getInput(final 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();
}
2020-04-13 10:34:38 +00:00
2020-04-13 12:36:06 +00:00
public void addInput(final DigitalInput in) {
2020-04-13 10:34:38 +00:00
inputs.add(in);
}
2020-04-13 09:14:15 +00:00
}