ICS 211 Homework H06: Postfix Calculator

Purpose

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.

Tasks

1. Create a package named edu.ics211.h06

This is where we will put all our classes for homework 06.

2. Create a singleton PostFixCalculator class.

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.

This assignment requires you to implement a postfix calculator. 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 essence of your calculator is a class PostFixCalculator with at least the method Number calculate(String expression).

  /**
   * Calculates the answer to the postfix expression and return the result as a Number.
   * 
   * @param expression a String the postfix expression.
   * @return the result of the postfix expression as a Number.
   * @throws InvalidExpressionException if the expression isn't valid postfix.
   */
  public Number calculate(String expression) throws InvalidExpressionException {
  ...
  }

Your program 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.

Use the JUnit PostFixCalculatorTest.java to test your calculator for some good postfix expressions. It tests the following postfix expressions:

1 2 + 3 * 6 + 2 3 + /            answer 3

2.2 7.0 + 3.0 *                  answer 27.6

1 3 5 + -                        answer -7

3 4 / 5.0 +                      answer 5.0

3 4.0 / 5.0 +                    answer 5.75

You must also write test code to accept input from the user and print the result. You are welcome to create either a graphical user interface or a text-based user interface.

When submitting the assignment, turn in your test code and your PostFixCalculator class.

For this project, you are welcome to adapt any code from the book. I suggest you use the java.util.Stack<E> class for this project, but if you have extra time, you are welcome to implement your own stack.

Feel free to implement other methods in any of the classes which will help solve this problem.

Testing

Please thoroughly test your code and briefly discuss your testing strategy. Turn in all test code.

Grading Rubric

CriterionExcellent (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.

Turning in the Assignment

The assignment is due on Friday at 11:55pm. You may turn it in early.

  1. Does your code follow the Java Coding Standard and pass CheckStyle?
  2. Does your code pass the JUnit Tests?
  3. Export your project following the instructions at the bottom of the page. Name the file H06.zip
  4. Sign into Laulima, then navigate to the ICS211 site. In the left hand side of the site, there is an Assignments tab/link. Click on it and view all of the posted assignments. Select the assignment that you want to turn in and attach your H06.zip file accept the honor pledge to submit the assignment.