KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > xjep > function > Sum


1 /* @author rich
2  * Created on 18-Nov-2003
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

8 package org.lsmp.djep.xjep.function;
9
10 import java.util.*;
11 import org.nfunk.jep.*;
12 import org.nfunk.jep.function.*;
13
14 /**
15  * A sum function Sum(x^2,x,1,10) finds the sum of x^2 with x running from 1 to 10.
16  * Sum(x^2,x,1,10,2) calculates the 1^2+3^2+5^2+7^2+9^2 i.e. in steps of 2.
17  * @author Rich Morris
18  * Created on 10-Sept-2004
19  */

20 public class Sum extends SumType {
21
22     static Add add = new Add();
23
24     public Sum()
25     {
26         super("Sum");
27     }
28
29         
30     public Object JavaDoc evaluate(Object JavaDoc elements[]) throws ParseException
31     {
32         Object JavaDoc ret;
33         ret = elements[0];
34         for(int i=1;i<elements.length;++i)
35         {
36             ret = add.add(ret,elements[i]);
37         }
38         return ret;
39     }
40 }
41
Popular Tags