Java Reference
In-Depth Information
// Get the reference of the global script object calc
Object scriptCalc = engine.get("calculator");
// Get the implementation of the Calculator interface
Calculator calc = inv.getInterface(scriptCalc,
Calculator.class);
if (calc == null) {
System.err.println("Calculator interface " +
"implementation not found.");
return;
}
double x = 15.0;
double y = 10.0;
double addResult = calc.add(x, y);
double subResult = calc.subtract(x, y);
double mulResult = calc.multiply(x, y);
double divResult = calc.divide(x, y);
System.out.printf(
"calc.add(%.2f, %.2f) = %.2f%n",
x, y, addResult);
System.out.printf(
"calc.subtract(%.2f, %.2f) = %.2f%n",
x, y, subResult);
System.out.printf(
"calc.multiply(%.2f, %.2f) = %.2f%n",
x, y, mulResult);
System.out.printf(
"calc.divide(%.2f, %.2f) = %.2f%n",
x, y, divResult);
}
catch (ScriptException e) {
e.printStackTrace();
}
}
}
calc.add(15.00, 10.00) = 25.00
calcr.subtract(15.00, 10.00) = 5.00
calcr.multiply(15.00, 10.00) = 150.00
calc.divide(15.00, 10.00) = 1.50
Search WWH ::




Custom Search