KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Half


1 import java.util.*;
2 import org.nfunk.jep.*;
3 import org.nfunk.jep.function.*;
4
5 /**
6  * An example custom function class for JEP.
7  */

8 class Half extends PostfixMathCommand {
9
10     /**
11      * Constructor
12      */

13     public Half() {
14         numberOfParameters = 1;
15     }
16     
17     /**
18      * Runs the square root operation on the inStack. The parameter is popped
19      * off the <code>inStack</code>, and the square root of it's value is
20      * pushed back to the top of <code>inStack</code>.
21      */

22     public void run(Stack inStack) throws ParseException {
23
24         // check the stack
25
checkStack(inStack);
26
27         // get the parameter from the stack
28
Object JavaDoc param = inStack.pop();
29
30         // check whether the argument is of the right type
31
if (param instanceof Double JavaDoc) {
32             // calculate the result
33
double r = ((Double JavaDoc)param).doubleValue() / 2;
34             // push the result on the inStack
35
inStack.push(new Double JavaDoc(r));
36         } else {
37             throw new ParseException("Invalid parameter type");
38         }
39     }
40 }
41
Popular Tags