ca.bcit.cst.comp2526.assign2b.solution
Class RPNCalculator

java.lang.Object
  extended by ca.bcit.cst.comp2526.assign2b.solution.RPNCalculator

public class RPNCalculator
extends java.lang.Object

Perform discrete operations on operands stored in a stack. http://en.wikipedia.org/wiki/Reverse_Polish_notation


Field Summary
static int MIN_STACK_SIZE
          The smallest possible stack size that will still work for an RPN calculator.
 
Constructor Summary
RPNCalculator(int stackSize)
          Construct an RPNCalculator with the specified stack size.
 
Method Summary
static Operation getOperation(char code)
          Get the Operation specified by the code.
 int getResult()
          Get the current result from the operand stack.
 void perform(Operation operation)
          Perform the specified operation with the top two values on the operand stack.
 void push(int operand)
          Push the speicifed operand onto the operand stack.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MIN_STACK_SIZE

public static final int MIN_STACK_SIZE
The smallest possible stack size that will still work for an RPN calculator.

Constructor Detail

RPNCalculator

public RPNCalculator(int stackSize)
Construct an RPNCalculator with the specified stack size.

Parameters:
stackSize - the number of items on the stack.
Throws:
java.lang.IllegalArgumentException - if the stackSize < MIN_STACK_SIZE.
See Also:
MIN_STACK_SIZE
Method Detail

push

public void push(int operand)
          throws StackOverflowException
Push the speicifed operand onto the operand stack.

Parameters:
operand - the operand to push onto the operand stack.
Throws:
StackOverflowException - if the operand stack is full.

getResult

public int getResult()
              throws StackUnderflowException
Get the current result from the operand stack.

Returns:
the current result from the operand stack. This does not alter the number of items on the stack.
Throws:
StackUnderflowException - if the operand stack is empty.

perform

public void perform(Operation operation)
             throws StackOverflowException,
                    StackUnderflowException,
                    java.lang.IllegalArgumentException
Perform the specified operation with the top two values on the operand stack.

Parameters:
operation - the operation to perform.
Throws:
StackUnderflowException - if there are less than two operands on the stack.
StackOverflowException - if the result overflows the stack (impoossible to actually occur).
java.lang.IllegalArgumentException - if the operation is null.
See Also:
Operation.getCode()

getOperation

public static Operation getOperation(char code)
                              throws InvalidOperationTypeException
Get the Operation specified by the code.

Parameters:
code - the type of operation.
Returns:
the Operation that corresponds to the code.
Throws:
InvalidOperationTypeException - if the code does not match an Operation.
See Also:
Operation.getCode()