nrs.util
Class PostfixCalculator

java.lang.Object
  extended bynrs.util.PostfixCalculator

public class PostfixCalculator
extends java.lang.Object

Postfix calculator.

Specify a calculation using postfix notation and then call calculate to get the value. In addition to numbers and operators this calculator also understands functions from the java.lang.Math class which take one or two double arguments. It also understands the random function which takes no arguments.

To see an example of how this class works try the following:

java -cp nrsutils.jar nrs.util.PostfixCalculator "36 2 * toRadians cos"

which in normal terms can be translated as cos(toRadians(36 * 2)) with an answer of 0.30901699437494745.

In programmatic use, you would create a new instance of PostfixCalculator and then use the add* methods to add numbers and operands to the stack. The alternative method is to use the parseCalculation method which will parse a space separated String. The getResult method then returns the answer as a double.

The supported mathematical operators are:

Divide
OperatorUsageDescription
+op1 op2 +Add
-op1 op2 -Subtract
*op1 op2 *Multiply
/op1 op2 /
%op1 op2 %Remainder
&op1 op2 &Bitwise AND
|op1 op2 |Bitwise OR
^op1 op2 ^Bitwise XOR
~op1 ~Bitwise complement
<<op1 op2 <<Shift bits of op1 left by distance op2; fills with zero bits on the right-hand side
>>op1 op2 >>Shift bits of op1 right by distance op2; fills with highest (sign) bit on the left-hand side
>>>op1 op2 >>> Shift bits of op1 right by distance op2; fills with zero bits on the left-hand side

Author:
Copyright © 2003-2005 Nick Sydenham <nsydenham@yahoo.co.uk>

Field Summary
static java.lang.String allowedOperands
          Allowed operands are + - / * % & | ^ << >> <<<
 
Constructor Summary
PostfixCalculator()
           
 
Method Summary
 void add(double num)
          Add a double
 void add(long num)
          Add a long
 void add(java.lang.Object item)
          Add edither a number or operator to the stack
 void addMathFunction(java.lang.String function)
          Add a math function to the stack
 void addNumber(java.lang.Number num)
          Add a number to the stack
 void addOperand(java.lang.String op)
          Add an operand
 void clear()
          Remove all previously added values, functions and operands
 java.lang.Double getResult()
          Returns the top value on the stack.
static boolean isMathFunction(java.lang.String param)
          Checks to see if the specified parameter is a java.lang.Math method
static boolean isOperand(java.lang.String param)
          Checks to see if the specified parameter is an operand
static void main(java.lang.String[] args)
          Main method used for testing
 void parseCalculation(java.lang.String calculation)
          Instead of passing number and operands one at a time they can be passed in altogether
 void reset()
          Reset the calculator
 java.lang.String toString()
          Get a description of the internal Stack
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

allowedOperands

public static final java.lang.String allowedOperands
Allowed operands are + - / * % & | ^ << >> <<<

See Also:
Constant Field Values
Constructor Detail

PostfixCalculator

public PostfixCalculator()
Method Detail

isOperand

public static boolean isOperand(java.lang.String param)
Checks to see if the specified parameter is an operand

Returns:
true if it is an operand, false otherwise

isMathFunction

public static boolean isMathFunction(java.lang.String param)
Checks to see if the specified parameter is a java.lang.Math method

Returns:
true if it is a Math method, false otherwise

add

public void add(java.lang.Object item)
         throws java.lang.IllegalArgumentException
Add edither a number or operator to the stack

Parameters:
item - either Number or String (operand or Math method)
Throws:
java.lang.IllegalArgumentException - item wasn't a Number, operand or Math method

add

public void add(long num)
Add a long


add

public void add(double num)
Add a double


addNumber

public void addNumber(java.lang.Number num)
Add a number to the stack

Parameters:
num - Number to add

addOperand

public void addOperand(java.lang.String op)
                throws java.lang.IllegalArgumentException
Add an operand

Parameters:
op - must be one of the allowed operands
Throws:
java.lang.IllegalArgumentException - either incorrect op or stack doesn't have any numbers on it yet
See Also:
allowedOperands

addMathFunction

public void addMathFunction(java.lang.String function)
                     throws java.lang.IllegalArgumentException
Add a math function to the stack

Parameters:
function - function as defined by java.lang.Math. The methods allowed are random and methods which take one or two double arguments.
Throws:
java.lang.IllegalArgumentException
See Also:
Math

reset

public void reset()
Reset the calculator


getResult

public java.lang.Double getResult()
Returns the top value on the stack. This may be null if the stack is empty or the top of the stack isn't a number.

Returns:
the top of the stack converted to a Double

parseCalculation

public void parseCalculation(java.lang.String calculation)
Instead of passing number and operands one at a time they can be passed in altogether

Parameters:
calculation - space separated string of numbers, operands and functions

clear

public void clear()
Remove all previously added values, functions and operands


toString

public java.lang.String toString()
Get a description of the internal Stack


main

public static void main(java.lang.String[] args)
Main method used for testing



Copyright © 2004-2005 Nick Sydenham <nsydenham@yahoo.co.uk>