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

Question:

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 < 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. 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 < 2) are still assumed to be in parentheses.)

(c) The expression need not be well formed, and if it is not well formed, then the user is asked to re-enter the expression.
FIGURE 6.5 A Method to Evaluate a Fully Parenthesized Arithmetic Expression 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

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question
Question Posted: