Problem #2 (15 points): Replicating 2D arrays 1. Create a Java project named Fir
ID: 3702991 • Letter: P
Question
Problem #2 (15 points): Replicating 2D arrays
1. Create a Java project named FirstName-LastName-HW7 . a. Add a new package named hw7p2 to the project.
Right click the package and add a new class named Clone without a main method.
Add another class named CloneTester with a main method.
2. Write
a. The first method copies a two-dimensional array which is square (same number of rows
code for the Clone.java with three methods. and columns). Use the following array:
(attached in photo)
The second method copies a two-dimensional array which is rectangular (diffrent number of rows and columns). This method must work for 2D square array, too. Use the following array:
The third method copies a two-dimensional array which is ragged. This method must work for both 2D square and rectangular arrays. Use the following array:
(attached in photo)
3. Write
a. Test each method.
code for the CloneTester.java.
b. Test if the second method works for a 2D square array.
c. Test if the third method works for both 2D square and rectangular arrays.
ACRONYMS:
ID ay17.moodle.umn.edu New Youth Beware ? PRG420 Course: F https:lay17 Chega 1 2 3 45 6 7 89 10 11 12 13 1415 16 17 18 19 20 21 22 23 24 25 262728 29 30 31 32 33 34 35 36 b. The second method copies a two-dimensional array which is rectangular (diffrent number of rows and columns). This method must work for 2D square array, too. Use the following array 1 23 4 5 6 78 9 10 11 12 13 1415 16 17 18 19 20 21 22 23 24 25 262728 29 30 The third method copies a two-dimensional array which is ragged. This method must work for both 2D square and rectangular arrays. Use the following array: c. 1 23 4 5 6 7 8 9 10 11 12 13 14 15 16 19 20 21222324 25 26 27 28 29Explanation / Answer
package test ;
import java.util.*;
public class MyClass {
public static void main(String args[]){
CloneTester ct = new CloneTester();
ct.testFirstMethod();
ct.testSecondMethod();
ct.testThirdMethod();
}
}
class Clone {
public int[][] firstMethod(int arr[][]){
int n = arr.length; //n stores number of rows or columns
int copiedArray[][]=new int[n][n];
for(int r=0;r<n;r++){
for(int c=0;c<n;c++){
copiedArray[r][c] = arr[r][c];
}
}
return copiedArray;
}
public int[][] secondMethod(int arr[][]){
int row = arr.length;
int columns = arr[0].length;
int copiedArray[][]=new int[row][columns];
for(int r=0;r<row;r++){
for(int c=0;c<columns;c++){
copiedArray[r][c] = arr[r][c];
}
}
return copiedArray;
}
public int[][] thirdMethod(int arr[][]){
int row = arr.length;
int copiedArray[][] = new int[row][];
for(int r=0;r<row;r++){
int columns = arr[r].length;
copiedArray[r] = new int[columns];
for(int c=0;c<columns;c++){
copiedArray[r][c] = arr[r][c];
}
}
return copiedArray;
}
}
class CloneTester{
int arr1[][]={{1,2,3,4,5,6},{7,8,9,10,11,12},{13,14,15,16,17,18},{19,20,21,22,23,24},{25,26,27,28,29,30},{31,32,33,34,35,36}};
int arr2[][]={{1,2,3,4,5,6},{7,8,9,10,11,12},{13,14,15,16,17,18},{19,20,21,22,23,24},{25,26,27,28,29,30}};
int arr3[][]={{1,2,3,4,5,6},{7,8,9,10},{11,12,13,14,15,16},{19,20},{21,22,23,24,25,26},{27,28,29}};
public void printArray(int arr[][]){
int row = arr.length;
for(int r=0;r<row;r++){
int columns = arr[r].length;
for(int c=0;c<columns;c++){
System.out.print(arr[r][c]+" ");
}
System.out.println();
}
}
public boolean equal(final int[][] arr1, final int[][] arr2) { //function to check if two 2 d arrays are equal
if (arr1 == null) {
return (arr2 == null);
}
if (arr2 == null) {
return false;
}
if (arr1.length != arr2.length) {
return false;
}
for (int i = 0; i < arr1.length; i++) {
if (!Arrays.equals(arr1[i], arr2[i])) {
return false;
}
}
return true;
}
public void testFirstMethod(){
System.out.println("Testing first method:");
Clone clone=new Clone();
System.out.println("Input Array:");
printArray(arr1);
int [][] copiedArr = clone.firstMethod(arr1);
System.out.println("Copied Array:");
printArray(copiedArr);
if(equal(arr1,copiedArr)){
System.out.println("Passed");
}
else{
System.out.println("Failed");
}
}
public void testSecondMethod(){
System.out.println("Testing second method:");
Clone clone=new Clone();
int [][] copiedArr;
System.out.println("Input Array:");
printArray(arr1);
copiedArr = clone.secondMethod(arr1);
System.out.println("Copied Array:");
printArray(copiedArr);
if(equal(arr1,copiedArr)){
System.out.println("Passed");
}
else{
System.out.println("Failed");
}
System.out.println("Input Array:");
printArray(arr2);
copiedArr = clone.secondMethod(arr2);
System.out.println("Copied Array:");
printArray(copiedArr);
if(equal(arr2,copiedArr)){
System.out.println("Passed");
}
else{
System.out.println("Failed");
}
}
public void testThirdMethod(){
System.out.println("Testing third method:");
Clone clone=new Clone();
int [][] copiedArr;
System.out.println("Input Array:");
printArray(arr1);
copiedArr = clone.thirdMethod(arr1);
System.out.println("Copied Array:");
printArray(copiedArr);
if(equal(arr1,copiedArr)){
System.out.println("Passed");
}
else{
System.out.println("Failed");
}
System.out.println("Input Array:");
printArray(arr2);
copiedArr = clone.thirdMethod(arr2);
System.out.println("Copied Array:");
printArray(copiedArr);
if(equal(arr2,copiedArr)){
System.out.println("Passed");
}
else{
System.out.println("Failed");
}
System.out.println("Input Array:");
printArray(arr3);
copiedArr = clone.thirdMethod(arr3);
System.out.println("Copied Array:");
printArray(copiedArr);
if(equal(arr3,copiedArr)){
System.out.println("Passed");
}
else{
System.out.println("Failed");
}
}
}
Sample Output:
Testing first method:
Input Array:
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36
Copied Array:
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36
Passed
Testing second method:
Input Array:
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36
Copied Array:
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36
Passed
Input Array:
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
Copied Array:
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
Passed
Testing third method:
Input Array:
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36
Copied Array:
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36
Passed
Input Array:
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
Copied Array:
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
Passed
Input Array:
1 2 3 4 5 6
7 8 9 10
11 12 13 14 15 16
19 20
21 22 23 24 25 26
27 28 29
Copied Array:
1 2 3 4 5 6
7 8 9 10
11 12 13 14 15 16
19 20
21 22 23 24 25 26
27 28 29
Passed
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.