Due: Friday November 16, 2018 @ 11:59PM
TokenScanner.java
and Token.java
can be used from the prior labTestPrefixEvaluator.java
For this assignment, you'll build a prefix expression evaluator class (PrefixEvaluator
). This class will use recursion to evaluate the expression. There are also JUnit tests which your program needs to pass.
You do NOT write the token scanner, the applet, or the JUnit tests. You write the expression evaluator class from scratch. There are several supporting files in the handout file.
Create a PrefixEvaluator
class that has a public static evaluate
method. That method takes in a String
as input and returns the String
result of evaluating the input as a prefix expression. Use the algorithm we've discussed in class.
For example, an expression of
/ * 9 + 3 7 4.5
should result in 20.0
.
You must implement a helper method (which is recursive) that accepts a single TokenScanner
parameter.
Input numbers can be real numbers (normal notation) but may NOT be negative. The result may be negative.
Allowed operations are addition(+), subtraction(-), multiplication(*), and division(/). Any other operator should cause an error message to be printed.
The evaluate method returns a string. That string is the value of the prefix expression if it is well-formed and has no errors. If there are extra values in the TokenStream
, the method returns an error message which may include the result but must note that it is not valid.
If an exception occurs, your code should catch it and return a string describing the problem. Your program should not throw any exceptions. Your messages should be very similar to the following.
Unusual or exceptional condition | Message |
---|---|
input string is empty or all spaces or tabs | No input. |
NoSuchElementException | Not enough operands. |
ArithmeticException | Infinity |
evaluation ended before end of input | Computed answer, but not all input used. |
parentheses in input | ( has no meaning here |
The PrefixEvaluator
class does not have a constructor and does not create any instances. It does not have instance variables. It has a public static evaluate method that takes in a String
and returns a String
as noted above.
You must have a private
helper method which accepts a TokenScanner
as a parameter and returns a double
. If a problem arises, you should throw an Exception
of some kind.
evaluate
should process all exceptions -- the private helper method will be recursive and do most of the heavy lifting
Submit this program as Lab 7: Recursive Prefix Evaluator
This lab is graded out of 60 points