name: ___________________________________ name: ___________________________________ name: ___________________________________
3 8 7 + * 5 /
6 4 * 20 - 12 +
8 6 + 4 + 2 *
5 4 3 2 1 + + + +
The questions below refer to the Postfix.java program. Here's a capture of one run of the program. The user entered input is in italics.
3 5 8 + * value of 3 5 8 + * = 39 5 3 - value of 5 3 - = 2 5 3 + + error: java.lang.RuntimeException: empty stack error java.util.EmptyStackException 3 a + error: java.lang.RuntimeException: badly formed postfix expression java.lang.NumberFormatException: For input string: "a" 9 + error: java.lang.RuntimeException: empty stack error java.util.EmptyStackException 5 7 % error: java.lang.NumberFormatException: For input string: "%"
StringTokenizer
class splits a string into a
sequence of tokens separated by some character in the String
if (t.equals(" "))
continue;
statement in the method evaluate
(note
that there is a space as the last character of DELIMS
).
5 3 + +
generates an error. Explain the error and how the exceptions are caught
and
rethrown in the code.
3 a +
generates an error. Explain the error and how the exceptions are caught
and
rethrown in the code -- in particular, when does the error occur in the
processing and evaluation of the expression?
"5 3 %"
generates an error as shown above -- note that this is not
a RunTimeException. Why is a NumberFormatException generated --- when
does the error occur.
Explain how to modify DELIMS
so that the IllegalArgumentException
about unrecognized operators is generated (and why/how modifying
DELIMS
will generate the error).
These questions don't refer to the postfix evaluation code.
convert
below executes.
(describe the contents in a general manner based on what's in s
before the code executes.)