Question: I need this implimented into Java code it is a second part of my warm up excercise to help me further learn. This excercise seems

I need this implimented into Java code it is a second part of my warm up excercise to help me further learn. This excercise seems difficult and a solution with comments would be greatly appreciated.

You need to output Java code to a .java file, ready to run. That code needs to consist of a sequence of pop and push operations on a stack made with your stack class to evaluate the reverse polish translation of the expression.

In the example, instead of outputting

2 3 4 + * 5 +

you will need to output the text

stk.push(2); stk.push(3); stk.push(4); right = stk.pop(); left = stk.pop(); stk.push(left+right); right = stk.pop(); left = stk.pop(); stk.push(left*right); stk.push(5); right = stk.pop(); left = stk.pop(); stk.push(left+right);

inside the text of the main method of an otherwise fixed Java program containing a stack class, the creation of a stack object in the variable stk, and a line to pop the answer off the stack at the end of main and print it out.

Note that this is a compiler. It makes each element of the expression into instructions to evaluate a part of the expression. There is no loop running through the original reverse polish expression in the code that is output, unlike in the small program I suggested in the middle of this email, which is an interpreter, because it reads each instruction in a loop, and looks up what to do an does it. The Java program you output here is just the sequence of actual actions to take, and when compiled it executes that sequence of actions.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!