java - Workaround for passing a method into a method? -


i looking way pass method parameter method.

i trying simulate newton's-method (http://en.wikipedia.org/wiki/newton%27s_method) in java following code:

public class newton {   // iterationmethod public void newtoncalc(double x0) {     double x;      // counter     int = 0;      //newton-iteration x0     x = x0 - (y2(x0) / y2deriv(x0));      while (math.sqrt(y2(x)*y2(x)) >= math.pow(10, -10)){         //newton-iteration x(n+1)         x = x - (y2(x))/ y2deriv(x);         i++;         system.out.printf("%d. %.11f\n",i,y2(x));     }      system.out.printf("%d steps necessary resolution of 10^-10", i); }  // function (2) public static double y2(double x) {     return math.sin(x) / (1 - math.tan(x)); }  // derivative (2) public static double y2deriv(double x) {     return (math.cos(x) + math.sin(x) * math.tan(x) * math.tan(x))             / ((math.tan(x) - 1) * (math.tan(x) - 1)); }  // function (4) public static double y4(double x) {     return math.exp(-1/math.sqrt(x)); }  // derivative (4) public static double y4deriv(double x) {     return math.exp(-1/math.sqrt(x))/(2*math.pow(x, 3d/2)); }    public static void main(string[] args) {     newton newton = new newton();     newton.newtoncalc(1); } 

}

newtoncalc(x0) gets x0 @ wich iteration should started. function (y2) hardcoded method. want flexible. example newtoncalc(double x0, method y) run iteration y starting @ x0. have 2 different functions (y2 , y4 both functions excercise sheet lecture plus derivatives y2deriv , y4deriv used in iterationmethod).

i know passing method not possible dont easy workaround.

forgive me if unclear or have missed necessary information!

regards,

tak3r07

it's entirely possible since java 8, using lambda expressions


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -