Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Two arrays are identical arrays if they are the same size and have the same elem

ID: 653338 • 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 of integers, 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:

Explanation / Answer

import java.util.Arrays;

import java.util.Scanner;

public class IdenticalArrays {

@SuppressWarnings("resource")

   public static void main(String[] args) {

       Scanner input = new Scanner(System.in);

       while (true) {

           System.out.println("Enter your first 3-by-3 matrix row by row:");

           int array1[][] = IdenticalArrays.inputArray();

           System.out.println("Enter your second 3-by-3 matrix row by row:");

           int array2[][] = IdenticalArrays.inputArray();

           if (IdenticalArrays.equals(array1, array2))

               System.out.println(" The two arrays are identical");

           else

               System.out.println("The two list are not identical");

           System.out.println("Would you like to go again? ");

           String decision = input.next();

           if (decision.equals("Yes"))

               continue;

           else

               break;

       }

   }

   public static boolean equals(int[][] m1, int[][] m2) {

       return Arrays.deepEquals(m1, m2);

   }

   public static int[][] inputArray() {

       @SuppressWarnings("resource")

       Scanner input = new Scanner(System.in);

       int[][] array = new int[3][3];

       for (int i = 0; i < 3; i++) {

           for (int j = 0; j < 3; j++)

               array[i][j] = input.nextInt();

       }

       return array;

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote