diff options
| author | Adam Russell <ac.russell@live.com> | 2022-07-03 01:35:18 -0400 |
|---|---|---|
| committer | Adam Russell <ac.russell@live.com> | 2022-07-03 01:35:18 -0400 |
| commit | b93769510bc810b07e2708b0474496c2a6e331a6 (patch) | |
| tree | a7232d7b6c71ae606a40317b6478d289753b8fd2 /challenge-171/adam-russell/java/ch-2.java | |
| parent | 950917bcdffa35a582223cfb3cc64de598c33e5f (diff) | |
| download | perlweeklychallenge-club-b93769510bc810b07e2708b0474496c2a6e331a6.tar.gz perlweeklychallenge-club-b93769510bc810b07e2708b0474496c2a6e331a6.tar.bz2 perlweeklychallenge-club-b93769510bc810b07e2708b0474496c2a6e331a6.zip | |
initial commit
Diffstat (limited to 'challenge-171/adam-russell/java/ch-2.java')
| -rw-r--r-- | challenge-171/adam-russell/java/ch-2.java | 82 |
1 files changed, 82 insertions, 0 deletions
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 |
