completing this assignment, be sure to do the following: Ensure that the program
ID: 653571 • Letter: C
Question
completing this assignment, be sure to do the following:
Ensure that the program loops over all permutations of truth values for the Boolean variables contained in each expression, calculate the truth value of the expression, and output a truth table.
You may hard-code these particular expressions into functions in your program
You don't need to write a string parser that can generate the truth table for an arbitrary Boolean expression (although it may be edifying to do so, should you seek additional challenge - but most of the work is in learning how to do string processing, which is beyond the scope of this class).
Simplify all the expressions by hand using the fundamental properties of Boolean algebra as far as you can.
Use your program to verify that your simplified versions have the same truth table as the originals.
Show your simplification work in comments in your source code.
code in java please thank you
Explanation / Answer
Program code:
//DisplayTruthTable.java
public class DisplayTruthTable
{
// main method
public static void main(String args[])
{
// declare an array of strings
String exps[] = new String[3];
// assign the string values into the array
exps[0] = "(a+(~a.(~b+b))).b";
exps[1] = "~(a+b.c)+a.(b+~c)";
exps[2] = "(a+b).(a.c+a.~c)+a.b+b";
// call the different Evaluate methods with different string values
Evaluate(exps[0]);
Evaluate1(exps[1]);
Evaluate2(exps[2]);
}
// definition of the method Evaluate that computes and prints the first
// boolean expression
public static void Evaluate(String s)
{
boolean a[] = new boolean[4];
boolean b[] = new boolean[4];
boolean result[] = new boolean[4];
char ch = '|';
for (int i = 0; i < a.length; i++)
{
if (i < 2)
a[i] = false;
else
a[i] = true;
}
for (int i = 0; i < a.length; i++)
{
if (i % 2 == 0)
b[i] = false;
else
b[i] = true;
}
System.out.println("Truth Table of " + s + " ");
System.out.println("===================================================");
System.out.printf("%s %12s %25s ", "a", "b", s);
System.out.println("===================================================");
for (int i = 0; i < a.length; i++)
{
result[i] = (!b[i] | b[i]);
result[i] = !a[i] & result[i];
result[i] = a[i] | result[i];
result[i] = result[i] & b[i];
System.out.println(a[i] + " | " + b[i] + " | " + result[i]);
}
System.out.println("===================================================");
System.out.println();
}
// definition of the method Evaluate1 that computes and prints the second
// boolean expression
public static void Evaluate1(String s)
{
boolean a[] = new boolean[8];
boolean b[] = new boolean[8];
boolean c[] = new boolean[8];
boolean result[] = new boolean[8];
char ch = '|';
for (int i = 0; i < a.length; i++)
{
if (i < 4)
a[i] = false;
else
a[i] = true;
}
for (int i = 0; i < a.length; i++)
{
if(i==0 || i==1 || i == 4 || i ==5)
{
for(int j=0;j<2;j++)
b[i] = false;
}
else
b[i] = true;
}
for (int i = 0; i < a.length; i++)
{
if (i % 2 == 0)
c[i] = false;
else
c[i] = true;
}
System.out.println("Truth Table of " + s + " ");
System.out.println("===================================================");
System.out.printf("%s %12s %12s %25s ", "a", "b", "c", s);
System.out.println("===================================================");
for (int i = 0; i < a.length; i++)
{
result[i] = (!(a[i] | b[i] & c[i]) | (a[i] & (b[i]) | (!c[i])));
System.out.printf("%b %5c %5b %5c %5b %5c %5b ", a[i], ch, b[i], ch, c[i], ch,
result[i]);
}
System.out.println("===================================================");
System.out.println();
}
// definition of the method Evaluate2 that computes and prints the third
// boolean expression
public static void Evaluate2(String s)
{
boolean a[] = new boolean[8];
boolean b[] = new boolean[8];
boolean c[] = new boolean[8];
boolean result[] = new boolean[8];
for (int i = 0; i < a.length; i++)
{
if (i < 4)
a[i] = false;
else
a[i] = true;
}
for (int i = 0; i < a.length; i++)
{
if(i==0 || i==1 || i == 4 || i ==5)
{
for(int j=0;j<2;j++)
b[i] = false;
}
else
b[i] = true;
}
for (int i = 0; i < a.length; i++)
{
if (i % 2 == 0)
c[i] = false;
else
c[i] = true;
}
char ch = '|';
System.out.println("Truth Table of " + s + " ");
System.out.println("======================================================");
System.out.printf("%s %12s %12s %28s ", "a", "b", "c", s);
System.out.println("======================================================");
for (int i = 0; i < a.length; i++)
{
result[i] = ((a[i] | b[i]) & ((a[i] & c[i]) | (a[i] | (!c[i]))) | (a[i] & b[i]) | b[i]);
System.out.printf("%b %5c %5b %5c %5b %5c %5b ", a[i], ch, b[i], ch, c[i], ch,
result[i]);
}
System.out.println("======================================================");
System.out.println();
}
}
Sample Output:
Truth Table of (a+(~a.(~b+b))).b
===================================================
a b (a+(~a.(~b+b))).b
===================================================
false | false | false
false | true | true
true | false | false
true | true | true
===================================================
Truth Table of ~(a+b.c)+a.(b+~c)
===================================================
a b c ~(a+b.c)+a.(b+~c)
===================================================
false | false | false | true
false | false | true | true
false | true | false | true
false | true | true | false
true | false | false | true
true | false | true | false
true | true | false | true
true | true | true | true
===================================================
Truth Table of (a+b).(a.c+a.~c)+a.b+b
======================================================
a b c (a+b).(a.c+a.~c)+a.b+b
======================================================
false | false | false | false
false | false | true | false
false | true | false | true
false | true | true | true
true | false | false | true
true | false | true | true
true | true | false | true
true | true | true | true
======================================================
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.