diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-07-04 04:38:08 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-04 04:38:08 +0100 |
| commit | aaa9125297949777e07afaf0b8c6fbfc5a883a08 (patch) | |
| tree | 2827672bf3744f2281205a475e978a586aa5a990 /challenge-171/adam-russell/java/ch-2.java | |
| parent | 08f7aa01b62ef047d70fe0eaf949b99a4c0e7240 (diff) | |
| parent | b93769510bc810b07e2708b0474496c2a6e331a6 (diff) | |
| download | perlweeklychallenge-club-aaa9125297949777e07afaf0b8c6fbfc5a883a08.tar.gz perlweeklychallenge-club-aaa9125297949777e07afaf0b8c6fbfc5a883a08.tar.bz2 perlweeklychallenge-club-aaa9125297949777e07afaf0b8c6fbfc5a883a08.zip | |
Merge pull request #6387 from adamcrussell/challenge-171
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 |
