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

Write a program to evaluate boolean expressions. Rather than arithmetic operatio

ID: 3654468 • Letter: W

Question

Write a program to evaluate boolean expressions. Rather than arithmetic operations, the input expressions for this program will use the operations && (the "and" operation), || (the "or" operation), and ! (the "not" operation.) Rather than combining numbers, the input expression will combine simple boolean comparisons of numbers such as (1 <2) and (6>3). Assume all the numbers in these simple comparisons are integers. Allow the following comparison operations: <, >, <=, >=, == and !=. At first assume that all boolean expressions are fully parenthesized and well formed. Be sure to note that "not" is a unary operation. You can assume that the argument to "not" (which follows the !) is enclosed in parentheses. Your program should allow the user to evaluate additional expressions until the user wishes to end the program.

Explanation / Answer

import java.util.Stack; import java.util.Scanner; import java.util.regex.Pattern; public class KauffmanW7_LargeProgram05_EvalBoolean { public static void main(String[ ] args) { Scanner stdin = new Scanner(System.in); String expression; boolean answer; // Testing Expressions. expression = "(5>1)"; answer = evaluate(expression); System.out.print("Test01: " + expression + " evaluated as "); if (answer == true) { System.out.println("true, which is correct."); } else { System.out.println("false, which is WRONG."); } expression = "(5>=1)"; answer = evaluate(expression); System.out.print("Test02: " + expression + " evaluated as "); if (answer == true) { System.out.println("true, which is correct."); } else { System.out.println("false, which is WRONG."); } expression = "(5= 5)"; answer = evaluate(expression); System.out.print("Test08: " + expression + " evaluated as "); if (answer == true) { System.out.println("true, which is correct."); } else { System.out.println("false, which is WRONG."); } expression = "(10 < 5)"; answer = evaluate(expression); System.out.print("Test09: " + expression + " evaluated as "); if (answer == true) { System.out.println("true, which is WRONG."); } else { System.out.println("false, which is correct."); } expression = "(10 5) && (10 > 15))"; answer = evaluate(expression); System.out.print("Test13: " + expression + " evaluated as "); if (answer == true) { System.out.println("true, which is WRONG."); } else { System.out.println("false, which is correct."); } expression = "((10 > 5) || (10 > 15))"; answer = evaluate(expression); System.out.print("Test14: " + expression + " evaluated as "); if (answer == true) { System.out.println("true, which is correct."); } else { System.out.println("false, which is WRONG."); } expression = "(((10 > 5) && (10 > 15)) || (20 > 15))"; answer = evaluate(expression); System.out.print("Test15: " + expression + " evaluated as "); if (answer == true) { System.out.println("true, which is correct."); } else { System.out.println("false, which is WRONG."); } expression = "(((10 > 5) || (10 > 15)) && (15 >= 20))"; answer = evaluate(expression); System.out.print("Test16: " + expression + " evaluated as "); if (answer == true) { System.out.println("true, which is WRONG."); } else { System.out.println("false, which is correct."); } expression = "(!(51))"; answer = evaluate(expression); System.out.print("Test18: " + expression + " evaluated as "); if (answer == true) { System.out.println("true, which is WRONG."); } else { System.out.println("false, which is correct."); } answer = query(stdin, " Would you like to try some expressions of your own?"); if (!answer) System.exit(0); System.out.println(" Please type a Boolean expression made from"); System.out.println("comparison between unsigned numbers, and the"); System.out.println("operations && (AND) and || (OR). The "); System.out.println("expression must be fully parenthesized."); do { System.out.print("Your expression: "); expression = stdin.nextLine( ); try { answer = evaluate(expression); System.out.println("The value is " + answer); } catch (Exception e) { System.out.println("Error." + e.toString( )); } } while (query(stdin, "Another string?")); System.out.println("All numbers are interesting."); } public static boolean query(Scanner input, String prompt) { String answer; System.out.print(prompt + " [Y or N]: "); answer = input.nextLine( ).toUpperCase( ); while (!answer.startsWith("Y") && !answer.startsWith("N")) { System.out.print("Invalid response. Please type Y or N: "); answer = input.nextLine( ).toUpperCase( ); } return answer.startsWith("Y"); } public static boolean evaluate(String s) { // Precondition: The string is a fully parenthesized Boolean expression // formed from non-negative numbers, parentheses, comparisons, and the three // logical operations: !(NOT, unary), && (AND, binary), and || (OR, binary). // Postcondition: The string has been evaluated and the value returned. // Exceptions: Can throw an NumberFormatException if the expression contains // characters other than digits, operations, parentheses and whitespace. // Can throw IllegalArgumentException if the input line is an // illegal expression, such as unbalanced parentheses or an unknown symbol. s = s.replace(">=","G"); s = s.replace("
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