-2

Is there a way I can pass one of the 3 functions fun1 to fun3 as argument to eval and then evaluate it? The code:

public class Pruebas {
    public static double fun1(double x){
        return x*1;
    }
    public static double fun2(double x){
        return x*2;
    }
    public static double fun3(double x){
        return x*3;
    }
    public static double eval(funx,double x0){
       /* funx at this point i expect it to be fun1, fun2 or fun3 */
       double f=funx(x0);
       return f;
    }
}
Erick G. Hagstrom
  • 4,873
  • 1
  • 24
  • 38
Richard
  • 11
  • 2
  • Check out reflection: http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful – Marv Mar 14 '16 at 16:22
  • 1
    There are several means. Which to use depends on the situation. What are you trying to achieve? – Raedwald Mar 14 '16 at 16:24
  • Depending on what you want to do, the [Strategy and Template Method design patterns](http://stackoverflow.com/questions/669271/what-is-the-difference-between-the-template-method-and-the-strategy-patterns) can be useful for this. – Raedwald Mar 14 '16 at 16:26
  • Hi thanks for replying, I simplified the problem, but what I´m doing is a thermodinamics program that uses a lot of mathematics specificly numerical methods that require me to evaluate a lot of functions a lot of times with different input arguments each time. – Richard Mar 14 '16 at 16:42
  • Are you working with Java 8? Or an earlier release? – Erick G. Hagstrom Mar 14 '16 at 16:49
  • Oh, wait. In your sample code, all 3 of your `funx()` methods take a single argument of type `double`. Is that the case in your real code? Do all of the methods that you want to do this with take one argument of type `double` (or at least of the same type)? – Erick G. Hagstrom Mar 14 '16 at 16:53
  • Yes they all take double arguments, some of them take more than one but I can change that if necesary. – Richard Mar 14 '16 at 17:19

2 Answers2

3

You can use Method References if you can afford Java 8

package javaapplication4;

import java.util.function.Function;

public class JavaApplication4 {

    public static class Pruebas 
    {
       public static double fun1(double x)
       {
           return x*1;
       }

       public static double fun2(double x){
           return x*2;
       }
       public static double fun3(double x){
           return x*3;
       }

       public static double eval(Function<Double, Double> fun,double x0)
       {
          double f=fun.apply(x0);
          return f;
       }
    }


    public static void main(String[] args) 
    {
        System.out.println(Pruebas.eval(Pruebas::fun3, 5));
    }

}
Margaret Bloom
  • 41,768
  • 5
  • 78
  • 124
0

usually, the way to do this would be to create an interface and then have 3 implementing classes. For example,

public interface FuncInterface
{
    public double func(double x);
}
public class Pruebas {
    public class Func1 implements FuncInterface {
        public static double func(double x){
           return x*1;
        }
    }
    public class Func2 implements FuncInterface {
        public static double func(double x){
           return x*2;
        }
    }
    public class Func3 implements FuncInterface {
        public static double func(double x){
           return x*3;
        }
    }
    public static double eval(FuncInterface funcI,double x0){
       /* funx at this point i expect it to be fun1, fun2 or fun3 */
       double f=funcI.func(x0);
       return f;
    }
}
William B.
  • 85
  • 5