-6

I have some problems with using function when I call the other class.

Main class:

public static void main(String [] args) {
    baseIteration(new Function() {
        public double calculate(double x) {
            return Math.sin(x) - Math.log10(x);
        }

        @Override
        public Object apply(Object t) {
            // TODO Auto-generated method stub
            return null;
        }
    }.calculate(double x),0,10.0);
}

And baseIteration function:

public static void baseIteration(Function f, double a, double b) {
    double prev = f.calculate(a);
    double step = (b-a)/10;
    for(double p = a+step; p<=b+10e-8; p+=step) {
        double curr = f.calculate(p);
    }
}

It is not all, but main problem: f.calculation is undefined!

Maksym
  • 1
  • 1

1 Answers1

0

Well, Function does not have the calculate method, it only has the apply method. And you normally don't need to implement Function explicitly.

Try the following:

public static void main(String [] args) {
    baseIteration(x -> Math.sin(x) - Math.log10(x), 0, 10.0);
}

public static void baseIteration(Function<Double, Double> f, double a, double b) {
    double prev = f.apply(a);
    double step = (b-a)/10;
    for(double p = a+step; p<=b+10e-8; p+=step) {
        double curr = f.apply(p);
    }
}
lexicore
  • 42,748
  • 17
  • 132
  • 221
  • It is started working! Thank's you so much! I have problems with algorithm now, but, it is started working! Thanks! – Maksym Oct 04 '17 at 18:51