Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Figure 6.5 on page 330, we presented a program to evaluate arithmetic expressions. In this exercise, you will write a similar program to evaluate

image text in transcribed

image text in transcribed

In Figure 6.5 on page 330, we presented a program to evaluate arithmetic expressions. In this exercise, you will write a similar 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, =, ==, 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. For a more difficult assignment, enhance your program by adding any or all of the following features: (a) The numbers need not be integers. (b) The expression need not be fully parenthesized, and if parentheses are missing, then the Java precedence rules apply. (Note that innermost expressions such as (1 Method Specification and Implementation evaluate public static double evaluate(String expression) The evaluate method evaluates the arithmetic expression. Parameter: expression, a fully parenthesized arithmetic expression Precondition: The expression must be a fully parenthesized arithmetic expression formed from double numbers (with no + or - sign in front), any of the four arithmetic operations (+,-, *, or /), and spaces. Returns: the value of the arithmetic expression Throws: IllegalArgumentException Indicates that the expression had the wrong format. public static double evaluate(String expression) { // Two generic stacks to hold the expression's numbers and operations: Stack numbers = new Stack(; Stack operations = new Stack(; // Convert the expression to a Scanner for easier processing. The next String holds the // next piece of the expression: a number, operation, or parenthesis. Scanner input = new Scanner(expression); This code requires String next; java.util. Stack, java.util.Scanner, and while (input.hasNext() java.util.Pattern. { if (input.hasNext (UNSIGNED_DOUBLE)) See Appendix B for { // The next piece of the expression is a number UNSIGNED_DOUBLE and next = input.findInLine (UNSIGNED_DOUBLE); CHARACTER, which we use numbers.push(new Double(next)); here to simplify reading from } a Scanner. else { // The next piece of the input is an operation (+, ; *, or /) or a parenthesis. next input.findInLine (CHARACTER); switch (next.charAt(0)) { case '+': // Addition case '-': // Subtraction case '*': // Multiplication case '/': // Division operations.push(next.charAt(0); break; case ')': // Right parenthesis (the evaluateStackTops function is on the next page) evaluateStackTops (numbers, operations); break; case '(': // Left parenthesis break; default : // Illegal character throw new IllegalArgumentException ("Illegal character"); } } } if (numbers.size() != 1) throw new IllegalArgumentException("Illegal input expression"); return numbers.pop(); } Method Specification and Implementation evaluate public static double evaluate(String expression) The evaluate method evaluates the arithmetic expression. Parameter: expression, a fully parenthesized arithmetic expression Precondition: The expression must be a fully parenthesized arithmetic expression formed from double numbers (with no + or - sign in front), any of the four arithmetic operations (+,-, *, or /), and spaces. Returns: the value of the arithmetic expression Throws: IllegalArgumentException Indicates that the expression had the wrong format. public static double evaluate(String expression) { // Two generic stacks to hold the expression's numbers and operations: Stack numbers = new Stack(; Stack operations = new Stack(; // Convert the expression to a Scanner for easier processing. The next String holds the // next piece of the expression: a number, operation, or parenthesis. Scanner input = new Scanner(expression); This code requires String next; java.util. Stack, java.util.Scanner, and while (input.hasNext() java.util.Pattern. { if (input.hasNext (UNSIGNED_DOUBLE)) See Appendix B for { // The next piece of the expression is a number UNSIGNED_DOUBLE and next = input.findInLine (UNSIGNED_DOUBLE); CHARACTER, which we use numbers.push(new Double(next)); here to simplify reading from } a Scanner. else { // The next piece of the input is an operation (+, ; *, or /) or a parenthesis. next input.findInLine (CHARACTER); switch (next.charAt(0)) { case '+': // Addition case '-': // Subtraction case '*': // Multiplication case '/': // Division operations.push(next.charAt(0); break; case ')': // Right parenthesis (the evaluateStackTops function is on the next page) evaluateStackTops (numbers, operations); break; case '(': // Left parenthesis break; default : // Illegal character throw new IllegalArgumentException ("Illegal character"); } } } if (numbers.size() != 1) throw new IllegalArgumentException("Illegal input expression"); return numbers.pop(); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is management growth? What are its factors

Answered: 1 week ago

Question

What is digital literacy? Why is it necessary?

Answered: 1 week ago