In: Computer Science
The 2 -dimensional arrays m1 and m2 are strictly identical if their corresponding elements are equal.
Write the method "equals" which returns True if the arrays are strictly identical and False if they are not.
The method's header is: public static boolean equals(int[][] m1, int[][] m2)
Here is the program:
import java.util.Scanner;
public class StrictlyEquals {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int ROW_SIZE = 3;
final int COLUMN_SIZE = 3;
System.out.print("Enter m1 (a 3 by 3 matrix) row by row: ");
int[][] m1 = new int[ROW_SIZE][COLUMN_SIZE];
for (int i = 0; i < m1.length; i++)
for (int j = 0; j <
m1[0].length; j++)
m1[i][j] =
input.nextInt();
System.out.print("Enter m2 (a 3 by 3 matrix) row by row:
");
int[][] m2 = new int[ROW_SIZE][COLUMN_SIZE];
for (int i = 0; i < m2.length; i++)
for (int j = 0; j < m2[0].length; j++)
m2[i][j] =
input.nextInt();
if (equals(m1, m2))
System.out.println("The two arrays are strictly
identical");
else
System.out.println("The two arrays are not
strictly identical");
}
public static boolean equals(int[][] m1, int[][] m2) {
/* YOUR CODE GOES HERE*/
}
}
import java.util.Scanner;
public class StrictlyEquals {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int ROW_SIZE = 3;
final int COLUMN_SIZE = 3;
System.out.print("Enter m1 (a 3 by 3 matrix) row by row: ");
int[][] m1 = new int[ROW_SIZE][COLUMN_SIZE];
for (int i = 0; i < m1.length; i++)
for (int j = 0; j < m1[0].length; j++)
m1[i][j] = input.nextInt();
System.out.print("Enter m2 (a 3 by 3 matrix) row by row: ");
int[][] m2 = new int[ROW_SIZE][COLUMN_SIZE];
for (int i = 0; i < m2.length; i++)
for (int j = 0; j < m2[0].length; j++)
m2[i][j] = input.nextInt();
if (equals(m1, m2))
System.out.println("The two arrays are strictly identical");
else
System.out.println("The two arrays are not strictly identical");
}
public static boolean equals(int[][] m1, int[][] m2) {
if(m1.length!=m2.length)
return false;
for(int i=0;i<m1.length;i++) {
if(m1[i].length!=m2[i].length)
return false;
for(int j=0;j<m1[i].length;j++)
if(m1[i][j]!=m2[i][j])
return false;
}
return true;
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME