reformat code

simplified generateion of new gates
This commit is contained in:
lukas-heiligenbrunner 2020-04-13 12:56:58 +02:00
parent 992eebabae
commit 7bb6be21b6
7 changed files with 39 additions and 23 deletions

View File

@ -5,7 +5,7 @@ public class AndGate extends LogicGate {
throw (new TooManyInputsException());
}
public AndGate(boolean... inputvals) {
public AndGate(DigitalInput... inputvals) {
super(inputvals);
}

View File

@ -2,7 +2,19 @@ public class DigitalInput {
private boolean value = false;
public DigitalInput(boolean value) {
this.value = value;
setValue(value);
}
public DigitalInput(LogicGate gate) {
setValue(gate);
}
public DigitalInput(int value) {
setValue(value);
}
public DigitalInput(char value) {
setValue(value);
}
/**
@ -11,6 +23,10 @@ public class DigitalInput {
public DigitalInput() {
}
public boolean getValue() {
return value;
}
public void setValue(boolean value) {
this.value = value;
}
@ -26,8 +42,4 @@ public class DigitalInput {
public void setValue(char value) {
this.value = value == 'H';
}
public boolean getValue() {
return value;
}
}

View File

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

View File

@ -1,26 +1,26 @@
import java.util.ArrayList;
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++) {
inputs.add(new DigitalInput()); // generate new objects for new inputs
}
}
public LogicGate(boolean... inputvals) {
for (boolean i:inputvals) {
inputs.add(new DigitalInput(i));
}
public LogicGate(DigitalInput... inputvals) {
inputs.addAll(Arrays.asList(inputvals));
}
/**
* validate gate output.
*
* @return validated output
*/
public abstract boolean getOutput();
public DigitalInput getInput(int nr) {
if (nr >= getInputNumber())
throw (new InputOutOfBoundsException());
@ -31,7 +31,7 @@ public abstract class LogicGate {
return inputs.size();
}
public void addInput(DigitalInput in){
public void addInput(DigitalInput in) {
inputs.add(in);
}
}

View File

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

View File

@ -1,16 +1,16 @@
public class OrGate extends LogicGate{
public class OrGate extends LogicGate {
public OrGate(int inputnr) {
super(inputnr);
}
public OrGate(boolean... inputvals) {
public OrGate(DigitalInput... 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;

View File

@ -1,4 +1,4 @@
public class TooManyInputsException extends RuntimeException{
public class TooManyInputsException extends RuntimeException {
public TooManyInputsException() {
super("Too many inputs.");
}