Page 175 - Introduction to Programming with Java: A Problem Solving Approach
P. 175
4.15 Problem Solving with Boolean Logic (Optional) 141
/************************************************************
*
TruthTable.java
Dean & Dean
This proves equivalence of two boolean expressions
************************************************************/
*
*
*
public class TruthTable
{
public static void main(String[] args)
{
boolean x = false;
boolean y = false;
boolean result1, result2;
System.out.println("x\ty\tresult1\tresult2");
for (int i=0; i<2; i++)
{
}
// end TruthTable class
}
// end main
}
// end for i
Sample output:
for (int j=0; j<2; j++)
{
!(x || y);
!x && !y;
}
x
result1 =
result2 =
System.out.println(x + "\t" + y +
"\t" +Apreasuglto1 y = !y;
// end for j
= !x;
+ P"\Dt"F+
rEensuhlta2)n; cer
x
false
y
false
result1
true
false
false
false
result2
true
false
false
false
false
true
true
false
true
true
To test the equivalence of any two boolean expressions, substitute them for these two (shaded) expressions.
Figure 4.21 Program that generates a truth table for two logical expressions
If result1 and result2 values are the same in all rows, the expressions are equivalent.
Applications
There are many ways you can use Boolean identities.
For example, consider the condition in the if statement in Figure 4.5, which looked like this:
((temp >= 50) && (temp <= 90))
⎧ ⎪ ⎨ ⎪ ⎩