Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java code where the program will ask the user expression and then print

ID: 3734896 • Letter: W

Question

Write a java code where the program will ask the user expression and then print truth table in format T or F Note. It should include every where use can enter anything like in between the expression And,or,not,Conditional, biconditional,xor and Xnor. Expert Q&A; Done bnter thi no mberob position. 3 Enten tho Danda) g Brconditional Crors) expremion ror s Like this write a java code for the expression for whatever user will enter and then print out the truth table for each expression and the final expression in format T or F

Explanation / Answer

Solution:

code:

import java.util.Scanner;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class TruthTable {

private static int n;//number of variables
public int isExpressionValid(String expression, boolean check) {
if (check) {//if check is true then initialze all operands to 0.
//This is just to check if the expression is valid. This should not be done everytime so check has been used
for (int i = 0; i < expression.length(); i += 2) {
expression = expression.replace("" + expression.charAt(i), "0");
}
}
ScriptEngineManager manager = new ScriptEngineManager();//We are going to use Javascript engine to evaluate this expression
//To get an instance of JavaScript engine an object of Engine Manager is needed
ScriptEngine engine = manager.getEngineByName("JavaScript");
try {
Object result = engine.eval(expression);//Evaluate the expression and get the result as an object
if (result instanceof Integer) {//check if the result is an integer
return ((Integer) result);//this is the output of expression after evaluation
}
return -1;//the expression is invalid because the output of expression is not an integer
} catch (ScriptException e) {
System.out.println("Invalid Expression Entered");
//Java script engine couldn't parse the expression which states that expression is invalid
return -1;
}
}

public void createTable(String expression) {
int res = isExpressionValid(expression, true);//True states that the expression needs to be validated
String processExpr = expression;
if (res != -1) {//If the expression is valid print the truth table
for (int i = 0; i < TruthTable.n; i++) {
System.out.print("" + (char) (65 + i) + " ");//Print the headings of truth table
}
System.out.println(expression);//Print the expression which will be evaluated
System.out.println("===============================================");
for (int i = 0; i < Math.pow(2, TruthTable.n); i++) {//The loop must go from 0 to (2^(n))-1 if n is 4 then it must go from 0 to 15
String binary = Integer.toBinaryString(i);//Get the binary value for this number
while (binary.length() < TruthTable.n)//if the number is 3 then java returns 11. If the number of operands is 4 then we need
{
binary = "0" + binary;//0011 So append 0's at the start to make 0011
}
for (int j = 0; j < binary.length(); j++) {
System.out.print("" + binary.charAt(j) + " ");//Print the numbers in input
}
for (int j = 0, k = 0; j < expression.length(); j += 2, k++) {
expression = expression.replace(expression.charAt(j), binary.charAt(k));//replace A, B (operands) with input values
}
res = isExpressionValid(expression, false);//false states that the expression needs to be evaluated
System.out.println("" + res);//Print the result of expression
expression = processExpr;//reset the expression so that it can be evaluated again
}
} else {
//Entered expression cannot be evaluated to a boolean value
System.out.println("Please enter a Valid Boolean Expression");
}
}

public static void main(String[] args) {
// TODO code application logic here
String expr = "A";//Expression to be created(Start with A as operand)
Scanner sc = new Scanner(System.in);
System.out.println("[& : AND][| : OR][^ : Ex-OR]");//Print the valid operators that can be used
System.out.print("Please Enter the Number of Boolean Variables in the expression ");
TruthTable.n = sc.nextInt();//Scan the number of operands(Boolean variables)
for (int i = 0; i < n - 1; i++) {
char op1 = (char) (65 + i);//for i=0 this will be A-> 65 is the ASCII value of A
char op2 = (char) (66 + i);//for i=0 this will be B
System.out.print("Enter Operator between " + op1 + " And " + op2 + " ");
expr += "" + sc.next().charAt(0) + op2;//Make the expression
}
System.out.println("The Entered Boolean Expression Is " + expr);//Show the built Boolean expression
new TruthTable().createTable(expr);//Create the truth table
}
}

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote