basic class definitions

This commit is contained in:
lukas-heiligenbrunner 2020-04-13 11:14:15 +02:00
parent 308eb5a4bb
commit 5b271a57b3
10 changed files with 134 additions and 1 deletions

21
eu.heili/AndGate.java Normal file
View File

@ -0,0 +1,21 @@
public class AndGate extends LogicGate{
public AndGate(int inputnr) {
super(inputnr);
if(inputnr>8){
throw(new TooManyInputsException());
}
}
public AndGate(DigitalInput... inputvals) {
super(inputvals);
}
@Override
public boolean getOutput() {
boolean state = getInput(0).getValue();
for(int i = 1; i < getInputNumber(); i++){
state &= getInput(i).getValue();
}
return state;
}
}

View File

@ -0,0 +1,41 @@
public class DigitalInput {
private boolean value = false;
public DigitalInput(boolean value) {
this.value = value;
}
/**
* default value is false!
*/
public DigitalInput() {
}
public void setValue(boolean value) {
this.value = value;
}
public void setValue(LogicGate gate) {
this.value = gate.getOutput(); // todo validation stuff
}
public void setValue(int value) {
if(value > 0){
this.value = true;
}else{
this.value = false;
}
}
public void setValue(char value) {
if(value == 'H'){
this.value = true;
}else{
this.value = false;
}
}
public boolean getValue() {
return value;
}
}

2
eu.heili/EnxorGate.java Normal file
View File

@ -0,0 +1,2 @@
public class EnxorGate {
}

2
eu.heili/ExorGate.java Normal file
View File

@ -0,0 +1,2 @@
public class ExorGate {
}

27
eu.heili/LogicGate.java Normal file
View File

@ -0,0 +1,27 @@
import java.util.ArrayList;
import java.util.Arrays;
public abstract class LogicGate {
private final ArrayList<DigitalInput> inputs = new ArrayList<>();
public LogicGate(int inputnr) {
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 abstract boolean getOutput();
public DigitalInput getInput(int nr) {
// todo validate existance of nr --> indexoutofbounds exception
return inputs.get(nr);
}
public int getInputNumber(){
return inputs.size();
}
}

View File

@ -1,5 +1,18 @@
public class Main{
public class Main {
public static void main(String[] args) {
System.out.println("bla");
OrGate o1 = new OrGate(2);
AndGate a2 = new AndGate(2);
// Verbinde Eingang 0 mit dem Ausgang vom OR - Gate.
a2.getInput(0).setValue(o1);
// Setze Eingang 1 auf 1.
a2.getInput(1).setValue(0);
// Setze Eingang 0 auf 0.
o1.getInput(0).setValue(0);
// Setze Eingang 1 auf 1.
o1.getInput(1).setValue(0);
System.out.println(a2.getOutput());
}
}

2
eu.heili/NorGate.java Normal file
View File

@ -0,0 +1,2 @@
public class NorGate {
}

2
eu.heili/NotGate.java Normal file
View File

@ -0,0 +1,2 @@
public class NotGate {
}

18
eu.heili/OrGate.java Normal file
View File

@ -0,0 +1,18 @@
public class OrGate extends LogicGate{
public OrGate(int inputnr) {
super(inputnr);
}
public OrGate(DigitalInput... inputvals) {
super(inputvals);
}
@Override
public boolean getOutput() {
boolean state = getInput(0).getValue();
for(int i = 1; i < getInputNumber(); i++){
state |= getInput(i).getValue();
}
return state;
}
}

View File

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