Question

In: Computer Science

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 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*/
}


}

Solutions

Expert Solution


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


Related Solutions

A collision occurs between two equal masses m1 and m2. Before the collision m2 is stationary....
A collision occurs between two equal masses m1 and m2. Before the collision m2 is stationary. After the collision both masses are moving differently. After the collision the position of the center of mass and motion of the center of mass respectively are best described as. ANSWER CHOICE A) halfway between the two masses, and stationary B) halfway between the two masses and moving C) halfway between the two masses and moving with the speed of mass m1 D) centered...
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...
Three machines M1, M2 and M3 produce nominally identical items. The evidence of the engineers past...
Three machines M1, M2 and M3 produce nominally identical items. The evidence of the engineers past experience is that 5 % of the output from machine M1 is faulty, 3.5 % of the output from machine M2 is faulty and 2.5 % of the output from machine M3 is faulty. On a given day, M1 has produced 15 % of the total output, M2 has produced 30 % and M3 the remainder. An item selected at random is found to...
Two particles of equal masses m1=m2 move on a frictionless horizontal surface in the vicinity of...
Two particles of equal masses m1=m2 move on a frictionless horizontal surface in the vicinity of a fixed force center, with potential energies U1 = 1/2kr^(2)1 and U2 = 1/2kr^(2)2. In addition they interact with each other via a potential energy U12 = 1/2αkr^2 where r is the distance between them and α and k are positive constants. (a) Find the Lagrangian in terms of the CM position R and the relative position r. (b) Write down and solve the...
For the arrays x and y given below, use MATLAB to find all the elements in x that are greater than the corresponding elements in y.
For the arrays x and y given below, use MATLAB to find all the elements in x that are greater than the corresponding elements in y.x = [-3, 0, 0, 2, 6, 8] y = [-5, -2, 0, 3, 4, 10]
Block 1 with m1 = 0.127 kg and block 2 with m2 = 0.163 kg are...
Block 1 with m1 = 0.127 kg and block 2 with m2 = 0.163 kg are supported on a horizontal frictionless table whose surface is 1.75 m above a horizontal floor as shown in the Figure. Block 1 has an initial speed of v = 5.50 m/s toward block 2 which is initially at rest. A) (7 pts) Block 1 collides with block 2 and coalesces (forms one object). Calculate the velocity of the coalesced object. B) (10 pts) The...
1) Define MB, M1, M2 and M3. 2) What is the reason for using these different...
1) Define MB, M1, M2 and M3. 2) What is the reason for using these different measures of money supply? 3) Is there a measure of money supply that is NOT affected by the banking multiplier, i.e. that is determined solely by the actions of the Central Bank? 4) Which nominal interest rate is controlled by the Central Bank? How?
Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we...
Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we will call our visible field, needs to be 5 by 5 and should initially hold the underscore character “_”.  This will be used in our game of minesweeper to represent the possible locations the player can check. The second array, which we will call our hidden field, should also be 5 by 5 but filled with the capital letter "S” which means safety. However, we...
A utility maximizing saver has u(f1, f2) = f11/2f21/2 and earns m1 = 90, m2 =...
A utility maximizing saver has u(f1, f2) = f11/2f21/2 and earns m1 = 90, m2 = 90. She can save at an interest rate of 25 percent. If she hires an investment advisor she can save at an interest rate of 80 percent. What is the most that she would pay in fees to this advisor?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT