1. Create a Java project named FirstName-LastName-HW7 (e.g., Seung-Lee-HW7). a.
ID: 3706242 • Letter: 1
Question
1. Create a Java project named FirstName-LastName-HW7 (e.g., Seung-Lee-HW7).
a. Add a new package named hw7p2 to the project.
b. Right click the package and add a new class named Clone without a main method.
c. Add another class named CloneTester with a main method.
2. Write code for the Clone.java with three methods.
a. The first method copies a two-dimensional array which is square (same number of rows and columns). Use the following array:
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:
C. 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:
3. Write code for the CloneTester.java.
a. Test each method.
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.
7 8 9 10 11 12 13 14 15 16 17 18 19 202122 23 24 25 26 27 28 29 30 31 32 33 34 35 36Explanation / Answer
package hw7p2;
public class Clone {
// this method takes 2_D array as argumemt and return its clone
public static int[][] cloneSquareArray(int[][] squareArray) {
int length = squareArray.length; // length of the array
int[][] result = new int[length][length]; // Declaration of 2D Array
for (int row = 0; row < length; row++) {
// Copy Each row from array to result array
System.arraycopy(squareArray[row], 0, result[row], 0, squareArray[row].length);
}
return result;
}
// this method takes 2_D Rectangular array as argument and return its clone
public static int[][] cloneRectangularArray(int[][] rectangularArray) {
int length = rectangularArray.length; // length of the array
int[][] result = new int[length][rectangularArray[0].length]; // Declaration of 2D Array
for (int row = 0; row < length; row++) {
// Copy Each row from array to result array
System.arraycopy(rectangularArray[row], 0, result[row], 0, rectangularArray[row].length);
}
return result;
}
// this method takes 2_D Rectangular array as argument and return its clone
public static int[][] cloneRaggedArray(int[][] raggedArray) {
int length = raggedArray.length; // length of the array
int[][] result = new int[length][]; // Declaration of 2D Array , Column is left because it may be different for
// each row
for (int i = 0; i < length; i++) {
int[] row = raggedArray[i];
result[i] = new int[row.length];
System.arraycopy(row, 0, result[i], 0, row.length);
}
return result;
}
}
package hw7p2;
public class CloneHelper {
public static void main(String[] args) {
// create Square Array
int squareArray[][] = new int[6][6];
int k = 1;
for (int i = 0; i <= squareArray.length - 1; i++) {
for (int j = 0; j <= squareArray[i].length - 1; j++) {
squareArray[i][j] = k++;
}
}
System.out.println(" Square Array");
// Print the array
printArray(squareArray);
// Calling clone methods
int[][] a = Clone.cloneSquareArray(squareArray);
System.out.println("Cloned Square Array");
// Print the array
printArray(a);
// create Rectangular Array
int rectArray[][] = new int[5][6];
int l = 1;
for (int i = 0; i <= rectArray.length - 1; i++) {
for (int j = 0; j <= rectArray[i].length - 1; j++) {
rectArray[i][j] = l++;
}
}
System.out.println("Cloned Rectangul Array");
// Print the array
printArray(rectArray);
// Calling clone method
int[][] b = Clone.cloneRectangularArray(rectArray);
System.out.println("Cloned Rectangular Array");
// Print the array
printArray(b);
// create Ragged Array
int[][] raggedArray = new int[6][];
// Initialize the elements
raggedArray[0] = new int[] { 1, 2, 3, 4, 5, 6 };
raggedArray[1] = new int[] { 7, 8, 9, 10 };
raggedArray[2] = new int[] { 11, 12, 13, 14, 15, 16 };
raggedArray[3] = new int[] { 19, 20 };
raggedArray[4] = new int[] { 21, 22, 23, 24, 25, 26 };
raggedArray[5] = new int[] { 27, 28, 29 };
System.out.println("Ragged Array");
// Print the array
printArray(raggedArray);
// Calling clone method
int[][] c = Clone.cloneRaggedArray(raggedArray);
System.out.println("Cloned Ragged Array");
// Print the array
printArray(c);
}
public static void printArray(int[][] array) {
// print the array (using for each loop)
for (int[] is : array) {
for (int i : is) {
System.out.print(" " + i + " ");
}
System.out.println();
}
}
}
output
Square 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
Cloned Square 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
Cloned Rectangul 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
Cloned Rectangular 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
Ragged 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
Cloned Ragged 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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.