7.1 import java.util.Scanner;public class Exercise01 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("Enter a 4-by-4 matrix row by row:");int[][] matrix = new int[4][4];for (int i = 0; i < 4; i++) {for (int j = 0; j < 4; j++) {matrix[i][j] = input.nextInt();}}System.out.println("Sum of the matrix is " + sumMatrix(matrix));}public static int sumMatrix(int[][] m) {int sum = 0;for (int i = 0; i < m.length; i++) {for (int j = 0; j < m[i].length; j++) {sum += m[i][j];}}return sum;}}7.2 import java.util.Scanner;public class Exercise02 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("Enter a 4-by-4 matrix row by row:");int[][] matrix = new int[4][4];for (int i = 0; i < matrix.length; i ++) {for (int j = 0; j < matrix[i].length; j++) {matrix[i][j] = input.nextInt();}}System.out.println("Sum of the elements in the major diagonal is " + sumMajorDiagonal(matrix));}public static int sumMajorDiagonal(int[][] m) {int sum = 0;for (int i = 0, j = 0; i < m.length && j < m[0].length; i++, j++)sum += m[i][j];return sum;}}7.3 public class Exercise03 {public static void main(String[] args) {//Student's answers to the questionschar[][] answers = {{'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},{'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'},{'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'},{'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'},{'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},{'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},{'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},{'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}};//Key to the questionschar[] keys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'};int[][] countOfCorrect = new int[answers.length][2];//Grand all answersfor (int i = 0; i < answers.length; i++) {//Grade one studentint correctCount = 0;for (int j = 0; j < answers[i].length; j++) {if (answers[i][j] == keys[j])correctCount++;}countOfCorrect[i][1] = correctCount;countOfCorrect[i][0] = i;}for (int i = 0; i < countOfCorrect.length; i++) {int currentStudent = countOfCorrect[i][0];int min = countOfCorrect[i][1];int minIndex = i;for (int j = i + 1; j < countOfCorrect.length; j++) {if (countOfCorrect[j][1] < min) {currentStudent = countOfCorrect[j][0];min = countOfCorrect[j][1];minIndex = j;}}if (minIndex != i) {countOfCorrect[minIndex][0] = countOfCorrect[i][0];countOfCorrect[minIndex][1] = countOfCorrect[i][1];countOfCorrect[i][0] = currentStudent;countOfCorrect[i][1] = min;}}for (int j = 0; j < countOfCorrect.length; j++)System.out.println("Student " + countOfCorrect[j][0] + "'s correct count is " + countOfCorrect[j][1]);}}7.4 public class Exercise04 {public static void main(String[] args) {int[][] workHours = {{2, 4, 3, 4, 5, 8, 8},{7, 3, 4, 3, 3, 4, 4},{9, 3, 4, 7, 3, 4, 1},{3, 5, 3, 4, 6, 3, 8},{3, 4, 4, 6, 3, 4, 4},{3, 7, 4, 8, 3, 8, 4},{6, 3, 5, 9, 2, 7, 9}};int[][] totalWorkHours = new int[workHours.length][2];for (int i = 0; i < workHours.length; i++) {int sum = 0;for (int j = 0; j < workHours[i].length; j++) {sum += workHours[i][j];}totalWorkHours[i][0] = i;totalWorkHours[i][1] = sum;}int[][] sortTotalWorkHours = sortArray(totalWorkHours);for (int j = 0; j < sortTotalWorkHours.length; j++)System.out.println("Employee " + sortTotalWorkHours[j][0] + "'s total work hours is " + sortTotalWorkHours[j][1]);}public static int[][] sortArray(int[][] array) {for (int i = 0; i < array.length; i++) {int currentEmployee = array[i][0];int currentMaxWorkHours = array[i][1];int currentMaxIndex = i;for (int j = i + 1; j < array.length; j++) {if (array[j][1] > currentMaxWorkHours) {currentEmployee = array[j][0];currentMaxWorkHours = array[j][1];currentMaxIndex = j;}}if (currentMaxIndex != i) {array[currentMaxIndex][0] = array[i][0];array[currentMaxIndex][1] = array[i][1];array[i][0] = currentEmployee;array[i][1] = currentMaxWorkHours;}}return array;}}7.5 import java.util.Scanner;public class Exercise05 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("Enter a 4-by-4 matrix1:");int[][] matrix1 = new int[4][4];int[][] matrix2 = new int[4][4];for (int i = 0; i < matrix1.length; i++) {for (int j = 0; j < matrix1[i].length; j++)matrix1[i][j] = input.nextInt();}System.out.println("Enter a 4-by-4 matrix2:");for (int i = 0; i < matrix2.length; i++) {for (int j = 0; j < matrix2[i].length; j++)matrix2[i][j] = input.nextInt();}int[][] sumOfMatrix = addMatrix(matrix1, matrix2);printMatrix(matrix1, matrix2, sumOfMatrix);}public static int[][] addMatrix(int[][] a, int[][] b) {int[][] sum = new int[a.length][a[0].length];for (int i = 0; i < a.length; i++) {for (int j = 0; j < a[i].length; j++)sum[i][j] = a[i][j] + b[i][j];}return sum;}public static void printMatrix(int[][] matrix1, int[][] matrix2, int[][] sumOfMatrix) { for (int i = 0; i < matrix1.length; i++) {for (int j = 0; j < matrix1[i].length; j++) {System.out.print((matrix1[i][j] < 10 ? " " : "") + matrix1[i][j] + " ");}if (i != Math.ceil(matrix1.length / 2))System.out.print(" ");elseSystem.out.print("+ ");for (int j = 0; j < matrix2[i].length; j++) {System.out.print((matrix2[i][j] < 10 ? " " : "") + matrix2[i][j] + " ");}if (i != Math.ceil(matrix1.length / 2))System.out.print(" ");elseSystem.out.print("= ");for (int j = 0; j < matrix1[i].length; j++) {System.out.print((sumOfMatrix[i][j] < 10 ? " " : "") + sumOfMatrix[i][j] + "");}System.out.println();}}}7.6 import java.util.Scanner;public class Exercise06 {public static void main(String[] args) {Scanner input = new Scanner(System.in);double[][] matrix1 = new double[3][3];double[][] matrix2 = new double[3][3];System.out.println("Enter a 3-by-3 matrix1:");for (int i = 0; i < matrix1.length; i++) {for (int j = 0; j < matrix1[i].length; j++)matrix1[i][j] = input.nextDouble();}System.out.println("Enter a 3-by-3 matrix2:");for (int i = 0; i < matrix2.length; i++) {for (int j = 0; j < matrix2[i].length; j++)matrix2[i][j] = input.nextDouble();}double[][] multiplyMatrix = multiplyMatrix(matrix1, matrix2);for (int i = 0; i < matrix1.length; i++) {for (int j = 0; j < matrix1[i].length; j++) {System.out.print(matrix1[i][j] + " ");}if (i != Math.ceil(matrix1.length / 2))System.out.print(" ");elseSystem.out.print("* ");for (int j = 0; j < matrix2[i].length; j++) {System.out.print(matrix2[i][j] + " ");}if (i != Math.ceil(matrix1.length / 2))System.out.print(" ");elseSystem.out.print("= ");for (int j = 0; j < matrix1[i].length; j++) {System.out.print((int)(multiplyMatrix[i][j] * 10) / 10.0 + " ");}System.out.println();}}public static double[][] multiplyMatrix(double[][] a, double[][] b) {double[][] multiply = new double[a.length][a[0].length];for (int i = 0; i < a.length; i++) {for (int j = 0; j < multiply[i].length; j++) {for (int m = 0; m < a.length; m++)multiply[i][j] += a[i][m] * b[m][j];}}return multiply;}}7.7 public class Exercise07 {public static void main(String[] args) {double[][] points = {{-1, 0, 3}, {-1, -1, -1}, {4, 1, 1}, {2, 0.5, 9}, {3.5, 2, -1}, {3, 1.5, 3}, {-1.5, 4, 2}, {5.5, 4, -0.5}};//p1 and p2 are the indices in the points arrayint p1 = 0, p2 = 1; //Initial two pointsdouble shortestDistance = distance(points[p1][0], points[p1][1], points[p1][2],points[p2][0], points[p2][1], points[p2][2]); //Initialize shortestDistance//Compute distance for every two pointsfor (int i = 0; i < points.length; i++) {for (int j = i + 1; j < points.length; j++) {double distance = distance(points[i][0], points[i][1], points[i][2],points[j][0], points[j][1], points[j][2]); //Find distanceif (distance < shortestDistance) {p1 = i; //Update p1p2 = j; //Update p2shortestDistance = distance; //Update shortestDistance}}}//Display resultSystem.out.println("The closest two points are " + "(" + points[p1][0] + ", " +points[p1][1] + points[p1][2] + ") and (" +points[p2][0] + ", " + points[p2][1] + points[p2][2] + ")");}public static double distance(double x1, double y1, double z1, double x2, double y2, double z2) {return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1));}}7.8 import java.util.Scanner;public class Exercise08 {public static void main(String[] args) {double[][] points = {{-1, 3}, {-1, -1}, {1, 1}, {0, 0.5},{2, 0.5}, {2, -1}, {3, 3}, {4, 2}, {4, -0.5}};//p1 and p2 are the indices in the points arrayint p1 = 0, p2 = 1; //Initial two pointsdouble shortestDistance = distance(points[p1][0], points[p1][1],points[p2][0], points[p2][1]); //Initialize shortestDistanceint[][] indices = new int[points.length][2];int k = 0;indices[k][0] = p1;indices[k][1] = p2;//Compute distance for every two pointsfor (int i = 0; i < points.length; i++) {for (int j = i + 1; j < points.length; j++) {double distance = distance(points[i][0], points[i][1],points[j][0], points[j][1]); //Find distanceif (distance < shortestDistance) {indices[k][0] = i; //Update p1indices[k][1] = j; //Update p2shortestDistance = distance; //Update shortestDistance}else if (distance == shortestDistance) {k++;indices[k][0] = i;indices[k][1] = j;}}}// Display all closest pairsfor (int i = 0; i <= k; i++) {p1 = indices[i][0]; p2 = indices[i][1];System.out.println("The closest two points are " +"(" + points[p1][0] + ", " + points[p1][1] + ") and (" +points[p2][0] + ", " + points[p2][1] + ")");}System.out.println("Their distance is " + shortestDistance);}public static double distance(double x1, double y1, double x2, double y2) { return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));}}7.9 import java.util.Scanner;public class Exercise09 {public static void main(String[] args) {char[][] ticTacToe = new char[3][3];printTable(ticTacToe);enterX(ticTacToe);}/*** Player X to enter X*/public static void enterX(char[][] ticTacToe) {Scanner input = new Scanner(System.in);System.out.print("Enter a row (0, 1 or 2) for player X: ");int rowForX = input.nextInt();System.out.print("Enter a column (0, 1 or 2) for player X: ");int columnForX = input.nextInt();ticTacToe[rowForX][columnForX] = 'X';printTable(ticTacToe);if (status(ticTacToe)) {enterO(ticTacToe);}}/*** Player O to enter O*/public static void enterO(char[][] ticTacToe) {Scanner input = new Scanner(System.in);System.out.print("Enter a row (0, 1 or 2) for player O: ");int rowForO = input.nextInt();System.out.print("Enter a column (0, 1 or 2) for player O: ");int columnForO = input.nextInt();ticTacToe[rowForO][columnForO] = 'O';printTable(ticTacToe);if (status(ticTacToe)) {enterX(ticTacToe);}}/*** Display the board*/public static void printTable(char[][] array) {System.out.println("-------------");for (int i = 0; i < array.length; i++) {System.out.print("|");for (int j = 0; j < array[i].length; j++) {if (array[i][j] == 'X' | array[i][j] == 'O')System.out.print(" " + array[i][j] + " ");elseSystem.out.print(" ");System.out.print("|");}System.out.println();System.out.println("-------------");}}/*** Return false if one player wins or all the cells have been filled, return true if nobody wins */public static boolean status(char[][] array) {//return false if find one row, column or diagonal with all Xs or Osint i = 0, j = 0;while (i < 3 && j < 3) {if (array[i][j] == 'X' || array[i][j] == 'O') {if (i == 0 && j == 0) {if ((array[i][j] == array[i][j + 1] && array[i][j + 1] == array[i][j + 2]) ||(array[i][j] == array[i + 1][j] && array[i + 1][j] == array[i + 2][j]) ||(array[i][j] == array[i + 1][j + 1] && array[i + 1][j + 1] == array[i + 2][j + 2])) {System.out.println("Player " + array[i][j] + " won");return false;}}if (i == 1 && j == 1) {if ((array[i][j] == array[i][j - 1] && array[i][j] == array[i][j + 1]) ||(array[i][j] == array[i - 1][j] && array[i][j] == array[i + 1][j]) ||(array[i][j] == array[i - 1][j + 1] && array[i][j] == array[i + 1][j -1])) {System.out.println("Player " + array[i][j] + " won");return false;}}if (i == 2 && j == 2) {if ((array[i][j] == array[i][j - 1] && array[i][j] == array[i][j - 2]) ||(array[i][j] == array[i - 1][j] && array[i][j] == array[i - 2][j])) {System.out.println("Player " + array[i][j] + " won");return false;}}}i++;j++;}//return true if not all the cells have been filledfor (int m = 0; m < array.length; m++) {for (int n = 0; n < array[m].length; n++) {if (array[m][n] != 'X' && array[m][n] != 'O')return true;}}//return false if all the cells have been filledSystem.out.println("It's a draw");return false;}}7.10 public class Exercise10 {public static void main(String[] args) {int[][] ticTacToe = new int[3][3];for (int i = 0; i < ticTacToe.length; i++) {for (int j = 0; j < ticTacToe[i].length; j++)ticTacToe[i][j] = (int)(Math.random() * 2);}for (int i = 0; i < ticTacToe.length; i++) {for (int j = 0; j < ticTacToe[i].length; j++)System.out.print(ticTacToe[i][j]);System.out.println();}all0sOr1s(ticTacToe);}public static void all0sOr1s(int[][] array) {int i = 0, j = 0;while (i < array.length && j < array.length) {if (i == 0 && j == 0) {if (array[i][j] == array[i][j + 1] && array[i][j + 1] == array[i][j + 2])System.out.println("All " + array[i][j] + "s on row 0");if (array[i][j] == array[i + 1][j] && array[i + 1][j] == array[i + 2][j])System.out.println("All " + array[i][j] + "s on column 0");if (array[i][j] == array[i + 1][j + 1] && array[i + 1][j + 1] == array[i + 2][j + 2])System.out.println("All " + array[i][j] + "s on major diagonal");}if (i == 1 && j == 1) {if (array[i][j] == array[i][j - 1] && array[i][j] == array[i][j + 1])System.out.println("All " + array[i][j] + "s on row 1");if (array[i][j] == array[i - 1][j] && array[i][j] == array[i + 1][j])System.out.println("All " + array[i][j] + "s on column 1");if (array[i][j] == array[i - 1][j + 1] && array[i][j] == array[i + 1][j -1])System.out.println("All " + array[i][j] + "s on sub-diagonal");}if (i == 2 && j == 2) {if (array[i][j] == array[i][j - 1] && array[i][j] == array[i][j - 2])System.out.println("All " + array[i][j] + "s on row 2");if (array[i][j] == array[i - 1][j] && array[i][j] == array[i - 2][j])System.out.println("All " + array[i][j] + "s on column 2");}i++;j++;}}}7.11 import java.util.Scanner;public class Exercise11 {public static void main(String[] args) {//Create a ScannerScanner input = new Scanner(System.in);//Prompt the user to enter a numberSystem.out.print("Enter a number between 0 and 511: ");int number = input.nextInt();//Change the number to binaryint[][] matrix = decimalToBinary(number);//Display the matrix with charactersprintMatrix(matrix);}/*** Change a decimal number to a binary number*/public static int[][] decimalToBinary(int number) {int[][] matrix = new int[3][3];for (int i = matrix.length - 1; i >= 0; i--) {for (int j = matrix[i].length - 1; j >= 0; j--) {matrix[i][j] = number % 2;number = number / 2;}}return matrix;}/*** Print the matrix with characters*/public static void printMatrix(int[][] matrix) {for (int i = 0; i < matrix.length; i++) {for (int j = 0; j < matrix[i].length; j++) {if (matrix[i][j] == 0)System.out.print("H ");if (matrix[i][j] == 1)System.out.print("T ");}System.out.println();}}}7.12 import java.util.Scanner;public class Exercise12 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter the taxable income: ");double income = input.nextDouble();System.out.print("0-single filer, 1-married joinly,\n" +"2-married separately, 3-head of household\n" +"Enter the filing status: ");int status = input.nextInt();System.out.println("Tax is " + (int)(computeTax(income, status) * 100) / 100.0);}public static double computeTax(double income, int status) {double[] rates = {0.10, 0.15, 0.25, 0.28, 0.33, 0.35};int[][] brackets = {{8350, 33950, 82250, 171550, 372950}, //Single filer{16700, 67900, 137050, 208850, 372950}, //Married jointly{8350, 33950, 68525, 104425, 186475}, //Married separately{11950, 45500, 117450, 190200, 372950} //Head of household };double tax = 0;if (status == 0) {if (income <= brackets[0][0])tax = income * rates[0];else if (income <= brackets[0][1])tax = brackets[0][0] * rates[0] +(income - brackets[0][0]) * rates[1];else if (income <= brackets[0][2])tax = brackets[0][0] * rates[0] +(brackets[0][1] - brackets[0][0]) * rates[1] +(income - brackets[0][1]) * rates[2];else if (income <= brackets[0][3])tax = brackets[0][0] * rates[0] +(brackets[0][1] - brackets[0][0]) * rates[1] +(brackets[0][2] - brackets[0][1]) * rates[2] +(income - brackets[0][2]) * rates[3];else if (income <= brackets[0][4])tax = brackets[0][0] * rates[0] +(brackets[0][1] - brackets[0][0]) * rates[1] +(brackets[0][2] - brackets[0][1]) * rates[2] +(brackets[0][3] - brackets[0][2]) * rates[3] +(income - brackets[0][3]) * rates[4];elsetax = brackets[0][0] * rates[0] +(brackets[0][1] - brackets[0][0]) * rates[1] +(brackets[0][2] - brackets[0][1]) * rates[2] +(brackets[0][3] - brackets[0][2]) * rates[3] +(brackets[0][4] - brackets[0][3]) * rates[3] +(income - brackets[0][4]) * rates[5];}if (status == 1) {if (income <= brackets[1][0])tax = income * rates[0];else if (income <= brackets[1][1])tax = brackets[1][0] * rates[0] +(income - brackets[1][0]) * rates[1];else if (income <= brackets[1][2])tax = brackets[1][0] * rates[0] +(brackets[1][1] - brackets[1][0]) * rates[1] +(income - brackets[1][1]) * rates[2];else if (income <= brackets[1][3])tax = brackets[1][0] * rates[0] +(brackets[1][1] - brackets[1][0]) * rates[1] +(brackets[1][2] - brackets[1][1]) * rates[2] +(income - brackets[1][2]) * rates[3];else if (income <= brackets[1][4])tax = brackets[1][0] * rates[0] +(brackets[1][1] - brackets[1][0]) * rates[1] +(brackets[1][2] - brackets[1][1]) * rates[2] +(brackets[1][3] - brackets[1][2]) * rates[3] +(income - brackets[1][3]) * rates[4];elsetax = brackets[1][0] * rates[0] +(brackets[1][1] - brackets[1][0]) * rates[1] +(brackets[1][2] - brackets[1][1]) * rates[2] +(brackets[1][3] - brackets[1][2]) * rates[3] +(brackets[1][4] - brackets[1][3]) * rates[3] +(income - brackets[1][4]) * rates[5];}if (status == 2) {if (income <= brackets[2][0])tax = income * rates[0];else if (income <= brackets[2][1])tax = brackets[2][0] * rates[0] +(income - brackets[2][0]) * rates[1];else if (income <= brackets[2][2])tax = brackets[2][0] * rates[0] +(brackets[2][1] - brackets[2][0]) * rates[1] +(income - brackets[2][1]) * rates[2];else if (income <= brackets[2][3])tax = brackets[2][0] * rates[0] +(brackets[2][1] - brackets[2][0]) * rates[1] +(brackets[2][2] - brackets[2][1]) * rates[2] +(income - brackets[2][2]) * rates[3];else if (income <= brackets[2][4])tax = brackets[2][0] * rates[0] +(brackets[2][1] - brackets[2][0]) * rates[1] +(brackets[2][2] - brackets[2][1]) * rates[2] +(brackets[2][3] - brackets[2][2]) * rates[3] +(income - brackets[2][3]) * rates[4];elsetax = brackets[2][0] * rates[0] +(brackets[2][1] - brackets[2][0]) * rates[1] +(brackets[2][2] - brackets[2][1]) * rates[2] +(brackets[2][3] - brackets[2][2]) * rates[3] +(brackets[2][4] - brackets[2][3]) * rates[3] +(income - brackets[2][4]) * rates[5];}if (status == 3) {if (income <= brackets[3][0])tax = income * rates[0];else if (income <= brackets[3][1])tax = brackets[3][0] * rates[0] +(income - brackets[3][0]) * rates[1];else if (income <= brackets[3][2])tax = brackets[3][0] * rates[0] +(brackets[3][1] - brackets[3][0]) * rates[1] +(income - brackets[3][1]) * rates[2];else if (income <= brackets[3][3])tax = brackets[3][0] * rates[0] +(brackets[3][1] - brackets[3][0]) * rates[1] +(brackets[3][2] - brackets[3][1]) * rates[2] +(income - brackets[3][2]) * rates[3];else if (income <= brackets[3][4])tax = brackets[3][0] * rates[0] +(brackets[3][1] - brackets[3][0]) * rates[1] +(brackets[3][2] - brackets[3][1]) * rates[2] +(brackets[3][3] - brackets[3][2]) * rates[3] +(income - brackets[3][3]) * rates[4];elsetax = brackets[3][0] * rates[0] +(brackets[3][1] - brackets[3][0]) * rates[1] +(brackets[3][2] - brackets[3][1]) * rates[2] +(brackets[3][3] - brackets[3][2]) * rates[3] +(brackets[3][4] - brackets[3][3]) * rates[3] +(income - brackets[3][4]) * rates[5];}return tax;}}7.13 import java.util.Scanner;public class Exercise13 {public static void main(String[] args) {//Create a ScannerScanner input = new Scanner(System.in);//Prompt the user to enter the number of rows and columns of the arraySystem.out.print("Enter the number of rows and columns of the array: ");int rows = input.nextInt();int columns = input.nextInt();//Prompt the user to enter the arraySystem.out.println("Enter the array:");double[][] array = new double[rows][columns];for (int i = 0; i < array.length; i++) {for (int j = 0; j < array[i].length; j++)array[i][j] = input.nextDouble();}//Display the location of the largest element in the arrayint[] location = findLocation(array);System.out.println("The location of the largest element is at (" + location[0] + ", " + location[1] + ")");}/*** Return the location of the largest element in the array*/public static int[] findLocation(double[][] array) {//Assume array[0][0] is the largest elementdouble largest = array[0][0];int[] location = {0, 0};//Repeatedly find the largest elementfor (int i = 0; i < array.length; i++) {for (int j = 0; j < array[i].length; j++) {if (array[i][j] > largest) {largest = array[i][j];location[0] = i;location[1] = j;}}}return location;}}7.14 import java.util.Scanner;public class Exercise14 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter the size of the matrix: ");int size = input.nextInt();int[][] matrix = new int[size][size];for (int i = 0; i < matrix.length; i++) {for (int j = 0; j < matrix[i].length; j++)matrix[i][j] = (int)(Math.random() * 2);}printMatrix(matrix);find0sAnd1s(matrix);}public static void printMatrix(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();}}public static void find0sAnd1s(int[][] array) {int i = 0, j= 0;int countRow = 0, countColumn = 0, countMajorDiagonal = 0;while (i < array.length && j < array.length) {int a = array[0][0];int b = array[i][j];for (int n = 0; n < array.length; n++) {if (array[i][n] != b)break;else {if (n == array.length - 1) {System.out.println("All " + b + "s on row " + i);countRow++;}}}for (int n = 0; n < array.length; n++) {if (array[n][j] != b)break;else {if (n == array.length - 1) {System.out.println("All " + b + "s on column " + j);countColumn++;}}}if (b != a)countMajorDiagonal++;i++;j++;}if (countRow == 0)System.out.println("No same numbers on a row");if (countColumn == 0)System.out.println("No same number on a column");if (countMajorDiagonal == 0)System.out.println("All " + array[0][0] + "s on the major diagonal");if (countMajorDiagonal > 0)System.out.println("No same numbers on the major diagonal");for (int p = 1, q = array.length - 2; p < array.length && q >= 0; p++, q--) {if (array[p][q] != array[0][array.length - 1]) {System.out.println("No same numbers on the sub-diagonal");break;}else {if (p == array.length - 1 && q == 0)System.out.println("All " + array[0][array.length - 1] + "s on the sub-diagonal");}}}}7.15 public class Exercise15 {public static void main(String[] args) {double[][] set1 = {{-1, -1}, {2, 2}, {3, 3}, {4, 4}};double[][] set2 = {{0, 1}, {1, 2}, {4, 5}, {5, 6}};double[][] set3 = {{0, 1}, {1, 2}, {4, 5}, {4.5, 4}};System.out.println("Is set1 on the same line? " + isOnSameLine(set1));。