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

IN JAVA input: 6 8 7 3 8 3 8 output: Fix the code below: import java.util.Random

ID: 668646 • Letter: I

Question

IN JAVA

input: 6 8 7 3 8 3 8

output:

Fix the code below:

import java.util.Random;
import java.util.Scanner;

public class Life {
   public static void main(String[] args) {
       // New Scanner
       Scanner console = new Scanner(System.in);
       // Gets number of rows from user
       int rows = console.nextInt();
       // Gets number of columns from user
       int columns = console.nextInt();
       // Gets seed number from user
       long seed = console.nextLong();
       // Gets least number in birth range from user
       // must be >=0
       int birthLow = console.nextInt();
       if (birthLow < 0) {
           throw new IllegalStateException("Range must be between 0 and 9");
       }
       // Gets greatest number in birth range from user
       // must be <= 9
       int birthHigh = console.nextInt();
       if (birthHigh > 9) {
           throw new IllegalStateException("Range must be between 0 and 9");
       }
       // birthHigh >= birthLow
       if (birthHigh < birthLow) {
           throw new IllegalStateException(
                   "birthHigh must be greater than birthLow");
       }
       // Gets least number in live range from user
       // must be >=0
       int liveLow = console.nextInt();
       if (liveLow < 0) {
           throw new IllegalStateException("Range must be between 0 and 9");
       }
       // Gets greatest number in live range from user
       // must be <= 9
       int liveHigh = console.nextInt();
       if (liveHigh > 9) {
           throw new IllegalStateException("Range must be between 0 and 9");
       }
       // liveHigh >= liveLow
       if (liveHigh < liveLow) {
           throw new IllegalStateException(
                   "liveHigh must be greater than liveLow");
       }

       // creates 2d array
       boolean matrix[][] = new boolean[rows][columns];

       // call methods to print first matrix
       array(matrix, rows, columns, seed);
       printArray(matrix, rows, columns);
       System.out.println();
       // get matrix to update and print the new matrix 4 times
       for (int i = 0; i < 4; i++) {
           updateMatrix(matrix, rows, columns, birthLow, birthHigh, liveLow,
                   liveHigh);
           printArray(matrix, rows, columns);
           System.out.println();
       }

   }

   public static void array(boolean[][] matrix, int rows, int columns,
           long seed) {
       // creates random
       Random generator = new Random(seed);
       // 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;
               }
           }
       }
   }

   public static void printArray(boolean[][] matrix, int rows, int columns) {
       // 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();
       }
   }

   public static void updateMatrix(boolean[][] matrix, int rows, int columns,
           int birthLow, int birthHigh, int liveLow, int liveHigh) {
       // creates clone of matrix
       boolean[][] newMatrix = (boolean[][]) matrix.clone();
       for (int row = 0; row < matrix.length; row++) {
           newMatrix[row] = (boolean[]) newMatrix[row].clone();
           // nested for loop for rows and columns
           for (int i = 1; i < rows - 1; i++) {
               for (int j = 1; j < columns - 1; j++) {

                   // if dead or "- " determine if born again or stay dead
                   if (!matrix[i][j]) {
                       // test for life, if life is there increase by 1
                       int count = 0;

                       if (matrix[i - 1][j - 1]) {
                           count = count + 1;
                       }
                       if (matrix[i - 1][j]) {
                           count = count + 1;
                       }
                       if (matrix[i - 1][j + 1]) {
                           count = count + 1;
                       }
                       if (matrix[i][j - 1]) {
                           count = count + 1;
                       }
                       if (matrix[i][j + 1]) {
                           count = count + 1;
                       }
                       if (matrix[i + 1][j + 1]) {
                           count = count + 1;
                       }
                       if (matrix[i + 1][j - 1]) {
                           count = count + 1;
                       }
                       if (matrix[i + 1][j]) {
                           count = count + 1;
                       } else {

                       }
                       // if >=birthLow && <=birthHigh birth will occur
                       if (count >= birthLow && count <= birthHigh) {
                           newMatrix[i][j] = true;
                       }
                       // if live or "# " determine if dies or stays alive
                   } else {
                       int count2 = 1;
                       // if true count2 increase by 1
                       if (matrix[i - 1][j - 1]) {
                           count2 = count2 + 1;
                       }
                       if (matrix[i - 1][j]) {
                           count2 = count2 + 1;
                       }
                       if (matrix[i - 1][j + 1]) {
                           count2 = count2 + 1;
                       }
                       if (matrix[i][j - 1]) {
                           count2 = count2 + 1;
                       }
                       if (matrix[i][j + 1]) {
                           count2 = count2 + 1;
                       }
                       if (matrix[i + 1][j + 1]) {
                           count2 = count2 + 1;
                       }
                       if (matrix[i + 1][j - 1]) {
                           count2 = count2 + 1;
                       }
                       if (matrix[i + 1][j]) {
                           count2 = count2 + 1;
                       }
                       // if <=liveLow or >=liveHigh death will occur
                       if (count2 >= liveHigh || count2 <= liveLow) {
                           newMatrix[i][j] = false;
                       } else {

                       }
                   }
               }
           }
       }
   }
}

Sample Output

Explanation / Answer

import java.util.Random;

import java.util.Scanner;

public class Life

{

public static void main(String[] args)

{

// New Scanner

Scanner console = new Scanner(System.in);

// Gets number of rows from user

int rows = console.nextInt();

// Gets number of columns from user

int columns = console.nextInt();

// Gets seed number from user

long seed = console.nextLong();

// Gets least number in birth range from user

// must be >=0

int birthLow = console.nextInt();

if(birthLow < 0)

{

throw new IllegalStateException("Range must be between 0 and 9");

}

// Gets greatest number in birth range from user

// must be <= 9

int birthHigh = console.nextInt();

if(birthHigh > 9)

{

throw new IllegalStateException("Range must be between 0 and 9");

}

// birthHigh >= birthLow

if(birthHigh < birthLow)

{

throw new IllegalStateException(

"birthHigh must be greater than birthLow");

}

// Gets least number in live range from user

// must be >=0

int liveLow = console.nextInt();

if(liveLow < 0)

{

throw new IllegalStateException("Range must be between 0 and 9");

}

// Gets greatest number in live range from user

// must be <= 9

int liveHigh = console.nextInt();

if(liveHigh > 9)

{

throw new IllegalStateException("Range must be between 0 and 9");

}

// liveHigh >= liveLow

if(liveHigh < liveLow)

{

throw new IllegalStateException(

"liveHigh must be greater than liveLow");

}

// creates 2d array

boolean matrix[][] = new boolean[rows][columns];

// call methods to print first matrix

array(matrix, rows, columns, seed);

printArray(matrix, rows, columns);

System.out.println();

// get matrix to update and print the new matrix 4 times

for(int i = 0; i < 4; i++)

{

updateMatrix(matrix, rows, columns, birthLow, birthHigh, liveLow,

liveHigh);

printArray(matrix, rows, columns);

System.out.println();

}

}

public static void array(boolean[][] matrix, int rows, int columns,

long seed)

{

// creates random

Random generator = new Random(seed);

// 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;

}

}

}

}

public static void printArray(boolean[][] matrix, int rows, int columns)

{

// 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();

}

}

public static void updateMatrix(boolean[][] matrix, int rows, int columns,

int birthLow, int birthHigh, int liveLow, int liveHigh)

{

// creates clone of matrix

//boolean[][] newMatrix = (boolean[][]) matrix.clone();

boolean[][] newMatrix = copy(matrix);

for(int row = 0; row < matrix.length; row++)

{

newMatrix[row] = (boolean[]) newMatrix[row].clone();

// nested for loop for rows and columns

for(int i = 1; i < rows - 1; i++)

{

for(int j = 1; j < columns - 1; j++)

{

// if dead or "- " determine if born again or stay dead

if(!matrix[i][j])

{

// test for life, if life is there increase by 1

int count = 0;

if(matrix[i - 1][j - 1])

{

count = count + 1;

}

if(matrix[i - 1][j])

{

count = count + 1;

}

if(matrix[i - 1][j + 1])

{

count = count + 1;

}

if(matrix[i][j - 1])

{

count = count + 1;

}

if(matrix[i][j + 1])

{

count = count + 1;

}

if(matrix[i + 1][j + 1])

{

count = count + 1;

}

if(matrix[i + 1][j - 1])

{

count = count + 1;

}

if(matrix[i + 1][j])

{

count = count + 1;

}

else

{}

// if >=birthLow && <=birthHigh birth will occur

if(count >= birthLow && count <= birthHigh)

{

newMatrix[i][j] = true;

}

// if live or "# " determine if dies or stays alive

}

else

{

int count2 = 1;

// if true count2 increase by 1

if(matrix[i - 1][j - 1])

{

count2 = count2 + 1;

}

if(matrix[i - 1][j])

{

count2 = count2 + 1;

}

if(matrix[i - 1][j + 1])

{

count2 = count2 + 1;

}

if(matrix[i][j - 1])

{

count2 = count2 + 1;

}

if(matrix[i][j + 1])

{

count2 = count2 + 1;

}

if(matrix[i + 1][j + 1])

{

count2 = count2 + 1;

}

if(matrix[i + 1][j - 1])

{

count2 = count2 + 1;

}

if(matrix[i + 1][j])

{

count2 = count2 + 1;

}

// if <=liveLow or >=liveHigh death will occur

if(count2 >= liveHigh || count2 <= liveLow)

{

newMatrix[i][j] = false;

}

else

{}

}

}

}

}

}

public static boolean[][] copy(boolean[][] arr)

{

boolean[][] temp = arr.clone();

for(int i = 0; i < temp.length; ++i)

{

temp[i] = temp[i].clone();

}

return temp;

}

}