diff --git a/examples/Arrays0.java b/examples/Arrays0.java new file mode 100644 index 0000000000000000000000000000000000000000..46db501e202489e427c4a66e4d36d0894d52aebc --- /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 0000000000000000000000000000000000000000..adb1ba66c863237f2a1803523206449a2bda61c3 --- /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 0000000000000000000000000000000000000000..11a386dc396679e23a36ce385e9245fd9b12da1e --- /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 0000000000000000000000000000000000000000..1d8e563a6cb1f81715d8a8ce8ee4234f875c9c21 --- /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