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

In java, write a program that prompts the user to enter the length of a square m

ID: 647292 • Letter: I

Question

In java, write a program that prompts the user to enter the length of a square matrix, randomly fills in 0s and 1s into the matrix, prints the matrix, and finds the rows, columns, and diagonals with all 0s or 1s. Here three sample runs of the program:

Enter the size of the matrix: 3
110
111
101
All 1s on row 1
All 1s on column 0
All 1s on major diagonal
Not same numbers on sub-diagonal

Enter the size of the matrix: 2
11
00
All 1s on row 0
All 0s on row 1
Not same numbers on a column
Not same numbers on major diagonal
Not same numbers on sub-diagonal

Enter the size of the matrix: 5
00001
00010
10110
00110
00000
All 0s on row 4
All 0s on column 1
Not same numbers on major diagonal
Not same numbers on sub-diagonal

Your program should format its output, and state its results, as shown above. This task is somewhat complex. Your solution is required to break the task down into smaller subtasks using method abstraction and stepwise refinement. Solve this problem by completing each of the provided method stubs. Here is a list of the stubbed methods:
public static void rowChecks(int[][] array)
public static void columnChecks(int[][] matrix)
public static void printTwoDArray(int[][] array)
public static int[][] randomZeroOneMatrix(int n)
public static int checkRowForSameness(int[][] matrix, int i) public static int checkColumnForSameness(int[][] matrix, int j)
public static void checkMajorDiagonal(int[][] matrix)
public static void checkSubDiagonal(int[][] matrix)

checkRowForSameness should be called from rowChecks, and checkColumnForSameness should be called from columnChecks. Each of the other six stub methods is called from main. You can generate a 0 or 1 randomly by using the call:
(int) (Math.random() * 2.0)

Explanation / Answer

Program code:

//import the required packages

import java.util.*;

//VerifyingTheMatrix will will verify for the same values on each rows and

//columns

public class VerifyingTheMatrix

{

     // main method

     public static void main(String args[])

     {

          // Scanner object to read data from the console

          Scanner input = new Scanner(System.in);

          // declare size to hold the size of the matrix

          int size;

          // declare an double dimensional array of integer type

          int matrix[][];

          // prompt the user to enter the values

          System.out.println("Enter the size of matrix: ");

          size = input.nextInt();

          // initialize the matrix

          matrix = new int[size][size];

          // fill the matrix by calling the method randomZeroOneMatrix

          matrix = randomZeroOneMatrix(size);

          // print the matrix by calling the printTwoDArray

          printTwoDArray(matrix);

          // print the row wise values by calling the rowChecks

          rowChecks(matrix);

          // print the column wise values by calling the columnChecks

          columnChecks(matrix);

          // print the major diagonal wise values by calling the

          // checkMajorDiagonal

          checkMajorDiagonal(matrix);

          // print the sub diagonal wise values by calling the checkSubDiagonal

          checkSubDiagonal(matrix);

     }

     // definition of rowChecks that internally calls for the

     // checkRowForSameness method

     public static void rowChecks(int[][] array)

     {

          int rowCounter = 0;

          int flag;

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

          {

              flag = checkRowForSameness(array, i);

              if (flag >= 0)

              {

                   System.out.println("All " + flag + "'s on row " + i + ".");

              } else

              {

                   rowCounter++;

              }

              if (rowCounter == array.length)

              {

                   System.out.println("No same numbers on a row.");

              }

          }

     }

     // definition of columnChecks that internally calls for the

     // checkColumnForSameness method

     public static void columnChecks(int[][] matrix)

     {

          int columnCounter = 0;

          int flagvalue;

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

          {

              flagvalue = checkColumnForSameness(matrix, i);

              if (flagvalue >= 0)

              {

                   System.out.println("All " + flagvalue + "'s on column " + i + ".");

              } else

              {

                   columnCounter++;

              }

              if (columnCounter == matrix.length)

              {

                   System.out.println("No same numbers on a column.");

              }

          }

     }

     // definition of printTwoDArray that prints the values of the matrix

     public static void printTwoDArray(int[][] array)

     {

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

          {

              for (int j = 0; j < array[i].length; j++)

              {

                   System.out.print(array[i][j] + " ");

              }

              System.out.println();

          }

          System.out.println();

     }

     // definition of the randomZeroOneMatrix method that randomly fills either

     // 1 or 0 into the matrix

     public static int[][] randomZeroOneMatrix(int n)

     {

          int array[][] = new int[n][n];

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

          {

              for (int j = 0; j < n; j++)

              {

                   array[i][j] = (int) (Math.random() * 2.0);

              }

          }

          return array;

     }

     // definition of the checkRowForSameness method that checks for the row

     // wise values

     public static int checkRowForSameness(int[][] matrix, int i)

     {

          int rowflag = 0;

          int rowcounter0 = 0;

          int rowcounter1 = 0;

          int k;

          for (k = 0; k < matrix.length - 1; k++)

          {

              if (matrix[i][k] == matrix[i][k + 1])

              {

                   if (matrix[i][k] == 1)

                        rowcounter1++;

                   else

                        rowcounter0++;

              }

          }

          if (rowcounter1++ >= matrix.length - 1)

              rowflag = 1;

          else if (rowcounter0++ >= matrix.length - 1)

              rowflag = 0;

          else

              rowflag = -1;

          return rowflag;

     }

     // definition of the checkColumnForSameness method that checks for the

     // column wise values

     public static int checkColumnForSameness(int[][] matrix, int j)

     {

          int colcounter0 = 0;

          int colcounter1 = 0;

          int k;

          int colflag = 0;

          for (k = 0; k < matrix[j].length - 1; k++)

          {

              if (matrix[k][j] == matrix[k + 1][j])

              {

                   if (matrix[k][j] == 1)

                        colcounter1++;

                   else

                        colcounter0++;

              }

          }

          if (colcounter1++ >= matrix.length - 1)

              colflag = 1;

          else if (colcounter0++ >= matrix.length - 1)

              colflag = 0;

          else

              colflag = -1;

          return colflag;

     }

     public static void checkMajorDiagonal(int[][] matrix)

     {

          int diagCounter0 = 0;

          int diagCounter1 = 0;

          for (int i = 0; i < matrix.length - 1; i++)

          {

              if (matrix[i][i] == 1)

              {

                   diagCounter1++;

              } else

              {

                   diagCounter0++;

              }

          }

          if (diagCounter0 == matrix.length)

              System.out.println("All " + 0 + "'s on major diagonol");

          else if (diagCounter1 == matrix.length)

              System.out.println("All " + 1 + "'s on major diagonol");

          else

              System.out.println("Not same numbers on major diagonal.");

     }

     public static void checkSubDiagonal(int[][] matrix)

     {

          for (int i = 0; i < matrix.length - 1; i++)

          {

              if (matrix[i][matrix.length - i - 1] != matrix[i + 1][matrix.length - i - 1 - 1])

              {

                   System.out.println("Not same numbers on sub-diagonal.");

              } else

              {

                   System.out

                             .println("All " + matrix[0][matrix.length - 1] + "'s on sub-diagonal");

              }

          }

     }

}

Sample Output:

test #1:

Enter the size of matrix:

4

0 1 0 0

0 1 0 1

0 1 0 1

0 0 0 1

No same numbers on a row.

All 0's on column 0.

All 0's on column 2.

Not same numbers on major diagonal.

All 0's on sub-diagonal

Not same numbers on sub-diagonal.

Not same numbers on sub-diagonal.

test #2:

Enter the size of matrix:

3

0 0 0

0 0 0

1 1 0

All 0's on row 0.

All 0's on row 1.

All 0's on column 2.

Not same numbers on major diagonal.

All 0's on sub-diagonal

Not same numbers on sub-diagonal.

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