ICS 211 Homework H07: Postfix (Extra Credit: Prefix) 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. For extra credit you can extend your calculator to handle prefix expressions.

Tasks

1. Create a package named edu.ics211.h07

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

2. Create the InvalidExpressionException class in package edu.ics211.h07.

Create a new class called InvalidExpressionException then copy the contents of InvalidExpressionException.java.

3. Create a singleton Calculator class that implements the ICalculator interface.

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.

public interface ICalculator {  
  /** Clears the calculator. */
  void clear();

  /**
   * Calculates the answer to the post-fix expression and returns it as a Number.
   * 
   * @param expression the post-fix expression.
   * @return The answer as a Number.
   * @throws InvalidExpressionException if the expression is invalid.
   */
  Number postFixCalculate(String expression) throws InvalidExpressionException;
  
  /**
   * Calculates the answer to the pre-fix expression and returns it as a Number.
   * This method is optional. You will get extra credit if you implement this method.
   * Otherwise just throw an UnsupportedOperationException.
   * 
   * @param expression the pre-fix expression.
   * @return The answer as a Number.
   * @throws InvalidExpressionException if the expression is invalid.
   */
  Number preFixCalculate(String expression) throws InvalidExpressionException;
}

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.

4. Extra Credit: Implement the preFixCalculate method.

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.

Testing

Use the JUnit CalculatorTest.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

It also tests the following prefix 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

If your code throws an UnsupportedOperationException the JUnit test will still pass.

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 H07.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 H07.zip file accept the honor pledge to submit the assignment.