import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

public class MethodPointerTest {
	public static void main(String[] args) {
		try {
			printTable(1,10, MethodPointerTest.class.getMethod("square", new Class[] {double.class} ));
			printTable(1,10, java.lang.Math.class.getMethod("sqrt", new Class[]{ double.class} ));
		} catch (NoSuchMethodException e) {
			System.out.println("A NoSuchMethodException occurred!\n"+e.toString()+"\n"+e.getMessage() );
		} //catch
	} //end main()

	public static double square (double x) {
		return (x*x);
	} //square()

	public static void printTable(double from, double to, Method f) {
		System.out.println(f);
		for (double x=from; x <= to; x++) {
			System.out.print( x );
			try {
				Object[] args = { new Double(x) };
				Double d = (Double) f.invoke(null,args);
				double y = d.doubleValue();
				System.out.println("  |  "+y);
			} catch (InvocationTargetException e) {
				System.out.println("An InvocationTargetException occurred!\n"+e.toString()+"\n"+e.getMessage() );
			} catch (IllegalAccessException e) {
				System.out.println("An IllegalAccessException occurred!\n"+e.toString()+"\n"+e.getMessage() );
			} //catch
		} //for
	} //printTable()
} //class MethodPointerTest