HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML

"Extending Your Applications with
 Bean Scripting Framework"
Vol 5 Issue 6, p.153


Listing 1 

package stat; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.Collections; 
import java.util.HashMap; 

public class Stats { 

 public ArrayList nums; 

 public Stats(ArrayList someNums){ 
  nums = new ArrayList(someNums); 

 public Stats(double[] someNums){ 
  nums = new ArrayList(someNums.length); 

  for (int index=0; index  < someNums.length; index++){ 
    nums.add(new Double(someNums[index])); 

 public double getMean (){ 
  return this.getMean(true); 

 public double getMean (boolean sample){ 
  // Define mean that finds two types of mean, namely: 
  // population mean and sample mean 
  double sum=0.0; 
  double average=0.0; 
  Iterator iterator = nums.iterator(); 

   sum = sum + ((Double)iterator.next()).doubleValue(); 

   // Check to see if this is a sample mean 
   average = sum / nums.size()-1; 
   average = sum / nums.size(); 

  return average; 

 public ArrayList getRange (){ 
   // Find the range. Returns a tuple with the minimum, 
   // maximum, and range value 
  double min, max; 
  ArrayList ranges; 

  min = ((Double)Collections.min(nums)).doubleValue(); 
  max = ((Double)Collections.max(nums)).doubleValue(); 

  ranges = new ArrayList(); 
  ranges.add(new Double (min)); 
  ranges.add(new Double (max)); 
  ranges.add(new Double (max-min)); 

  return ranges; 

 public double getMedian (){ 
  // Find the Median number 

   // create a duplicate since we are going to modify the 
   // sequence 
  ArrayList seq = new ArrayList(nums); 

   // sort the list of numbers 

  double median = 0.0; // to hold the median value 

  int length = seq.size(); // to hold the length of the 
                         // sequence 
  int index=0; 

   // Check to see if the length is an even number 
  if ( ( length % 2) == 0){ 
    // since it is an even number 
    // add the two middle number together 
   index = length / 2; 
   double m1 = ((Double)seq.get(index-1)).doubleValue(); 
   double m2 = ((Double)seq.get(index)).doubleValue(); 
   median = (m1 + m2) /2.0; 
    // since it is an odd number 
    // just grab the middle number 
   index = (length / 2); 
   median = ((Double)seq.get(index)).doubleValue(); 
  return median; 


 private int countMode(Object object, ArrayList list){ 
  int index = 0; 
  int count = 0; 
  do { 
   index = Collections.binarySearch(list, object); 
   if(index >=0)list.remove(index); 
  while (index >=0); 
  return count; 

 public double getMode (){ 
  // Find the number that repeats the most. 

   // make a duplicate copy of the nums argument 
  ArrayList duplicate = new ArrayList(nums); 

  double highest_count = -100; 
  double mode = -100; 

  Iterator iterator = nums.iterator(); 
   // iterate through nums removing each item out of the duplicate 
   // calculate the highest_count and the mode 
   double count = 0; 
   Object item = iterator.next(); 

    // Count the number of times the item occurs in the list 
    // If Count is 0 go to the next iteration 
   count = countMode(item, duplicate); 
   if (count == 0) continue; 

    // determine the highest count. The highest counted item is the mode. 
   if (count > highest_count){ 
    highest_count = count; 
    mode = ((Double)item).doubleValue(); 

  return mode; 


Listing 2 

print "The mode of the houses is %2.2f" %  houses.mode 
print "The mean of the houses is %2.2f" % houses.mean 
print "The median of the houses is %2.2f" % houses.median 

Listing 3 

import com.ibm.cs.util.IOUtils; 
import com.ibm.bsf.BSFManager; 
import com.ibm.bsf.BSFEngine; 
import java.io.FileReader; 
import stat.Stats; 

class Test { 
 public static void main (String [] args){ 
  BSFManager manager = new BSFManager (); 

   //Register JPython into the scripting manager. 
  String[] extensions = {"py"}; 
  manager.registerScriptingEngine ("jpython", 

    //Load JPython engine 
   BSFEngine jpythonEngine = manager.loadScriptingEngine ("jpython"); 

    //Execute an expression. 
   Object result = manager.eval ("jpython", "testString", 0, 0, "2+32"); 

    //Map some objects that the script will use. 
   double[] houses=new double [] {100.0e3, 130.0e3, 140e3, 150e3}; 
   Stats stats = new Stats(houses); 
   manager.declareBean("houses", stats, Stats.class); 

    //Load the script and execute it. 
   String fileName = "TestStat2.py"; 
   String language = manager.getLangFromFilename(fileName); 
   String script = IOUtils.getStringFromReader(new FileReader(fileName)); 
   manager.exec(language, fileName, 0, 0, script); 

  catch (Exception e){ 



All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.