// higher order function version // but still not MSP public class HigherOrder2 { public static class TimesTwo implements ILambda { public Double apply(Double param) { return param*2; } } public static class PlusFour implements ILambda { public Double apply(Double param) { return param+4; } } public static class SquareRoot implements ILambda { public Double apply(Double param) { return Math.sqrt(param); } } public static void printTable(ILambda f, double x1, double x2, int n) { assert n>1; double x = x1; double delta = (x2-x1)/(double)(n-1); // f.apply(x) just means what f(x) means in math! System.out.println("x f(x)"); System.out.printf("%20.10f %20.10f\n", x, f.apply(x)); for(int i=0; i<(n-1); ++i) { x += delta; System.out.printf("%20.10f %20.10f\n", x, f.apply(x)); } } public static void main(String[] args) { printTable(new TimesTwo(), -5, 5, 11); printTable(new PlusFour(), -5, 5, 11); printTable(new SquareRoot(), -5, 5, 11); // anonymous inner class // We define a new ILambda from Double to Double // without giving it a name. When we do that, we // need to fill in all the methods that are still // abstract. Here, it is just the apply method. printTable(new ILambda() { public Double apply(Double param) { return param*param; } }, -5, 5, 11); } }