(Strictly identical arrays) The two-dimensional arrays m1 and m2 are strictly id
ID: 3669758 • Letter: #
Question
(Strictly identical arrays) The two-dimensional arrays m1 and m2 are strictly identical if their corresponding elements are equal. Write a method that returns true if m1 and m2 are strictly identical, using the following header: public static boolean equals(int[][] m1, int[][] m2) Write a test program that prompts the user to enter two arrays of integers and displays whether the two are strictly identical. Here are the sample runs. 1. Write a class named Strict that has the method equals as specified in the book on page 316 (ALL CORRESPONDING ELEMENTS ARE THE SAME.) 2. The class named Strict should also have a method public static int howmany(int[][] m1, int[][] m2) that returns how many cell values are identical in the two arrays (it only counts if they are identical in the same cell.) 3. The class named Strict should also have a method public static int diagonal(int[][] m1, int[][] m2) that returns how many cell values are identical along the diagonal (that is, checking only cells [0][0], [1][1], and [2][2] from the two arrays.) 4. The class named Strict should also have a method public static double average(int[][] m1,int[][] m2)that returns the average of all the cell values from the arrays (one answer.) 5. The class named Strict should also have a method public static void display(int[][] m1, int[][] m2) that displays only those values of the arrays that are odd in rectangular form (row by row for each array.) You may assume the values entered are between 0 and 99 for formatting purposes. Make it pretty! 6. The class named Strict should also have a method public static boolean silly(int[][] m1, int[][] m2) that returns true if the two arrays have all numbers satisfying 1 < numbers <=10 and returns false otherwise. Write a public class named your n number (lowercase n) that permits the user to provide sample input like in the textbook and then displays the results of equals, howmany, diagonal , average (2 digits after the decimal point), display, and silly. Skip one line after the answer for each method is printed. DO NOT ASK FOR 18 PROMPTS. ENTER ALL THE NUMBERS AT ONCE AND USE NESTED FOR LOOPS TO PROCESS. Both classes should be in the same file. in java
Explanation / Answer
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
int[][] matrix1 = new int[3][3];
int[][] matrix2 = new int[3][3];
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter list 1: ");
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[i].length; j++) {
matrix1[i][j] = keyboard.nextInt();
}
}
System.out.print("Enter list 2: ");
for (int i = 0; i < matrix2.length; i++) {
for (int j = 0; j < matrix2[i].length; j++) {
matrix2[i][j] = keyboard.nextInt();
}
}
if (Strict.equals(matrix1, matrix2))
System.out.println(" Matrices are equal. ");
else
System.out.println(" Matrices are not equal. ");
System.out.println("There are " + Strict.howmany(matrix1, matrix2) + " matching cells. ");
System.out.println("There are " + Strict.diagonal(matrix1, matrix2) + " matching diagonal cells. ");
System.out.print("The average of all of the values in both matrices is ");
System.out.format("%.2f", Strict.average(matrix1, matrix2));
System.out.println(". ");
Strict.display(matrix1, matrix2);
if (Strict.silly(matrix1, matrix2))
System.out.println(" The matrices are silly. ");
else
System.out.println(" The matrices are not silly. ");
}
}
class Strict {
public static boolean equals(int[][] m1, int[][] m2) {
boolean matricesAreEqual = true;
// If the matrices are different lengths, they aren't equal
if (m1.length == m2.length) {
// I never actually tried using two conditions in a for loop. It apparantly works!
// Loop will stop if it finds a pair that aren't equal.
for (int i = 0; (i < m1.length && matricesAreEqual); i++) {
if (m1[i].length == m2[i].length) {
for (int j = 0; j < m1[i].length; j++) {
if (m1[i][j] != m2[i][j]) {
matricesAreEqual = false;
break;
}
}
}
}
}
else
matricesAreEqual = false;
return matricesAreEqual;
}
public static int howmany(int[][] m1, int[][] m2) {
int matchingCount = 0;
for (int i = 0; i < m1.length; i++) {
for (int j = 0; j < m1[i].length; j++) {
if (m1[i][j] == m2[i][j])
matchingCount++;
}
}
return matchingCount;
}
public static int diagonal(int[][] m1, int[][] m2) {
int matchingCount = 0;
for (int i = 0; (i < m1.length && i < m1[i].length); i++) {
if (m1[i][i] == m2[i][i])
matchingCount++;
}
return matchingCount;
}
public static double average(int[][] m1, int[][] m2) {
int valueCount = m1.length + m2.length;
int valueTotal = 0;
for (int i = 0; i < m1.length; i++) {
for (int j = 0; j < m1.length; j++) {
valueTotal += m1[i][j];
valueTotal += m2[i][j];
}
}
return ((double)valueTotal / (double)valueCount);
}
public static void display(int[][] m1, int[][] m2) {
// Begin special formatting voodoo
System.out.println("Matrix 1 odds:");
System.out.print("[ [");
for (int i = 0; i < m1.length; i++) {
for (int j = 0; j < m1[i].length; j++) {
if (m1[i][j] % 2 != 0)
System.out.format("%02d", m1[i][j]);
else
System.out.print("--");
if (j != m1[i].length - 1)
System.out.print(" ");
}
if (i == m1.length - 1) {
System.out.println("] ]");
}
else {
System.out.println("]");
System.out.print(" [");
}
}
System.out.println(" Matrix 2 odds:");
System.out.print("[ [");
for (int i = 0; i < m2.length; i++) {
for (int j = 0; j < m2[i].length; j++) {
if (m2[i][j] % 2 != 0)
System.out.format("%02d", m2[i][j]);
else
System.out.print("--");
if (j != m2[i].length - 1)
System.out.print(" ");
}
if (i == m2.length - 1) {
System.out.println("] ]");
}
else {
System.out.println("]");
System.out.print(" [");
}
}
}
public static boolean silly(int[][] m1, int[][] m2) {
boolean areArraysSilly = true;
for (int i = 0; i < m1.length; i++) {
for (int j = 0; j < m1[i].length; j++) {
if (m1[i][j] <= 1 || m1[i][j] > 10 || m2[i][j] <= 1 || m2[i][j] > 10)
areArraysSilly = false;
}
}
return areArraysSilly;
}
}
output
Enter list 1: 4 5
6 7
8 9
1 1
2 3
Enter list 2: 6 7
8 9
5 6
7 6
Matrices are not equal.
There are 0 matching cells.
There are 0 matching diagonal cells.
Matrix 1 odds:
[ [-- 05 --]
[07 -- 09]
[01 01 --] ]
Matrix 2 odds:
[ [03 -- 07]
[-- 09 05]
[-- 07 --] ]
The matrices are not silly.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.