## CS 201 - Homework 8 - Postfix calculator applet

#### Due: Wednesday 11/14 at 8am

This assignment consists of a single programming problem -- to build an applet mostly from scratch. Review the examples from lecture 23, including the slides.

When you have completed the assignment, please submit your program (Calc.java) using the HW 8 submission page.

### Lab exercises:

There are no specific exercises this week. You can use lab time to get started on the homework, and to ask questions about applets. Also, we can help you enable Java applets in your browser.

This week's assignment is to implement an integer calculator applet. The calculator should take expressions input in postfix notation and display the results of the computation. Thus, to calculate 3+5, push "3 Enter 5 +". The answer should then be displayed. To calculate 3+5*7 (which is written 3 5 7 * + in postfix notation), push "3 Enter 5 Enter 7 * +". Try these on the calculator below:

 Your browser does not appear to support applets. Here are static images for Windows (Internet Explorer) and Mac (Safari):     To run this applet using the appletviewer, type ``` appletviewer http://www.cs.middlebury.edu/~schar/courses/cs201-f18/hw/hw8/ ```

Your job is to implement an applet that replicates the applet above. A minimal (working) skeleton is provided in the Eclipse project hw8.zip. It mainly contains an event handler that checks which button has been pressed by looking at the button's label. This avoids having to declare all buttons as instance variables. You will have to write the rest of the code from scratch - including the layout of the applet and the methods that react to the pressed buttons. (If you want to run your applet using the appletviewer, you'll also have to write a small .html file that references your applet. You can modify HeapTest.html from homework 7, for example.)

You should use three instance variables to keep track of current "state" of the calculator:

• an integer current to store the current number that is being entered
• a Stack stack to store the previous numbers that have been entered or computed.
• a boolean entered to keep track of whether the current number has been pushed on the stack.
(Use a Stack<Integer> from java.util. The supported methods are empty(), peek(), pop(), and push() - see the API documentation.)

When a digit button is pressed (e.g., 3), the digit should be added to the right of the current number. This can be done by multiplying the current number by 10 and adding the digit. Pressing "Enter" should push the current number onto the stack, and then reset the current number to 0. (This is visualized in the above applet by changing the color of the number to red - use the instance variable entered to keep track of this information, don't check the color of the text!) When an operation button is pressed (e.g., "+"), the operation should be applied to the top two numbers on the stack (which should be popped off), and the result should be pushed back on. Be careful when implementing subtraction and division: "3 2 -" should result in 1, not -1!

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 result as "5 Enter 7 Enter +".

This is one of the first programs that you have to build (almost) from scratch. Carefully add a little bit of new functionality at a time, then compile and test it. The art of efficient programming is to find a way of building the whole (complex) program in small, incremental steps. After each step, you should be confident that the code you added works. This means that you need to test what you just wrote, which often involves sticking in println statements. Never, never, never write the whole thing first, then start debugging it.

Note: Not everything about the calculator is explained here. To fill in the missing pieces (e.g., what "Pop" and "(-)" do), you'll have to play with the sample applet above. As far as the layout of the applet goes, it should be very similar to the sample. The shape and layout of the buttons and display should be the same. Fonts and colors, as well as little things like spacing between buttons, can be (somewhat) different.

Here are screenshots of the applet on a PC using Internet Explorer and on a Mac using Safari.

Once your program works, make sure you clean it up and document it well. Your grade will not only be based on functionality, but also on organization and documentation.

Have fun!