I wrote some c# code, which implements the transfer of several functions. I used delegates for it.
public delegate float Func(float x);
public Dictionary<string, Func> Functions = new Dictionary<string, Func>();
Fill in dictionary:
Functions.Add("y=x", delegate(float x) { return x; });
Functions.Add("y=x^2", delegate(float x) { return x*x; });
Functions.Add("y=x^3", delegate(float x) { return x*x*x; });
Functions.Add("y=sin(x)", delegate(float x) { return (float)Math.Sin((double)x); });
Functions.Add("y=cos(x)", delegate(float x) { return (float)Math.Cos((double)x); });
and I use these functions to find max of them and some other tasks.
private float maxY(params Func[] F)
{
float maxYi = 0;
float maxY = 0;
foreach (Func f in F)
{
for (float i = leftx; i < rightx; i += step)
{
maxYi = Math.Max(f(i), maxYi);
}
maxY = Math.Max(maxY, maxYi);
}
return maxY;
}
How can I do this in Java?
I don`t know how to use delegates in Java.