Write a class named Array2DAlgorithms. In the class write: an equals method that
ID: 3604681 • Letter: W
Question
Write a class named Array2DAlgorithms.
In the class write:
an equals method that accepts two 2D integer arrays as parameters and returns true if the arrays contain the same elements in the same order.
If the arrays are not the same length in either dimension, your method should return false.
a printArray method that takes a 2D integer array as a parameter and prints each row of the array on a separate line. Each row should be contained in brackets with each element separated by a comma followed by a space.
a method called matrixAdd (Textbook Ex. 7-19) that accepts a pair of two-dimentional arrays of integers as parameters, treats the arrays as two-dimensional matrices, and returns their sum. The sum of two matrices A and B is a matrix C, where for every row i and column j, C[i][j] = A[i][j] + B[i][j]. You may assume that the arrays passed as parameters have the same dimensions.
Do NOT use the Arrays class when writing your methods.
In your main method, create two 3x4 2D integer arrays, both with the following elements:
row 1: 1, 2, 3 ,4
row 2: 5, 6, 7, 8
row 3: 9, 10, 11, 12
Call your equals method using these arrays as parameters and print the result with meaningful text.
Call the matrixAdd method using these arrays as parameters and print the result using your printArray method.
The output from your program should be similar to the following:
Arrays:
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]
and
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]
are equal.
and their sum is:
[2, 4, 6, 8]
[10, 12, 14, 16]
[18, 20, 22, 24]
You may not use Arrays.equals or Arrays.deepEquals.
Explanation / Answer
Array2DAlgorithms.java
package org.students;
public class Array2DAlgorithms {
public static void main(String[] args) {
//Declaring two arrays
int A[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
int B[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
//Displaying the arrays and compare two arrays
System.out.println("Arrays:");
printArray(A);
System.out.println("and");
printArray(B);
boolean bool=equals(A,B);
if(bool)
System.out.println("are equal.");
else
System.out.println("are not equal.");
System.out.println("and their sum is:");
//Adding two arrays
int C[][]=matrixAdd(A,B);
printArray(C);
}
//This method will add two arrays
private static int[][] matrixAdd(int[][] a, int[][] b) {
int res[][]=new int[a.length][a[0].length];
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[0].length;j++)
{
res[i][j]=a[i][j]+b[i][j];
}
}
return res;
}
//This method will display the array
private static void printArray(int[][] a) {
for(int i=0;i<a.length;i++)
{
System.out.print("[");
for(int j=0;j<a[0].length;j++)
{
System.out.print(a[i][j]);
if(j!=a[0].length-1)
{
System.out.print(",");
}
}
System.out.print("]");
System.out.println();
}
}
private static boolean equals(int[][] a, int[][] b) {
if(a.length!=b.length || a[0].length!=b[0].length)
{
return false;
}
else
{
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[0].length;j++)
{
if(a[i][j]!=b[i][j])
return false;
}
}
}
return true;
}
}
_________________
Output:
Arrays:
[1,2,3,4]
[5,6,7,8]
[9,10,11,12]
and
[1,2,3,4]
[5,6,7,8]
[9,10,11,12]
are equal.
and their sum is:
[2,4,6,8]
[10,12,14,16]
[18,20,22,24]
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.