Question

In: Computer Science

create a program in java that will evaluate a logical expression (compound proposition) based on the...

create a program in java that will evaluate a logical expression (compound proposition) based on the given truth values of individual propositional variables. The logical expression may include the logical AND or the logical OR operators. The NOT operator will be included in the variable names itself. So, a proposition such as ¬a would appear as na in the logical expression.

Here is an example of a logical expression using proper notation:

a ∧ b ∨ ¬c ∨ d

This expression will be represented as the following in this program to accommodate simple keyboard input:

a and b or nc or d

The logical operators will be evaluated left to right. Parentheses will not be included.

input the following information:

• number of variables in an expression, between 2 and 5

• the truth values of each of these variables

• a logical expression involving these variables, their negated counterparts and the logical operators and/or

Solutions

Expert Solution

Below is the code for the above problem. I have implement the solution in java language as no language was specified

I have also provided some sample test cases below the code

//Importing Scanner class for taking input from the user
import java.util.Scanner;
public class Main
{
        public static void main(String[] args) {
            
                Scanner sc = new Scanner(System.in);
                //taking Count of variable or operands from user
                System.out.println("Enter the number of variables (must be between 2 and 5 both inclusive):");
                int variable_count = sc.nextInt();
                
                //calculation Count of operators using operands
                int operator_count = variable_count - 1;
                
                
                //Taking boolean value for each operator and storing in array declared below
                System.out.println("Enter the Boolean values for the variables (True/False):");
                boolean truth_value_arr[] = new boolean[variable_count];
                for(int i=0; i<variable_count; i++) {
                    truth_value_arr[i] = sc.nextBoolean();
                }
                // this is basically used to clear the input buffer for taking a string input from user
                sc.nextLine();
                
                
                //Take the logical expression to be evaluated from the user
                System.out.println("Enter the logical expression to be evaluated:");
                /*
                 I have taken the expression and divided the tokens at space and stored it in an array
                 eg. if expression is "a and nb or c" then split operation will generate tokens as
                 "a", "and" , "nb", "or", "c". 
                 Then these tokens are stored in the array below.
                */
                String [] express = sc.nextLine().split(" "); 
                
                
                
                /*
                 Here i am preprocessing the token to evaluate all the operands who have "n" in front of them.
                 This means that all the operands which are having a not operation are evaluated here. 
                */
                for(int i=0; i<express.length;i=i+2) {
                    if(express[i].startsWith("n")) {
                        truth_value_arr[i/2] = !truth_value_arr[i/2];
                    }
                }
                
                //Taken a temp variable to store the intermediate result and also the final result at the end.
                boolean temp = true;
                int operator_index = 1;
                
                /*Here i have evaluated the first expression from left to right which means first two variables and the operand
                  and the result is stored in the temp variable
                  eg. if expression is "a and b or c" then "a and b" is evaluated here below.
                */
                if(express[operator_index].equals("and")) {
                    
                        temp = truth_value_arr[0] && truth_value_arr[1];
                 } 
                 else if(express[operator_index].equals("or")) {
                        temp = truth_value_arr[0] || truth_value_arr[1];
                 }
                 /*
                  this is incremented by 2 as logical operators are always at odd
                  position in expression array as we start index from 0 and we started operator_index at 1
                  */
                 operator_index = operator_index+2;
                 
                 
            
            /*
             In this for loop i have evaluated the rest of the logical expression.
             the loop runs for operator_count number of times as the 
             expression will be evaluated for the number operators are there in the expression 
            */
                 
                for(int i=1; i<operator_count;i++) {
                    if(express[operator_index].equals("and")) {
                        temp =  temp && truth_value_arr[i+1];
                        
                    } else if(express[operator_index].equals("or")) {
                        temp = temp || truth_value_arr[i+1];
                    }
                    
                    operator_index = operator_index+2;
                }
                
                //At last i print the result of expression based on value present in the temp variable
                
                if(temp) {
                    System.out.println("Result of given logical expression after evaluation: True");
                } else{
                    System.out.println("Result of given logical expression after evaluation: False");
                }
                
                
        }
}

Some Sample Test cases executed on above code


Related Solutions

: Create a Java program that will accept a regular expression and a filename for a...
: Create a Java program that will accept a regular expression and a filename for a text file. The program will process the file, looking at every line to find matches for the regular expression and display them. Regular Expression Format : The following operators need to be accepted: + - one or more of the following character (no groups) * - zero or more of the following character (no groups) [] – no negation, no character spans – the...
Evaluate the following logical expression. Choose True if the expression evaluates to true; choose False if...
Evaluate the following logical expression. Choose True if the expression evaluates to true; choose False if the expression evaluates to false. (3 * 5 > 10) || (20 < 15) True False Evaluate the following logical expression. Choose True if the expression evaluates to true; choose False if the expression evaluates to false. "Medium" < "High" True False Evaluate the following logical expression. Choose True if the expression evaluates to true; choose False if the expression evaluates to false. (4...
Create a Java Program to calculate compound interest. This can all be done in the main()...
Create a Java Program to calculate compound interest. This can all be done in the main() method. Create a double variable named currentBalance. Create a double variable named newBalance. Create a final double variable named INTEREST_RATE and assign 1.05 to it. Create a int variable named yearCount. Prompt for the "Number of years to invest" and store the input into the yearCount variable. Prompt for the "Amount to Invest" and store this in the currentBalance variable. Assign currentBalance to newBalance....
Java program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
1.    Given the following values, evaluate the logical value of each expression.   [3 each] int a...
1.    Given the following values, evaluate the logical value of each expression.   [3 each] int a = 3; int b = 8; int c = 3; int d = 10;               a.            b < c && c < d               b.           !(a == c && c != d) c.            !(d <= b) || (c > a) d.           a == c || c == d 2.           Given the following values, evaluate the logical value of each expression.   [4 each] int g...
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression...
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression tree and prints the expression in prefix and infix notation and evaluates the expression. (Hint use a stack)
create an expression using appropriate logical operators (AND,OR or NOT) 1. You can have only one...
create an expression using appropriate logical operators (AND,OR or NOT) 1. You can have only one meal a. pizza b. spaghetti 2. You need one of the following as proof of identity to get driver's license a. passport b. two of the following (a) birth certificate (b) ID CARD (c) Bill statement with your name and address
Write an algorithm to evaluate an infix expression in Java. Calculate its running time
Write an algorithm to evaluate an infix expression in Java. Calculate its running time
Your assignment for this program is to evaluate a numeric expression in postfix notation using a...
Your assignment for this program is to evaluate a numeric expression in postfix notation using a dynamic (pointer based) stack. As stated in the handout, in order to evaluate a numeric expression, a compiler converts an infix numeric expression to postfix notation and then it uses an algorithm and a stack to evaluate the expression. Your program should implement the pseudocode algorithm described in the attached handout. Your program will read and evaluate expressions stored in an input file (infile.txt)....
create a regular expression in Java that defines the following. A "Stir" is a right brace...
create a regular expression in Java that defines the following. A "Stir" is a right brace '}' followed by zero or more lowercase letters 'a' through 'z' and decimal digits '0' through '9', followed by a left parenthesis '('.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT