Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write the Java Code that will execute the array expansion in figure 2.5 b

ID: 646897 • Letter: P

Question

Please write the Java Code that will execute the array expansion in figure 2.5 below. Note: This question does not require the use of the problem solving process. However, your solution should output the values of the array after each step of the expansion process.

Example Output:

After (a) new array of ten integers

arr ==> 2, 3, 4, 5, 6, 9, 8, 7, 1, 8

After (b) step 1 of expansion

arr ==> 2, 3, 4, 5, 6, 9, 8, 7, 1, 8

original ==> 2, 3, 4, 5, 6, 9, 8, 7, 1, 8

After (c) step 2 of expansion

arr ==> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

original ==> 2, 3, 4, 5, 6, 9, 8, 7, 1, 8

After (c) step 3 of expansion (copy original integers)

arr ==> 2, 3, 4, 5, 6, 9, 8, 7, 1, 8, 0, 0

original ==> 2, 3, 4, 5, 6, 9, 8, 7, 1, 8

After (d)

arr ==> 2, 3, 4, 5, 6, 9, 8, 7, 1, 8, 0, 0

Please write the Java Code that will execute the array expansion in figure 2.5 below. Note: This question does not require the use of the problem solving process. However, your solution should output the values of the array after each step of the expansion process. Example Output: After (a) new array of ten integers arr ==> 2, 3, 4, 5, 6, 9, 8, 7, 1, 8 After (b) step 1 of expansion arr ==> 2, 3, 4, 5, 6, 9, 8, 7, 1, 8 original ==> 2, 3, 4, 5, 6, 9, 8, 7, 1, 8 After (c) step 2 of expansion arr ==> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 original ==> 2, 3, 4, 5, 6, 9, 8, 7, 1, 8 After (c) step 3 of expansion (copy original integers) arr ==> 2, 3, 4, 5, 6, 9, 8, 7, 1, 8, 0, 0 original ==> 2, 3, 4, 5, 6, 9, 8, 7, 1, 8 After (d) arr ==> 2, 3, 4, 5, 6, 9, 8, 7, 1, 8, 0, 0 original ==> null

Explanation / Answer

import java.util.Arrays;


class ArrayExpansion{
public static void main(String []args){
   int arr[] = {2,3,4,5,6,9,8,7,1,8};
      
       System.out.println("Initial Array : ");
       for(int i=0; i< arr.length; i++){
       System.out.print(arr[i] + " ");
       }
       System.out.println();
      
       int original[] = new int[arr.length];
      
       for(int i =0; i< arr.length; i++){
       original[i] = arr[i];
       }
      
       System.out.println("After Step 1 of expansion : ");
       for(int i=0; i< arr.length; i++){
       System.out.print(arr[i] + " ");
       }
       System.out.println();
      
       for(int i=0; i< original.length; i++){
       System.out.print(original[i] + " ");
       }
       System.out.println();
      
       int newArr[] = Arrays.copyOf(arr,12);
       for(int i =0; i< newArr.length; i++){
       newArr[i] = 0;
       }
      
       System.out.println("After step 2 of expansion : ");
       for(int i=0; i< newArr.length; i++){
       System.out.print(newArr[i] + " ");
       }
       System.out.println();
      
       for(int i=0; i< original.length; i++){
       System.out.print(original[i] + " ");
       }
       System.out.println();
      
       for(int i =0; i< original.length; i++){
       newArr[i] = original[i];
       }
      
       System.out.println("After step 3 of expansion : ");
       for(int i=0; i< newArr.length; i++){
       System.out.print(newArr[i] + " ");
       }
       System.out.println();
      
       for(int i=0; i< original.length; i++){
       System.out.print(original[i] + " ");
       }
       System.out.println();
       original = null;
      
       System.out.println("After step 4 of expansion : ");
      
       for(int i=0; i< newArr.length; i++){
       System.out.print(newArr[i] + " ");
       }
       System.out.println();
   }
}