KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nfunk > jep > function > Modulus


1 /*****************************************************************************
2
3 JEP - Java Math Expression Parser 2.3.0
4       October 3 2004
5       (c) Copyright 2004, Nathan Funk and Richard Morris
6       See LICENSE.txt for license information.
7
8 *****************************************************************************/

9
10 package org.nfunk.jep.function;
11
12 import java.util.*;
13 import org.nfunk.jep.*;
14
15 public class Modulus extends PostfixMathCommand
16 {
17     public Modulus()
18     {
19         numberOfParameters = 2;
20     }
21     
22     public void run(Stack inStack)
23         throws ParseException
24     {
25         checkStack(inStack);// check the stack
26
Object JavaDoc param2 = inStack.pop();
27         Object JavaDoc param1 = inStack.pop();
28         
29         if ((param1 instanceof Number JavaDoc) && (param2 instanceof Number JavaDoc))
30         {
31             double divisor = ((Number JavaDoc)param2).doubleValue();
32             double dividend = ((Number JavaDoc)param1).doubleValue();
33         
34             double result = dividend % divisor;
35     
36             inStack.push(new Double JavaDoc(result));
37         }
38         else
39         {
40             throw new ParseException("Invalid parameter type");
41         }
42         return;
43     }
44 }
45
Popular Tags