CSCI 136 Assignment 6


Your next program is to implement a calculator application. The calculator should take expressions input in postfix notation and display the results of the computation. Thus to calculate 3+5, click on 3, enter, 5, +. The answer should then be displayed. To calculate 3+5*7 (which is written 3 5 7 * + in postfix notation), click on 3, enter, 5, enter, 7, *, +. Try these on the calculator below:

Aside from the main class, the program should include four other classes - one, State, representing the state of the calculator, and three listener classes: DigitButtonListener, OpButtonListener, and MiscButtonListener, which each implement the ActionListener interface.

A state object represents the memory of the calculator. You may think of it as representing the printed circuit in the calculator. Its purpose is to keep track of the current state of the computation. For instance, it might keep track of whether the user is entering a number, and what the number is so far. It also might keep track of the stack used in the computation and update the display window.

Objects of class DigitButtonListener and OpButtonListener respond to events generated by buttons which represent digits and operations, respectively. Each digit key has its own specialized DigitButtonListener which is responsible for knowing which number key it is listening to. Similarly each of the operation keys has its own specialized OpButtonListener which is responsible for knowing which operation key it is listening to. When the user clicks on a button with a digit on it, its corresponding listener is responsible for informing the state what number has been clicked, so the state can use it to build the number being typed in. When the user clicks on an operation button (+, -, *, or /), the corresponding listener is responsible for informing the state what operation is to be performed next so that the state can perform the operation.

Notice that we only create special listeners for digits and operations. This is because there are several buttons of each kind. The "unique" buttons: Enter, pop, and clear, are handled directly by the MiscButtonListener. They simply send a message to the state corresponding to the operation.

You might find it easier to write the code first under the assumption that the user must push the "enter" button after punching in each number. However, for full credit, it should also handle the case where the user punches in a number followed immediately by an operation. The result should be equivalent to sticking in an intervening "enter". That is punching in 5, enter, 7, + should give the same results as 5, enter, 7, enter, +.

Once you have the rest of the calculator working properly I would like you to add one or more new buttons. Possibilities include a squaring button, factorial, or xy. You should add the new button so that it looks nice, add an appropriate listener (probably created from OpButtonListener), and make sure that the state knows how to handle that operation.

If you are interested in extra credit, I suggest that you add a decimal point button so that your calculator can handle floating point numbers. Be sure to check that the user does not input an illegal number (e.g., with two decimal points). Possibilities for new buttons with this include square root, trig functions (e.g., sin, cos, tan, etc.), inverse trig functions, xy, e x, factorial, inverses, or logarithms. See the class Math in package java.lang for a list of available mathematical functions in Java.


One goal of this lab is to work with stacks. A second (but not secondary) goal is to gain experience designing and implementing an entire application.

What to hand in

For this lab you should begin by writing up a very thorough design. Please bring your design to lab. Don't forget that your design (and calculator) should include at least two additional buttons of your choice.

Your final program will be due on Sunday, April 9 at 11:59 pm. Please note that this is the due date for students in the Thursday lab as well as the Wednesday labs.

Place all of your code in a folder and drop it off in the CSCI 136 drop-off folder. Your project should include the four classes specified above:

It should also contain a class for the calculator itself (which will largely borrow from the work you did in the previous lab).