Skip to content
Snippets Groups Projects
Commit 947ab4cb authored by ISWB Prasetya's avatar ISWB Prasetya
Browse files

fixing the small examples

parent b91e4bda
No related branches found
No related tags found
No related merge requests found
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
// 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
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
// 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment