next up previous
Next: Review the code Up: Assignment 8: Process Objects Previous: Add a new capability

Restructure your code

Reorganize your program as a system of Process Objects.

1.
Divide your code into three classes, Calculator, Translator and Evaluator. Calculator will contain main and inputLoop. Translator will be a process object for translating infix to postfix. Evaluator will be a process object for evaluating postfix.

Each time through the loop, Calculator will create a new Translator object to perform the translation and a new Evaluator object to evaluate it.

Here is what the public interface looks like for my implementation:


\begin{verbatim}\par public class Translator {
private int index;
private Stri...
...lic Evaluator (StringBuffer line);
public int evaluate ();
}
\par\end{verbatim}

Notice that the instance variables for the Translator and Evaluator objects encapsulate the state of the translation/evaluation process. They are private because no one outside the class should be messing with them.

Also notice that Evaluator has two constructors so that it can handle arguments that are Strings or StringBuffers. That was handy for debugging.

In normal operation, the translate method and the evaluate method will only be invoked once, but you might want to give some thought to dealing with repeated invocations.


next up previous
Next: Review the code Up: Assignment 8: Process Objects Previous: Add a new capability
Allen B. Downey
1999-10-28