Question

In: Computer Science

The two-dimensional arrays list1 and list2 are identical if they have the same contents. Write a...

The two-dimensional arrays list1 and list2 are identical if they have the same contents. Write a method that returns true if they are identical and false if they are not. Use the following header: public static boolean equals(int [][] list1, int [][] list2)

Write a test program that prompts the user to enter two 3 x 3 arrays of integers and displays whether the two are identical.

Enter list1:

Enter list2:

The two arrays are identical or The two arrays are not identical

Solutions

Expert Solution

import java.util.Scanner;

public class IdenticalArrays {

    public static boolean equals(int [][] list1, int [][] list2) {
        if (list1.length == list2.length) {
            for (int i = 0; i < list1.length; i++) {
                if (list1[i].length == list2[i].length) {
                    for (int j = 0; j < list1[i].length; j++) {
                        if (list1[i][j] != list2[i][j]) {
                            return false;
                        }
                    }
                } else {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[][] m1 = new int[3][3], m2 = new int[3][3];
        System.out.println("Enter first matrix: ");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                m1[i][j] = in.nextInt();
            }
        }
        System.out.println("Enter second matrix: ");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                m2[i][j] = in.nextInt();
            }
        }
        if (equals(m1, m2)) {
            System.out.println("Arrays are equal");
        } else {
            System.out.println("Arrays are not equal");
        }
    }
}

Related Solutions

By using Python code: 1. list1 = ['smith', 'john', 'andy'] list2 = ['kim', 'mary', 'paul'] Write...
By using Python code: 1. list1 = ['smith', 'john', 'andy'] list2 = ['kim', 'mary', 'paul'] Write a python program which does the following: a. add items in list2 to list1 b. Remove mary from list1 c. Insert sam at 5th position d. Sort the elements of list1 in ascending order 2. Write a python program that asks the user to enter a sentence and creates a list of words in that sentence. 3. friends = ('sam','andy','mary','andy','john','andy','mary') Write a python program...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
The 2 -dimensional arrays m1 and m2 are strictly identical if their corresponding elements are equal.  ...
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...
This is for c++ Write a program that works with two arrays of the same size...
This is for c++ Write a program that works with two arrays of the same size that are related to each other in some way (or parallel arrays). Your two arrays must be of different data types. For example, one array can hold values that are used in a formula that produces the contents of the second array. Some examples might be:  from a previous program, populations and the associated flowrates for those populations (an int array of populations...
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 2: Find the largest value of each row of a 2D array             (filename: FindLargestValues.java) Write a method with the following header to return an array of integer values which are the largest values from each row of a 2D array of integer values public static int[] largestValues(int[][] num) For example, if...
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 1: Find the average of an array of numbers (filename: FindAverage.java) Write two overloaded methods with the following headers to find the average of an array of integer values and an array of double values: public static double average(int[] num) public static double average(double[] num) In the main method, first create an...
Two firms have the same technology and pay the same wages for labor. They have identical...
Two firms have the same technology and pay the same wages for labor. They have identical factories, but firm 1 paid a higher price for its factory than firm 2 did. They both maximize their profits and have upward-sloping marginal cost curves. Explain whether our theory would predict firm 1 to have a higher output than firm 2.
Write a program that uses two identical arrays of at least 25 integers. It should call...
Write a program that uses two identical arrays of at least 25 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in descending order. The function should keep a count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other array. It should also keep count of the number of exchanges it makes. Display these values on...
In C++, write a program that uses two identical arrays of ten randomly ordered integers. It...
In C++, write a program that uses two identical arrays of ten randomly ordered integers. It should display the contents of the first array, then call a function to sort it using the most efficient descending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using descending order selection sort, modified to print out the...
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the...
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the union of elements in two lists. For example: list1 contains elements [1, 2, 3, 4, 5] list2 contains elements [3, 4, 5, 6, 7] Diff() method should return an array list with elements [1, 2, 3, 4, 5, 6, 7].
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT