Reverse Polish Notation

An arithmetic expression is usually of the form:
x = a * b + c;

The above notation is called infix where the binary operation is placed between the operands. There is an ambiguity to the above expression. Do we multiply first or do we add. This can be resolved using precedence rules or parentheses. In 1950s Polish logician, Jan Lukasiewicz, observed that parentheses are not necessary for postfix notation, also called Reverse Polish Notation (RPN). [The older HP calculators used this form of data entry.] In the postfix notation the operation symbol follows the operands. The expression
( 2 * 3 ) + 4               in RPN              2 3 * 4 +
whereas	2 * ( 3 + 4 )       in RPN              2 3 4 + *

You are asked to write a program called RPN that will prompt the user to enter an expression in Reverse Polish Notation, perform the calculation and print the result. We will assume that the numbers are all integers and we are limited to the following arithmetic operations: + - * /. The numbers and the operators are separated by a single white space. The sample session will look like:

Enter RPN expression: 2 4 * 9 5 + -

Result: -6

You use the Scanner object for your input. Use a stack to perform the calculation. The stack interface and the stack implementation was given to you in class. Modify both the interface and the implementation to handle real numbers. Read the RPN expression from left to right. We will only push numbers on the stack. For the above expression the sequence of steps is:

2 4 * 9 5 + -		Push 2 on the stack
4 * 9 5 + -		Push 4 on the stack
* 9 5 + -		Pop 4 and 2 from the stack, multiply, and 
			push the result 8
9 5 + -			Push 9 on the stack
5 + -			Push 5 on the stack
+ -			Pop 5 and 9 from the stack, add, and 
			push the result 14
-	                Pop 14 and 8 from the stack, subtract, and 
			push the result -6

To print the final answer, pop the result from the stack and print.

The file that you will be turning in will be called RPN.java. You will follow the standard Java coding convention that I have appended below. The file will have a header of the following form:

/*
  File: RPN.java

  Description:

  Student Name:

  Student UT EID:

  Course Name: CS 305J

  Unique Number: 

  TA:

  Date Created:

  Date Last Modified:

*/

You will follow the standard Java Coding Conventions. You can either view the HTML page or download the PDF or Postscript and print it out. There is a modification that I would like to make to the standard coding conventions. Please align the opening and closing braces vertically so that you can easily make out the blocks of code.

Use the turnin program to submit your .java files. The TAs should receive your work by 5 PM on Friday, 22 April 2005.