From 947ab4cb0470e16f295d02a6dc3cc8c49cffdedb Mon Sep 17 00:00:00 2001 From: ISWB Prasetya <iswbprasetya@iswbs-mbp.cs.uu.nl> Date: Tue, 22 Aug 2017 13:49:55 +0200 Subject: [PATCH] fixing the small examples --- examples/Arrays0.java | 19 ++++++++ examples/Arrays2D.java | 88 +++++++++++++++++++++++++++++++++++ examples/SideEffects.java | 15 ++++++ examples/TryCatchFinally.java | 40 ++++++++++++++++ 4 files changed, 162 insertions(+) create mode 100644 examples/Arrays0.java create mode 100644 examples/Arrays2D.java create mode 100644 examples/SideEffects.java create mode 100644 examples/TryCatchFinally.java diff --git a/examples/Arrays0.java b/examples/Arrays0.java new file mode 100644 index 0000000..46db501 --- /dev/null +++ b/examples/Arrays0.java @@ -0,0 +1,19 @@ +public class Arrays0 +{ + public static void foo() + { + int x; + int y = 4+8; + int[] a = new int[12]; + int[] b = new int[0]; + a[5] = 2; + try + { + x = b[y]; + } + catch (ArrayIndexOutOfBoundsException e) + { + x = a[5]; + } + } +} \ No newline at end of file diff --git a/examples/Arrays2D.java b/examples/Arrays2D.java new file mode 100644 index 0000000..adb1ba6 --- /dev/null +++ b/examples/Arrays2D.java @@ -0,0 +1,88 @@ +// https://www.cs.utexas.edu/~scottm/cs307/javacode/codeSamples/Life.java + +import java.util.Scanner; + +public class Arrays2D { + + public static void show(boolean[][] grid){ + String s = ""; + for(int x = 0; x < 10; x++){ + for(int y = 0; y < 10; y++) + if(grid[x][y]) + s += "*"; + else + s += "."; + s += "\n"; + } + System.out.println(s); + } + + public static boolean[][] gen(){ + boolean[][] grid2 = new boolean[10][10]; + for(int r = 0; r < 10; r++) + { + for(int c = 0; c < 10; c++) + { + if( 1 > 0.7 ) + grid2[r][c] = true; + } + } + return grid2; + } + + public static void main(String[] args){ + boolean[][] world = gen(); + show(world); + System.out.println(); + world = nextGen(world); + show(world); + Scanner sc = new Scanner(System.in); + while(sc.nextLine().length() == 0){ + System.out.println(); + world = nextGen(world); + show(world); + + } + } + + public static boolean[][] nextGen(boolean[][] world){ + boolean[][] newWorld + = new boolean[world.length][world[0].length]; + int num; + for(int w = 0; w < world.length; w++){ + for(int l = 0; l < world[0].length; l++){ + num = numNeighbors(world, w, l); + if( occupiedNext(num, world[w][l]) ) + newWorld[w][l] = true; + } + } + return newWorld; + } + + public static boolean occupiedNext(int numNeighbors, boolean occupied){ + if(occupied && ((numNeighbors == 2) || (numNeighbors == 3))) + return true; + else if ((!occupied) && (numNeighbors == 3)) + return true; + else + return false; + } + + private static int numNeighbors(boolean[][] world, int row, int col) { + int num2 = world[row][col] ? -1 : 0; + for(int r1 = (row - 1); r1 <= (row + 1); r1++) + for(int c1 = (col - 1); c1 <= (col + 1); c1++) + if((inbounds(world, r1, c1)) && (world[r1][c1]) ) + num2++; + + return num2; + } + + private static boolean inbounds(boolean[][] world, int i, int c2) { + return (i >= 0) && (i < world.length) && (c2 >= 0) && + (c2 < world[0].length); + } + + + +} \ No newline at end of file diff --git a/examples/SideEffects.java b/examples/SideEffects.java new file mode 100644 index 0000000..11a386d --- /dev/null +++ b/examples/SideEffects.java @@ -0,0 +1,15 @@ +public class SideEffects +{ + static int x, y; + + public static void foo() { + x = 0; + y = 3; + int i = 0; + while(f() < y) { x = f(); } + x++; + x++; + } + + public static int f() { y++; return 3; } +} \ No newline at end of file diff --git a/examples/TryCatchFinally.java b/examples/TryCatchFinally.java new file mode 100644 index 0000000..1d8e563 --- /dev/null +++ b/examples/TryCatchFinally.java @@ -0,0 +1,40 @@ +// Source: http://stackoverflow.com/questions/3779285/exception-thrown-in-catch-and-finally-clause + + +class MyExc1 extends Exception {} +class MyExc2 extends Exception {} +class MyExc3 extends MyExc2 {} + +public class TryCatchFinally { + + public static void foo() throws Exception { + int x ; + try { + x = 1; + + try + { + throw new MyExc1(); + } + catch (Exception y) + { + } + finally + { + x = 3; + throw new Exception(); + } + } + catch (Exception i) + { + throw new MyExc2(); + } + finally { + x = 2; + } + } + + static void q() throws Exception { + + } +} \ No newline at end of file -- GitLab