In this question, we will investigate the following rather complicated boolean expression:
\[ \phi = (\neg ( a \wedge b ) \wedge ( a \vee b) ) \vee (( a \wedge b) \vee \neg ( a \vee b)) \]We are using standard logical notation for boolean operators, where \( \neg \) is not, \( \wedge \) is and and \( \vee \) is or. The corresponding Java operators are !, && and ||, respectively. Consequently, the Java translation of the expression \( \phi \) above is as follows:
boolean phi = (!(a && b ) && (a || b )) || ((a && b) || !(a || b));
You will write a program that takes two boolean arguments, say B1 and B2, from the command-line and evaluates the result of substituting these arguments for a and b in phi.
Note
In Java, it is valid to use both double operators || and && or single operators | and & in boolean expressions. If double operators are used in an expression, the Java compiler can apply a performance improvement using a concept called Short Circuit Evaluation.
If using a short circuit AND-operator like &&, for example, the initial part of the boolean expression is checked first. Should it turn out to be false, the remainder of the expression is ignored since the overall result will always be false as well. This can improve performance, particularly, if functions are called as part of the expression (you will learn more about functions later).
We recommend that you should use the double operator by default but pay close attention to expressions that involve function calls.
Define a class called BooleanExpr which accepts two command-line boolean arguments, and inserts them as values in the expression phi.
Note
In order to read boolean values from the command line, we need to convert String arguments to boolean, as we have already been doing with int and double in previous exercises. This can be done using the function Boolean.parseBoolean()
Your program should print the values of the two input arguments as boolean expressions to ensure you are parsing the arguments correctly, and should also print out the result of evaluating the expression ``phi`. The output should look as follows`:
:java BooleanExpr True True a: true b: true phi: true
Once you have a working program, investigate the expression phi by manually running all combinations of boolean inputs:
:java BooleanExpr true true :java BooleanExpr false true :java BooleanExpr true false :java BooleanExpr false false
What can you say about this expression as a truth function?
An automated test has been created for this exercise: BooleanExprTest.java.