KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.nfunk.jep.type.*;
15 /**
16  * Converts a pair of real numbers to a complex number Complex(x,y)=x+i y.
17  *
18  * @author Rich Morris
19  * Created on 24-Mar-2004
20  */

21 public class ComplexPFMC extends PostfixMathCommand
22 {
23     public ComplexPFMC()
24     {
25         numberOfParameters = 2;
26     }
27     
28     public void run(Stack inStack)
29         throws ParseException
30     {
31         checkStack(inStack);// check the stack
32
Object JavaDoc param2 = inStack.pop();
33         Object JavaDoc param1 = inStack.pop();
34         
35         if ((param1 instanceof Number JavaDoc) && (param2 instanceof Number JavaDoc))
36         {
37             double real = ((Number JavaDoc)param1).doubleValue();
38             double imag = ((Number JavaDoc)param2).doubleValue();
39         
40             inStack.push(new Complex(real,imag));
41         }
42         else
43         {
44             throw new ParseException("Complex: Invalid parameter types "+param1.getClass().getName()+" "+param1.getClass().getName());
45         }
46         return;
47     }
48 }
49
Popular Tags