In Java, write a program that prompts the user to enter a length of a square mat
ID: 674562 • Letter: I
Question
In Java, write a program that prompts the user to enter a 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. Use the template provided below.
Here is what I have but the program keeps reprinting lines:
import java.util.*;
public class ExploringMatrix {
public static void main(String[] args)
{
System.out.print("Enter the size of the matrix: ");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[][] matrix = randomZeroOneMatrix(n);
printTwoDArray(matrix);
rowChecks(matrix);
columnChecks(matrix);
checkMajorDiagonal(matrix);
checkSubDiagonal(matrix);
}
public static void rowChecks(int[][] array)
// Check which rows have all of the same entries.
// Display a message for each such row.
// Display a different message if none of the rows
// have all the same entries.
// Precondition: none of the entries are -1.
{
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.");
}
}
}
public static void columnChecks(int[][] matrix)
// Check which columns have all of the same entries.
// Display a message for each such column.
// Display a different message if none of the columns
// have all the same entries.
// Precondition: none of the entries are -1.
{
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.");
}
}
}
public static void printTwoDArray(int[][] array)
// Display the array values.
{
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 int[][] randomZeroOneMatrix(int n)
// Generate an nxn matrix randomly filled
// with zeroes and ones.
{
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;
}
public static int checkRowForSameness(int[][] matrix, int i)
// If all values in row i are the same, then return that value.
// Otherwise, return -1.
{
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;
}
public static int checkColumnForSameness(int[][] matrix, int j)
// If all values in col j are the same, then return that value.
// Otherwise, return -1.
{
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)
// Check whether all values along the major diagonal
// of the matrix are the same.
// Print a message accordingly.
{
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 diagonal");
else if (diagCounter1 == matrix.length)
System.out.println("All " + 1 + "'s on major diagonal");
else
System.out.println("Not same numbers on major diagonal.");
}
public static void checkSubDiagonal(int[][] matrix)
// Check whether all values along the sub-diagonal
// of the matrix are the same.
// Print a message accordingly.
{
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");
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
Here is the template for the java file that was provided:
import java.util.*;
public class ExploreMatrix
{
public static void main(String[] args)
{
System.out.print("Enter the size of the matrix: ");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.close();
int[][] matrix = randomZeroOneMatrix(n);
printTwoDArray(matrix);
rowChecks(matrix);
columnChecks(matrix);
checkMajorDiagonal(matrix);
checkSubDiagonal(matrix);
}
public static void rowChecks(int[][] matrix)
// Check which rows have all of the same entries.
// Display a message for each such row.
// Display a different message if none of the rows
// have all the same entries.
// Precondition: none of the entries are -1.
{
System.out.println("rowChecks stub");
}
public static void columnChecks(int[][] matrix)
// Check which columns have all of the same entries.
// Display a message for each such column.
// Display a different message if none of the columns
// have all the same entries.
// Precondition: none of the entries are -1.
{
System.out.println("columnChecks stub");
}
public static void printTwoDArray(int[][] array)
// Display the array values.
{
System.out.println("printTwoDArray stub");
}
public static int[][] randomZeroOneMatrix(int n)
// Generate an nxn matrix randomly filled
// with zeroes and ones.
{
return new int[n][n]; // stub
}
public static int checkRowForSameness(int[][] matrix, int i)
// If all values in row i are the same, then return that value.
// Otherwise, return -1.
{
return -1; // stub
}
public static int checkColumnForSameness(int[][] matrix, int j)
// If all values in col j are the same, then return that value.
// Otherwise, return -1.
{
return -1; // stub
}
public static void checkMajorDiagonal(int[][] matrix)
// Check whether all values along the major diagonal
// of the matrix are the same.
// Print a message accordingly.
{
System.out.println("checkMajorDiagonal stub");
}
public static void checkSubDiagonal(int[][] matrix)
// Check whether all values along the sub-diagonal
// of the matrix are the same.
// Print a message accordingly.
{
System.out.println("checkMajorDiagonal stub");
}
}
Explanation / Answer
import java.util.Scanner; public class ExploringMatrix { public static void main (String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter the size for the matrix: "); int size = scan.nextInt(); int[][] m = new int[size][size]; for (int i =0;iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.