I would like to create a small virtual PLC application, where the user may choose from a variety of logic gates and put them together to form a boolean program. I just have written the logic gates, which all work as intended. May you give me some tips in regarding programming style? My class Gate uses a static int variable named gateValuesForced. This variable shall be an indicator for the user that any of the Gates has used method Gate.force() and not has used Gate.unforce() in sucession. Did I've implemented this in the right way? Also in regard to my method Gate.finalize().
Package operatoren - Class Gate
package operatoren;
public final class Gate{
private boolean value = false, force = false, forceValue = true;
private static int gateValuesForced = 0;
public final void setValue(boolean value) {
this.value = value;
}
public final void flipValue() {
if(value) value = false;
else value = true;
}
public final void setForceValue(boolean forceValue) {
this.forceValue = forceValue;
}
public final void flipForceValue() {
if(forceValue) forceValue = false;
else forceValue = true;
}
public final boolean force() {
if(!force) {
force = true;
gateValuesForced++;
return true;
}
else {
return false;
}
}
public final boolean unforce() {
if(force) {
force = false;
gateValuesForced--;
return true;
}
else {
return false;
}
}
public final boolean getValue() {
if(force) return forceValue;
else return value;
}
public final boolean getForceValue() {
return forceValue;
}
public final boolean isForced() {
return force;
}
public final static int gateValuesForced() {
return gateValuesForced;
}
protected void finalize() {
unforce();
}
public Gate() {
}
}
Package operatoren - Class LogicGate
package operatoren;
import java.util.ArrayList;
import java.util.List;
public abstract class LogicGate {
public List<Gate> input = new ArrayList<Gate>();
public Gate output = new Gate();
public abstract boolean calc();
public LogicGate(int inputs) {
for(int i = 0; i < inputs; i++) {
input.add(new Gate());
}
}
}
Package operatoren - Class AND
package operatoren;
import java.util.NoSuchElementException;
public class AND extends LogicGate{
public boolean calc() {
if(this.input.size() > 1) {
for(int i = 1; i < this.input.size(); i++) {
if(this.input.get(i-1).getValue() && this.input.get(i).getValue()) {
this.output.setValue(true);
}
else {
this.output.setValue(false);
break;
}
}
return this.output.getValue();
}
else {
throw new NoSuchElementException("AND-Gate has less than 2 inputs.");
}
}
public AND() {
super(2);
}
}
Package test - Class Test
package test;
import operatoren.*;
public class Test {
public static void testLogicGate(LogicGate logicGate) {
System.out.println(" ------- " + logicGate.toString() + " ------- ");
boolean intToBoolean = false;
for(int i = 0; i < (1 << logicGate.input.size()); i++) {
for(int j = 0; j < logicGate.input.size(); j++) {
if((i & (1 << j)) > 0) {
intToBoolean = true;
}
else {
intToBoolean = false;
}
logicGate.input.get(j).setValue(intToBoolean);
System.out.println("logicGate.input.get(" + j + ").getValue() = " + logicGate.input.get(j).getValue());
}
logicGate.calc();
System.out.println("logicGate.output.getValue() = " + logicGate.output.getValue() + "\n");
}
if(Gate.gateValuesForced() != 0) {
System.out.println("Gate.gateValuesForced() = " + Gate.gateValuesForced());
}
}
public static void main(String[] args) {
AND testAND = new AND();
/* uncomment one or more of following lines for testing */
//testAND.input.add(new Gate());
//testAND.input.get(0).setForceValue(false);
//testAND.input.get(0).force();
testLogicGate(testAND);
}
}
You may uncomment one or more of those comments to view another test situation.