Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The game of Life simulates organisms that inhabit an area. The organisms reprodu

ID: 667440 • Letter: T

Question

The game of Life simulates organisms that inhabit an area. The organisms reproduce and die out according to rules based on population density. The area is modeled in a 2-dimensional matrix with each entry in the matrix representing a cell in the living area. Time advances in discrete time units. At each time advance some new organisms may be born and existing ones may die. Existing organisms may die due to overcrowding or isolation. New ones are born when nearby cells reflect an appropriate population density.The game is thus parameterized by the particular population density ranges that allow organisms to be born or to continue to thrive. Each cell has up to eight neighbors (above, below, left, right, and four diagonals). Thus, if you count the cell as well, there are nine cells in its neighborhood. For a birth to occur, the cell must be empty and the neighborhood density be in the birth range. The birth range can be given as two integers from zero to nine, birthLow and birthHigh, so that a birth will occur if the cell is empty and the neighborhood population is at least birthLow and no more than birthHigh. Similarly, a death occurs in a cell if there is an organism there and the neighborhood has less than the minimum population for survival, or greater than the maximum. Hence there is a live range provided as two integers from zero to nine, liveLow and liveHigh. The border of the area is not compatible with life, so the top and bottom rows and the left and right columns will never have organisms. This part of the assignment will simply initialize and print the values for a start state in the game of Life. You do NOT have to implement the full game in this phase. Your program will read the parameters from standard input. This part of the program will have three inputs, the number of rows, the number of columns, and a seed to randomly fill the matrix. The number of rows and columns are integers, the seed is a long. You must read the values in exactly that order with no other input. You are to construct a two-dimensional matrix of booleans of the given size. You should fill the matrix with false. Then create a Random object initialized with the provided seed and fill the interior of the matrix row by row from left to right within each row. Do not place a new value in the first or last row, or the first or last column of the matrix as they may not contain an entity (and hence should remain false). You should use the nextBoolean method of the Random object to get each value. This will provide a matrix where each value (other than on the edge) is equally likely to be true or false. Finally, print the matrix by printing a - for each false and a # for each true. Include the outside rows and columns in the printing. Print a space before and after each - or #.

Instructions:

You must name your file and class "Life" for grading as well as compatibility with later assignments (so the full file name will be Life.java) You should use several public static methods with appropriate parameters in this assignment. Remember that each function should do one thing and do one thing well. You should not use any non-local (class or instance) variables

Sample Input

6 8

7

Sample Output

- - - - - - - -

- # # # - - - -

- # # # # - # -

- - - # # - # -

- # - # - # # -

- - - - - - - -

•You should not have a leading space (the first character of each line should be ‘-‘)

• You SHOULD have a trailing space (the last character of each line should be a space)

• You should have a trailing newline character. (so it will look like the last line of your output is blank)

• You should have a space after each output character (so that there is a column of spaces between each column of characters)

This is what i have so far, and i have no luck getting the right output.

public class Life {

   public static void main(String[] args) {
       // New Scanner
       Scanner console = new Scanner(System.in);
       // Gets number of rows from user
       System.out.println("What is the number of rows?");
       int rows = console.nextInt();
       // Gets number of columns from user
       System.out.println("What is the number of columns?");
       int columns = console.nextInt();
       // Gets seed number from user
       System.out.println("What is the seed number?");
       long seed = console.nextLong();
       // creates 2d array
       boolean matrix[][] = new boolean[rows][columns];
       rows = matrix.length;
       columns = matrix[0].length;
       for (int r = 0; r < rows; r++) {
           for (int c = 0; c < columns; c++) {
               matrix[r][c] = matrix[rows][columns];

           }

       }
       array(matrix, rows, columns, seed);
   }

   public static void array(boolean[][] matrix, int rows, int columns,
           long seed) {
       Random generator = new Random(seed);
       // fence post for loop to get true or false value
       for (int i = 1; i < rows - 1; i++) {
           for (int j = 1; j < columns - 1; j++) {
               boolean random = generator.nextBoolean();
               if (random == false) {
                   matrix[i][j] = false;
               } else {
                   matrix[i][j] = true;
               }

           }

       }
       // for loop to print matrix array
       for (int r = 0; r < rows; r++) {
           for (int c = 0; c < columns; c++) {
               // if false print -
               if (matrix[r][c] == false) {
                   System.out.print(" - ");
                   // if true print #
               } else {
                   System.out.print(" # ");
               }
           }
           // start new row
           System.out.println();

       }
   }
}

Explanation / Answer

boolean matrix[][] = new boolean[rows][columns];
       rows = matrix.length;
       columns = matrix[0].length;
       for (int r = 0; r < rows; r++) {
           for (int c = 0; c < columns; c++) {
               matrix[r][c] = matrix[rows][columns]; // you are assigning invalid

           }

       }

if you want to assign all elements as false initially, just make them

matrix[r][c] =false;

matrix[rows][columns] does not exist.

the rest of the code is correct

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote