package methods;
import java.lang.reflect.*;
public class MethodTableTest {
public static void main(String[] args) throws Exception
{
Method square = MethodTableTest.class.getMethod("square", double.class);
Method sqrt = Math.class.getMethod("sqrt", double.class);
xsave(1, 10, 10, square);
xsave(1, 10, 10, sqrt);
}
public static double square(double x)
{
return x * x;
}
public static void xsave(double from, double to, int n, Method f)
{
// to print out the method as table header
System.out.println(f);
double dx = (to - from) / (n - 1);
for (double x = from; x <= to; x += dx)
{
try
{
double y = (Double) f.invoke(null, x);
System.out.printf("%10.4f | %10.4f%n", x, y);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
Questions:
-
I do not understand why they set
double dx = (to - from) / (n - 1);
. What does this mean? -
They have the
for (double x = from; x <= to; x += dx)
. I do not understand thisfor
loop so much. -
The
try
/catch
, they made thedouble y = (Double) f.invoke(null, x);
. What does this mean? When I use google to find out the meaning of this, they said to me that it "invokes the underlying method represented by this Method object". So in this case, what does this mean?
Aucun commentaire:
Enregistrer un commentaire