Two arrays are identical arrays if they are the same size and have the same elem
ID: 646590 • Letter: T
Question
Two arrays are identical arrays if they are the same size and have the same elements, although the elements do not have to be in the same positions. Write a program called IdenticalArrays that asks the user to enter in two 3x3 arrays ofintegers, and then your program will display whether the two arrays are identical.
To accomplish this, your main() method will get all of the values (as integers) and place them into the two arrays, and then pass the arrays to a method you must write called equals(), which will return true if the elements are identical, and false if not. Your equals() method will have the following signature:
Design the main() method of your program such that it allows the user to re-run the program with different inputs (i.e., use a loop structure).
Here is a sample output:
This is what I have so far. It doesnt work with the given number. It always says it is identical. This is java btw
import java.util.*;
public class IdenticalArrays
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String answer = "";
do
{
System.out.println(" Enter your first 3-by-3 matrix row by row: ");
int[][] m1 = new int[3][3];
for (int i = 0; i < 3; ++i){
for (int j = 0; j < 3; ++j) {
m1 [i][j] = scan.nextInt();
}
}
System.out.println();
System.out.println("Enter your second 3-by-3 matrix row by row: ");
int[][] m2 = new int[3][3];
for (int i = 0; i < 3; ++i){
for (int j = 0; j < 3; ++j) {
m2 [i][j] = scan.nextInt();
}
}
if (equals(m1, m2))
System.out.print(" The two arrays are identical");
else
System.out.print(" The two arrays are not identical");
System.out.println();
System.out.print(" Would you like to go again? ");
answer = scan.next();
scan.nextLine();
} while (answer.equalsIgnoreCase("yes"));
}
// Determines if two 2D arrays are equal, meaning they have the same contents even
//in not in the same positions.
public static boolean equals(int[][] m1, int[][] m2)
{
boolean answer = true;
if (m1.length * m1[0].length != m2.length * m2[0].length){
answer = false;
return answer;
}
for(int row = 0; row < m1.length; ++row) {
if(m1 [row].length != m2 [row].length){
answer = false;
return answer;
}
for(int i = 0; i < m1.length; i++)
for (int j = 0; j < m1[0].length; j++)
if (m1[i][j] != m2[i][j]){
answer = true;
return answer;
}
}
return answer;
}
}
Explanation / Answer
import java.util.*;
public class IdenticalArrays
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String answer = "";
do
{
System.out.println(" Enter your first 3-by-3 matrix row by row: ");
int[][] m1 = new int[3][3];
for (int i = 0; i < 3; ++i){
for (int j = 0; j < 3; ++j) {
m1 [i][j] = scan.nextInt();
}
}
System.out.println();
System.out.println("Enter your second 3-by-3 matrix row by row: ");
int[][] m2 = new int[3][3];
for (int i = 0; i < 3; ++i){
for (int j = 0; j < 3; ++j) {
m2 [i][j] = scan.nextInt();
}
}
if (equals(m1, m2))
System.out.print(" The two arrays are identical");
else
System.out.print(" The two arrays are not identical");
System.out.println();
System.out.print(" Would you like to go again? ");
answer = scan.next();
scan.nextLine();
} while (answer.equalsIgnoreCase("yes"));
}
// Determines if two 2D arrays are equal, meaning they have the same contents even
//in not in the same positions.
public static boolean equals(int[][] m1, int[][] m2)
{
boolean answer=false;
if (m1.length * m1[0].length != m2.length * m2[0].length){
answer = false;
return answer;
}
for(int row = 0; row < m1.length; ++row) {
if(m1 [row].length != m2 [row].length){
answer = false;
return answer;
}
for(int i = 0; i < m1.length; i++)
for (int j = 0; j < m1[0].length; j++)
if (m1[i][j] == m2[i][j]){
answer = true;
return answer;
}
}
return answer;
}
}
Output:
Enter your first 3-by-3 matrix row by row:
1 1 1
1 1 1
1 1 1
Enter your second 3-by-3 matrix row by row:
1 1 1
1 1 1
1 1 1
The two arrays are identical
Would you like to go again? yes
Enter your first 3-by-3 matrix row by row:
1 1 1
1 1 1
1 1 1
Enter your second 3-by-3 matrix row by row:
2 2 2
2 2 2
2 2 2
The two arrays are not identical
Would you like to go again? yes
Enter your first 3-by-3 matrix row by row:
1 2 3
4 5 6
7 8 9
Enter your second 3-by-3 matrix row by row:
9 8 7
6 5 4
3 2 1
The two arrays are identical
Would you like to go again? yes
Enter your first 3-by-3 matrix row by row:
23 25 14
11 15 9
66 56 42
Enter your second 3-by-3 matrix row by row:
42 56 66
9 15 11
14 23 25
The two arrays are identical
Would you like to go again?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.