Good evening guys,
just as the titel says, my intention is, to pass a boolean operation like !x&&z
to a function so that I can apply this operation later on. The plan will be to transfer any boolean operation into a truth table. All what I need is a bit of recursion for the different inputs, a string analysis to convert String to boolean in the end applying this boolean operation to the recursion.
Beside the string analysis I just want to try to apply a boolean operation to the recursion. Is there any way to do that easily?
As wanted I will include a bit of code:
private static String recursivAequi(boolean[] bools,int id,BoolOp op,BoolOp op2)
{
String values[]=new String[]{"true","false"};
for(String i:values)
{
bools[bools.length-id-1]=Boolean.valueOf(i);
if(id!=0)
{
return recursivAequi(bools,id-1,op,op2);
}
else
{
boolean one=op.TruthValue(bools);
boolean two =op2.TruthValue(bools);
if(one^two)
{
return false+"";
}
}
}
return true+"";
}
so this methode will check, if two boolean operations are a equivalent to each other by inserting true and false for every variable the boolean expression has.
My Interface BoolOP looks now like this:
public interface BoolOp {
public boolean TruthValue(boolean[] bools);
}
I want to test this via a System out. My problem now is, how to pass a boolean array into a lambda expresssion.
I did this:
System.out.println(recursivAequi(new boolean[2],1,boolarray->boolarray[0]&&boolarray[1],boolarray->boolarray[1]&&boolarray[0]);
So the first array is just a placeholder in which the true and false values of my recursive function will be put in. the id is neccessary to have a break within the recursive function and the two following lambdas are the two boolean expressions I want to test.
Hope this helped a bit to understand the problem.
To achieve this, you could create an interface for the Boolean Operation objects and whenever you want to add one, utilize Lambda Expressions.
public interface BoolOp {
public boolean TruthValue(boolean x, boolean z);
}
...
public void performBoolOp (BoolOp op) {
op.TruthValue(true, true);
}
...
performBoolOp((x, z) -> !x^z);
As you pass a reference of the object as an instance of the BoolOp interface, it must have implemented the TruthValue function, hence you may invoke it as such.
Note:
(x, z) -> !x^z
is essentially the same, just more concise, as:
new BoolOp() {
@Override
public boolean TruthValue(boolean x, boolean y) {
return !x^z;
}
};