I have this Python code that I found online and would like to know how to translate it to Java. My question is not about the algorithm but how to handle the function's arguments.
Here is the code:
def ternarySearch(f, left, right, absolutePrecision):
#left and right are the current bounds; the maximum is between them
if (right - left) < absolutePrecision:
return (left + right)/2
leftThird = (2*left + right)/3
rightThird = (left + 2*right)/3
if f(leftThird) < f(rightThird):
return ternarySearch(f, leftThird, right, absolutePrecision)
return ternarySearch(f, left, rightThird, absolutePrecision)
I would like to know what the function definition would look like. For example, a function returning y=x^2+3
would look like:
public static int y(int x){
return x*x+3;
}
but
return ternarySearch(f, leftThird, right, absolutePrecision)
is not working for me and I'd like to know what to do.
Update:
so for example i have y=3*x+2 it will be like this?
interface MyFunctor {
int myFunction(int x);
}
class MyFunctorImpl implements MyFunctor {
int myFunction(int x) {
return 3*x+2
}
}
like this?