The Problem
You are to implement a postfix calculator, using the GUI shell provided. To do this, you will need to implement:
- A stack to manage the numbers.
- try/catch to handle Exceptions.
- Event handlers for various operations.
- a top method which will return the number on top of the stack without popping.
- a toString method which behaves a lot like the print method, but returns a String that can be assigned to the Text property of a text box object.
The GUI
Obtain a copy of the program shell. Done that? Good! When it is compiled properly, it will display a Java Swing set frame that looks like this: See image.
The Calculator
So far so good. Now, the main part will read a postfix expression entered using the buttons for number input and specifying operators. The buttons do the following things:
- A number button is pressed, in which case another digit is added to the number in the display.
- The "E" button is pressed, which enters the number and subsequently pushes it onto the stack.
- an operator button is pressed, in which case two numbers should be popped off the stack, the specified operation performed, and the result pushed back on the stack.
- The "C" button clears the current entry, while the "CA" button clears the current entry and also clears the stack.
- The "+/-" button changes the sign (positive/negative) of the current number.
The operations are performed by clicking on the operator buttons. The code for processing these should appear in the body of the actionPerformed method near the bottom of the class description.
When all the input has been processed, there should be one and only one number left on the stack. This is printed as the answer in the answer area.
Building an integer
Digits are entered by pressing the number buttons. The number to be entered is "built" by entering the successive digits, starting with the most significant. The algorithm goes something like this:
Set n to 0
While there is another digit
multiply n by 10
add the new digit
look for another digit
You will need to use an object of the class called NumberBuilder to do this, adding methods to do the following:
- Add the next digit to the number.
- reset the number to zero.
- return the current value of the number as a) an integer, and b) as a string.
- Change the sign of the number (+/-).
Errors
There are four basic types of error that could occur:
- Too many numbers and not enough operators, in which case there will be more than one number on the stack at the end.
- Too few numbers and too many operators, in which case at some point you will attempt to pop a number when there is none available.
- Division by zero, in which case an arithmetic exception is thrown. This occurs when the second number (that is, the first one popped) is zero when doing division.
- An invalid number. This one should not occur, as input is through the buttons.
In the first type, it's not clear whether it's a bad expression or the expression simply hasn't been finished yet, so the best way to handle this case would be to put a message "not enough operators" in the answer area.
In the last case, an invalid number message should be displayed, with the option of re-entering the number or canceling the equation given.
In cases 2 and 3, an appropriate error message should be shown and the expression cancelled.
Output
There are several places provided to place output:
- At the top of the window is an input window similar to a standard calculator's display. Numbers are displayed while being entered, and as the result of a calculation.
- At the bottom are three other lines, for respectively:
- The current state of the stack.
- The equation as it is being built.
- Any error messages. These might be intermediate ones that are not serious, like "not enough operators", which will happen while the equation is being built. Other errors will require a CA to fix, such as too many operators or divide by zero.
Procedure
As always with a programming, take it step by step. For example:
- Get the program to build a number correctly.
- Get the program to add numbers to the stack correctly.
- Get the Clear and ClearAll buttons working.
- Get +, -, *, and / working for positive integers.
- Get output and error messages working.
- Get the change sign (+/-) working.
- Get exponents and factorials working.
Remember to remove the message boxes! They are simply there to specify button functions when you begin.