CSC 132 Fall 2000

Homework 6

Due: 9 a.m., Thursday 2 November 2000.

50 points

"A calculated fix."

For this assignment you will implement a limited 4-function calculator applet. The user interface should look like (refer to printed assignment, the image didn't come out).

Each time the user presses a button, some action will occur. There are the buttons and actions:

Any of the 16 square buttons: the corresponding character followed by a space is appended to the input expression (which is a string) and to the one-line input display.

Clear button: clears the input expression and both displays.

Evaluate button: clears the results display, translates the (infix) input expression into postfix format, then outputs the expression value followed by a colon (:) followed by the corresponding postfix expression to the results display. If the expression is ill-formed or results in divide-by-zero, output an error message instead. The input display will remain as it was.

Details:

This calculator limits you to entering single digit positive values.

You need to detect ill-formed input expressions and attempts to divide by zero, and output appropriate error messages to the results display. More details below.

As each input button press is registered, add both the character and a space to the end of the input expression. This will allow you to use default delimiter for StringTokenizer later. All 16 input buttons can be associated with the same ActionListener class! The class below can be used with multiple buttons; the call "event.getActionCommand()" will return a String containing the label of the button that was pressed! With this capability, it is almost trivial to append the corresponding symbol to the input expression.

class SymbolListener implements ActionListener
{
	public void actionPerformed(ActionEvent event)
	{
		System.out.println(event.getActionCommand());
	} 
}

When it comes time to translate into postfix and evaluate, instantiate a StringTokenizer for the input expression. Then you can iterate through the expression grabbing one token at a time using its iterator (see http://java.sun.com/j2se/1.3/docs/api/java/util/StringTokenizer.html for details).

You may use pseudo-code from the text or from my chapter 6 lecture notes (http://www.cs.smsu.edu/~pete/csc132/notes/Stacks.html)to build the algorithms for infix --> postfix translation and for postfix evaluation.

Your solution should be well documented and well structured. Use any existing applet you want as a base for the applet part of your solution, or write it from scratch. Call it CalApplet.java.

Develop a class called Expression to do most of the actual processing. Its contructor will take the input infix expression (a String) as its parameter. It has several public methods:

There is no built-in class for MalformedExpressionException. You must define it yourself using this code:

public class MalformedExpressionException extends Exception
{
	MalformedExpressionException(String message)
	{
		super(message);
	{
}

 

When finished, submit CalApplet.java and Expression.java to your eccentric upload folder. Submit your CalApplet.html file too.


[ Assignments | CSC 132 | Peter Sanderson | Computer Science | SMSU ]


Last reviewed: 24 October 2000

Peter Sanderson ( PeteSanderson@smsu.edu )