Our second abstract data structure is the Stack. For this homework assignment we are going to implement a postfix calculator using a stack to keep track of the operands. This assignment should give you more practice with Eclipse and using a stack to store information. For extra credit you can extend your calculator to handle prefix expressions.
This is where we will put all our classes for homework 07.
Create a new class called InvalidExpressionException then copy the contents of InvalidExpressionException.java.
Remember a singleton class is a class that you can create only one instance of. Take a look at the ManoaBrewing class for an example of a singleton class.
The ICalculator interface has three methods, clear, postFixCalculate, and preFixCalculate.
You only need to implement the clear and postFixCalculate methods. Your calculator must be able to compute any expression of floating point or integer numbers using the operands +, -, *, or / (you do not need to handle negative input numbers such as “-5”). Your calculator should do its math the same way Java does its math. If the two operands are ints then it should do int math, else it should do floating point math.
The postFixCalculate method should be able to accept arbitrary, postfix expressions that use these four operators. If the expression is not valid your calculator should throw an InvalidExpressionException. Since Number is the super class of both Integer and Double, your calculate method should return an Integer when doing int math and a Double when doing floating point math.
If you don’t want the extra credit, just have the preFixCalculate method throw an UnsupportedOperationException.
Wikipedia Polish Notation has a good entry on prefix or Polish notation. It provides two algorithms for evaluating prefix expressions. I like the second algorithm since it is simpler.
Use the JUnit CalculatorTest.java to test your calculator for some good postfix expressions. It tests the following postfix expressions:
It also tests the following prefix expressions.
If your code throws an UnsupportedOperationException the JUnit test will still pass.
Criterion | Excellent (100%) | Satisfactory (75%) | Borderline (50%) | Unsatisfactory (25%) | Poor (0) |
---|---|---|---|---|---|
Adherence to standards - 2 points Does it conform to standards in every detail? |
No errors. | Minor details of the assignment are violated, or poor choices are made where the assignment is unclear. | Significant details of the assignment or the underlying program intent are violated, but the program still fulfills essential functions. | Significant details of the assignment or the underlying program intent are violated, but the program still fulfills some essential functions. | Misses the point of the assignment. |
Breakdown (modular design) - 1 point Does it demonstrate good modular design? |
No errors. | 1-3 minor errors. | > 3 minor errors OR 1 major error. | 2 major errors | > 2 major error. |
Correctness of code - 4 points Does it work? Does it pass JUnit? |
Passes all tests. | Works for typical input, may fail for minor special cases. | Fails for typical input, for a minor reason. | Fails for typical input, for a major reason. | No. |
Documentation, and style - 2 points Is it clear and maintainable? Does it pass CheckStyle? |
No errors. | 1-3 minor errors. | > 3 minor errors OR 1 major error. | 2 major errors | > 2 major error. |
Efficiency of code - 1 point Does it use the Java features well? |
No errors. | 1-3 minor errors. | > 3 minor errors OR 1 major error. | 2 major errors | > 2 major error. |
The assignment is due on Friday at 11:55pm. You may turn it in early.