Goals for this assignment are to learn about:
Chapter 4
![]() | Postfix Calculator: Project 4.20 on pages 116-117 describes postfix notation. Your job is to write
a calculator program that calculates the numeric value of a postfix expression containing
numbers and the operators +, -, *, /
(in the picture, ignore the "Calculate Infix" button for the moment). |
Below are some suggestions:
To see how to parse a string, look at the sample project called TokenProject (zipped). Note, in your program, the parser should be moved out of the GUI and into a Calculator class.
Use the Java Stack (see API).
Implement the DoubleStackQueue as described in problem 4.19 on p 116. Use the Java Stack (java.util.Stack) for your stack. The DoubleStackQueue should implement the Queue interface given on p. 109. If you have trouble with generics, please ask! Your program should have three different files:
Implements the queue interface. Include a main method and test the queue by adding and removing items. Convince yourself that it works properly.
Extends RuntimeException. Thrown when you try to remove an item from an empty queue. Test it by removing items from an empty queue to throw the exception.
The interface class given on p. 109.
Don't try this unless you have completed all of the above tasks.
Add another JButton to your postfix calculator which calculates infix expressions containing numbers, parentheses, and the 4 operations: +,-,*,/. The way to do this is to convert the infix expression to postfix and then use your postfix calculator to compute the result. Converting from infix to postfix also uses a stack but is trickier than you might think. Roughly, the procedure is as follows (I took this off the web):
Algorithm to convert from infix to postfix
By no later than class on Oct 1, you should do the following:
Demo your programs no later than Thurs, Oct 4.