new InputOutOfBoundsException
This commit is contained in:
parent
5b271a57b3
commit
ed29c393e0
@ -1,19 +1,18 @@
|
||||
public class AndGate extends LogicGate{
|
||||
public class AndGate extends LogicGate {
|
||||
public AndGate(int inputnr) {
|
||||
super(inputnr);
|
||||
if(inputnr>8){
|
||||
throw(new TooManyInputsException());
|
||||
}
|
||||
if (inputnr > 8)
|
||||
throw (new TooManyInputsException());
|
||||
}
|
||||
|
||||
public AndGate(DigitalInput... inputvals) {
|
||||
public AndGate(boolean... inputvals) {
|
||||
super(inputvals);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getOutput() {
|
||||
boolean state = getInput(0).getValue();
|
||||
for(int i = 1; i < getInputNumber(); i++){
|
||||
for (int i = 1; i < getInputNumber(); i++) {
|
||||
state &= getInput(i).getValue();
|
||||
}
|
||||
return state;
|
||||
|
5
eu.heili/InputOutOfBoundsException.java
Normal file
5
eu.heili/InputOutOfBoundsException.java
Normal file
@ -0,0 +1,5 @@
|
||||
public class InputOutOfBoundsException extends RuntimeException{
|
||||
public InputOutOfBoundsException() {
|
||||
super("Input out of bounds!");
|
||||
}
|
||||
}
|
@ -4,24 +4,31 @@ import java.util.Arrays;
|
||||
public abstract class LogicGate {
|
||||
private final ArrayList<DigitalInput> inputs = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* validate gate output.
|
||||
* @return validated output
|
||||
*/
|
||||
public abstract boolean getOutput();
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
public LogicGate(DigitalInput... inputvals) {
|
||||
inputs.addAll(Arrays.asList(inputvals));
|
||||
public LogicGate(boolean... inputvals) {
|
||||
for (boolean i:inputvals) {
|
||||
inputs.add(new DigitalInput(i));
|
||||
}
|
||||
}
|
||||
|
||||
public abstract boolean getOutput();
|
||||
|
||||
public DigitalInput getInput(int nr) {
|
||||
// todo validate existance of nr --> indexoutofbounds exception
|
||||
if (nr >= getInputNumber())
|
||||
throw (new InputOutOfBoundsException());
|
||||
return inputs.get(nr);
|
||||
}
|
||||
|
||||
public int getInputNumber(){
|
||||
public int getInputNumber() {
|
||||
return inputs.size();
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,8 @@ public class Main {
|
||||
// Setze Eingang 1 auf 1.
|
||||
o1.getInput(1).setValue(0);
|
||||
|
||||
AndGate gt = new AndGate(false,true,false);
|
||||
|
||||
System.out.println(a2.getOutput());
|
||||
}
|
||||
}
|
@ -3,7 +3,7 @@ public class OrGate extends LogicGate{
|
||||
super(inputnr);
|
||||
}
|
||||
|
||||
public OrGate(DigitalInput... inputvals) {
|
||||
public OrGate(boolean... inputvals) {
|
||||
super(inputvals);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user