/** * Test how to generate and display a two-dimensional random array. * For classroom demonstration. * * * @author Xiannong Meng * * August 2006 */ import java.io.*; import java.util.*; class TestRandomArray { /* * Some useful constants */ private static final double MAX_PCNT_LIV = 0.8; // upper limit private static final double MIN_PCNT_LIV = 0.2; // lower limit /* * The array */ private boolean [][] mBoard; // Good for a two-dimensional world private int mDimension; // the size of the board /** * Constructor that initializes the two dimensional array * * @param dimension: an integer specifying the dimension of the array * @param probLife: initial probability of life in a cell */ public TestRandomArray(int dimension, double probLife) { mDimension = dimension; mBoard = new boolean[mDimension][mDimension]; for (int i = 0; i < mBoard.length; i++) for (int j = 0; j < mBoard[i].length; j++) mBoard[i][j] = (Math.random() < probLife); } /** * display prints the board in text form on the screen */ public void display() { for (int i = 0; i < mBoard.length; i++) { for (int j = 0; j < mBoard[i].length; j++) if (mBoard[i][j] == true) System.out.print("+"); else System.out.print("-"); System.out.println(); } } /** * A simple, minimum test program to see if the 2-D random array works. */ public static void main(String[] argv) { TestRandomArray runATest = new TestRandomArray(8, 0.4); runATest.display(); } }