|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectnrs.util.PostfixCalculator
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:
| Operator | Usage | Description |
|---|---|---|
| + | op1 op2 + | Add |
| - | op1 op2 - | Subtract |
| * | op1 op2 * | Multiply |
| / | op1 op2 / | Divide|
| % | 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 |
| 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 |
public static final java.lang.String allowedOperands
| Constructor Detail |
public PostfixCalculator()
| Method Detail |
public static boolean isOperand(java.lang.String param)
public static boolean isMathFunction(java.lang.String param)
public void add(java.lang.Object item)
throws java.lang.IllegalArgumentException
item - either Number or String (operand or Math method)
java.lang.IllegalArgumentException - item wasn't a Number, operand or Math methodpublic void add(long num)
public void add(double num)
public void addNumber(java.lang.Number num)
num - Number to add
public void addOperand(java.lang.String op)
throws java.lang.IllegalArgumentException
op - must be one of the allowed operands
java.lang.IllegalArgumentException - either incorrect op or stack doesn't have any numbers on it yetallowedOperands
public void addMathFunction(java.lang.String function)
throws java.lang.IllegalArgumentException
function - function as defined by java.lang.Math.
The methods allowed are random and methods which take one or two double arguments.
java.lang.IllegalArgumentExceptionMathpublic void reset()
public java.lang.Double getResult()
public void parseCalculation(java.lang.String calculation)
calculation - space separated string of numbers, operands and functionspublic void clear()
public java.lang.String toString()
public static void main(java.lang.String[] args)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||