FuncEvaluator.java Source code

Java tutorial

Introduction

Here is the source code for FuncEvaluator.java

Source

    import java.io.FileReader;
    import java.io.IOException;

    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
    import javax.script.ScriptException;

    public class FuncEvaluator {
  public static void main(String[] args) {
    if (args.length != 2) {
      System.err
          .println("usage: java FuncEvaluator scriptfile " + "script-exp");
      return;
    }

    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("rhino");

    try {
      System.out.println(engine.eval(new FileReader(args[0])));
      System.out.println(engine.eval(args[1]));
    } catch (ScriptException se) {
      System.err.println(se.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
  }

}

        //////////////
function combinations (n, r)
{
   return fact (n)/(fact (r)*fact (n-r))
}

function fact (n)
{
   if (n == 0)
       return 1;
   else
       return n*fact (n-1);
}