Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I want help with my data structures and algorithms course. I want you to write in java and please do not use chat gpt

Hello, I want help with my data structures and algorithms course. I want you to write in java and please do not use chat gpt or any other bot because it will be noticed in the system and I will get 0.

Data Structures and Algorithms

-Application of Stacks: Expression Evaluator

image text in transcribedimage text in transcribedimage text in transcribed

*****Evaluate.java source code-----

image text in transcribed

-------------------TestEvaluate************

image text in transcribed Suppose we would like to evaluate the following arithmetic expression: " 20+23+(28+5)4. To evaluate this expression, we have to realize that has higher precedence than + , and therefore, compute 23 and add it to 20 rather than computing 20+2 and multiplying it with 3. Additionally, we need to put parentheses around (28+5) to specify that the result of this part will be multiplied by 4 . A typical evaluation sequence for the given infix expression would be the following: (1) Store 20 in accumulator A1 (2) Compute 23 and store the result 6 in accumulator A2 (3) Compute A1+A2 and store the result 26 in A1 (4) Compute 28 and store the result in A2 (5) Compute 5+A2 and store the result 21 in A2 (6) Compute 4A2 and store the result 84 in A2 (7) Compute A1+A2 and store the result 110 in A1 (8) Return the result, 110 , stored in A1 We can write this sequence of operations as follows: "20 23+285+4+ ". This notation is known as postfix or reverse polish notation. Postfix notation unambiguously specifies the sequence of operations that will be performed to evaluate an expression without the need for parenthesis. Therefore, it is much easier to evaluate an expression specified in postfix notation. We start with an initially empty stack. When an operand is encountered it is immediately placed onto the output. When a left parenthesis, ( , is encountered, it is placed onto the stack. If we see a right parenthesis, then we pop the stack, writing the symbols out until we encounter a corresponding left parenthesis, which is popped but not output. When an operator is encountered, then we pop entries from the stack until we find an operator of lower priority. That is, when a * or / is encountered, the stack is popped as long as we see * or /. Similarly, when + or - is encountered, the stack is popped as long as we see *, /,+ or -, we stop only if the stack is empty or ( is encountered. IMPORTANT NOTES - Grouping is not allowed in this homework. Please obey the ethical rules. - Never change the method names and descriptions. - You need to write the codes suitable for the methods left blank in the given java file. Figure 1 shows the steps in converting the infix expression 20+23+(28+5)4 to postfix (reverse polish) notation. Figure 2 shows the steps in evaluation of the postfix expression: 2023+285+4+ Figure 1: Steps in converting the infix expression 20+23+(28+5)4 to postfix (reverse poliah) notation. Figure 2: Steps in evaluating the postfix expression 2023+285+4+ import java.util.ArrayDeque; import java.util.Deque; import java.util.Scanner; import java.util.stack; public class Evaluate \{ // Fill the method and return the value static string infixToPostfix(String expression) \{ //Do not forget to delete return "" statement return " "; \} // Fill the method and return the value static int evaluatePostfixExpression(String expression) \{ //Do not forget to delete return statement return ; \} \} ort java.util.scanner; lic class TestEvaluate \{ //Do not change main method public static void main(String[] args) \{ Scanner input = new Scanner (System.in); System.out.print("Give the expression: "); string exp = input.nextLine (); // ex : " 20+23+(28+5)4"; String postFixExp = Evaluate infixToPostfix (exp); System. out.println (postFixExp); int result = Evaluate. evaluatePostfixExpression ( postFixExp); System. out.println(result); \}

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

Larry Ellison Database Genius Of Oracle

Authors: Craig Peters

1st Edition

0766019748, 978-0766019744

More Books

Students also viewed these Databases questions

Question

Perform an Internet search. Discuss a company that uses EPLI.

Answered: 1 week ago

Question

How do you feel about employment-at-will policies? Are they fair?

Answered: 1 week ago