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

Using java Write a program which asks the users for the dimensions of an n times

ID: 3832766 • Letter: U

Question

Using java Write a program which asks the users for the dimensions of an n times n matrix. The matrix should be randomly populated with the characters x and o. Display the matrix then report all rows, columns, and both diagonals that have all x's or all o's. Enter matrix size: 5 x o x x o o x o o o x x x x x o o x x o o o o o x All x's in row 3. All x's in major diagonal. Enter matrix size: 6 o x o o o x o x o o o o x x x x o o o x x x o x o x o o o x x x o o o x All x's in column 2. All o's in column 5.

Explanation / Answer

package org.students;

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

public class Matrix0OrX {

   public static void main(String[] args) {

       // Declaring variables
       int size, random;

       // Creating a random Class object
       Random r = new Random();

       // Scanner object is used to get the inputs entered by the user
       Scanner sc = new Scanner(System.in);

       // getting the size of the matrix entered by the user
       System.out.print("Enter matix size :");
       size = sc.nextInt();

       // Creating a 2_D matrix of tyep char based on user entered size
       char matrix[][] = new char[size][size];

       // Randomly Populating 'O' and 'X' into the array locations
       for (int i = 0; i < size; i++) {
           for (int j = 0; j < size; j++) {
               random = r.nextInt(2);
               if (random == 0) {
                   matrix[i][j] = 'O';
               } else {
                   matrix[i][j] = 'X';
               }
           }
       }

       // displaying the matrix
       System.out.println();
       for (int i = 0; i < size; i++) {
           for (int j = 0; j < size; j++) {

               System.out.print(matrix[i][j] + " ");
           }
           System.out.println();
       }

       // Checking if any row is having all 'X's
       int res;
       for (int i = 0; i < size; i++) {
           res = 0;
           if (matrix[i][0] == 'X') {
               res = checkXRows(matrix, i);
               if (res == 1) {
                   System.out.println("All X's in row " + (i + 1));
               }
           }

       }

       // Checking if any row is having all 'O's
       for (int i = 0; i < size; i++) {
           res = 0;
           if (matrix[i][0] == 'O') {
               res = checkORows(matrix, i);
               if (res == 1) {
                   System.out.println("All O's in row " + (i + 1));
               }
           }

       }

       // Checking if any column is having all 'X's
       for (int i = 0; i < size; i++) {
           res = 0;
           if (matrix[i][0] == 'X') {
               res = checkXCols(matrix, i);
               if (res == 1) {
                   System.out.println("All X's in col " + (i + 1));
               }
           }

       }

       // Checking if any column is having all 'O's
       for (int i = 0; i < size; i++) {
           res = 0;
           if (matrix[i][0] == 'O') {
               res = checkOCols(matrix, i);
               if (res == 1) {
                   System.out.println("All O's in col " + (i + 1));
               }
           }

       }

       // Checking if major Diagonal is having all 'X's
       if (matrix[0][0] == 'X') {
           res = checkMajorDiagonalForX(matrix);
           if (res == 1)
               System.out.println("All X's in Major Diagonal");
       }

       // Checking if major Diagonal is having all 'O's
       if (matrix[0][0] == 'O') {
           res = checkMajorDiagonalForO(matrix);
           if (res == 1)
               System.out.println("All O's in Major Diagonal");
       }

       // Checking if second Diagonal is having all 'X's
       if (matrix[0][0] == 'X') {
           res = checkSecondDiagonalForX(matrix);
           if (res == 1)
               System.out.println("All X's in Major Diagonal");
       }

       // Checking if second Diagonal is having all 'O's
       if (matrix[0][0] == 'O') {
           res = checkSecondDiagonalForO(matrix);
           if (res == 1)
               System.out.println("All O's in Major Diagonal");
       }

   }

   // Checking if second Diagonal is having all 'O's
   private static int checkSecondDiagonalForO(char[][] matrix) {
       for (int i = 0; i < matrix.length; i++) {
           for (int j = 0; j < matrix[0].length; j++) {
               if (i + j == (matrix.length - 1)) {
                   if (matrix[i][j] != 'O')
                       return 0;
               }
           }
       }
       return 1;
   }

   // Checking if second Diagonal is having all 'X's
   private static int checkSecondDiagonalForX(char[][] matrix) {
       for (int i = 0; i < matrix.length; i++) {
           for (int j = 0; j < matrix[0].length; j++) {
               if (i + j == (matrix.length - 1)) {
                   if (matrix[i][j] != 'X')
                       return 0;
               }
           }
       }
       return 1;

   }

   // Checking if major is having all 'O's
   private static int checkMajorDiagonalForO(char[][] matrix) {
       for (int i = 0; i < matrix.length; i++) {
           for (int j = 0; j < matrix[0].length; j++) {
               if (i == j) {
                   if (matrix[i][j] != 'O')
                       return 0;
               }
           }
       }
       return 1;
   }

   // Checking if major is having all 'X's
   private static int checkMajorDiagonalForX(char[][] matrix) {
       for (int i = 0; i < matrix.length; i++) {
           for (int j = 0; j < matrix[0].length; j++) {
               if (i == j) {
                   if (matrix[i][j] != 'X')
                       return 0;
               }
           }
       }
       return 1;
   }

   // Checking if any Column is having all 'O's
   private static int checkOCols(char[][] matrix, int col) {
       for (int j = 0; j < matrix.length; j++) {
           if (matrix[j][col] != 'O')
               return 0;
       }
       return 1;
   }

   // Checking if any Column is having all 'X's
   private static int checkXCols(char[][] matrix, int col) {
       for (int j = 0; j < matrix.length; j++) {
           if (matrix[j][col] != 'X')
               return 0;
       }
       return 1;
   }

   // Checking if any row is having all 'O's
   private static int checkORows(char[][] matrix, int row) {
       for (int j = 0; j < matrix.length; j++) {
           if (matrix[row][j] != 'O')
               return 0;
       }
       return 1;
   }

   // Checking if any row is having all 'X's
   private static int checkXRows(char[][] matrix, int row) {

       for (int j = 0; j < matrix.length; j++) {
           if (matrix[row][j] != 'X')
               return 0;
       }
       return 1;

   }

}

______________________

Output#1:

Enter matix size :4

O X O O
X X X X
O O O O
X O X X
All X's in row 2

_______________

Output#2:

Enter matix size :4

X O O O
O O O O
O O X X
O O O X
All O's in row 2
All O's in col 2

_____________Thank You

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