From b93769510bc810b07e2708b0474496c2a6e331a6 Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Sun, 3 Jul 2022 01:35:18 -0400 Subject: initial commit --- challenge-171/adam-russell/java/ch-2.java | 82 +++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 challenge-171/adam-russell/java/ch-2.java (limited to 'challenge-171/adam-russell/java/ch-2.java') diff --git a/challenge-171/adam-russell/java/ch-2.java b/challenge-171/adam-russell/java/ch-2.java new file mode 100644 index 0000000000..5fb543747e --- /dev/null +++ b/challenge-171/adam-russell/java/ch-2.java @@ -0,0 +1,82 @@ +import java.lang.reflect.Method; + +class Compose{ + + private Method f, g; + + public void setF(Method _f){ + this.f = _f; + } + + public void setG(Method _g){ + this.g = _g; + } + + public int f(int x){ + return x + x; + } + + public int g(int x){ + return x * x; + } + + public int h(int x){ + try{ + return ((Integer) this.f.invoke(this, new Object[]{this.g.invoke(this, new Object[]{new Integer(x)})})).intValue(); + } + catch(java.lang.reflect.InvocationTargetException ite){ + exceptionMessageInvoke(ite); + } + catch(IllegalArgumentException iae){ + exceptionMessageInvoke(iae); + } + catch(IllegalAccessException iae){ + exceptionMessageInvoke(iae); + } + return -1; + } + + public Method compose(Method f, Method g){ + setF(f); + setG(g); + Method h = null; + try{ + h = Compose.class.getMethod("h", new Class[]{int.class}); + } + catch(NoSuchMethodException e){ + e.printStackTrace(); + } + return h; + } + + public static void exceptionMessageInvoke(Exception e){ + System.out.println("Error executing h"); + e.printStackTrace(); + } + + public static void main(String[] args){ + Compose composer = new Compose(); + try{ + Method f = Compose.class.getMethod("f", new Class[]{int.class}); + Method g = Compose.class.getMethod("g", new Class[]{int.class}); + Method h = composer.compose(f, g); + try{ + int x = ((Integer)h.invoke(composer, new Object[]{new Integer(7)})).intValue(); + System.out.println(x); + } + catch(java.lang.reflect.InvocationTargetException ite){ + exceptionMessageInvoke(ite); + } + catch(IllegalArgumentException iae){ + exceptionMessageInvoke(iae); + } + catch(IllegalAccessException iae){ + exceptionMessageInvoke(iae); + } + } + catch(NoSuchMethodException nsme){ + System.out.println("Method not found."); + System.exit(1); + } + } +} \ No newline at end of file -- cgit