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

Please write the following answer in JAVA (not javascript). Please provide answe

ID: 3861757 • Letter: P

Question

Please write the following answer in JAVA (not javascript). Please provide answer in copyable text. Show input and output. Thanks in advance.

#1: Write a method called expandArray and a test class to expand an arbitrary array of ints. You will pass to expandArray an array of ints and an integer value of how many elements to expand the array by. This method expands the array in place, preserving all existing values. expandArray throws IllegalArgumentException if the input array is null or the amount to expand K-0. This is worth 10 pts

Explanation / Answer

ExpandArrayTest.java

import java.util.Arrays;


public class ExpandArrayTest {

  
   public static void main(String[] args) {
       int a[] = {1,2,3,4,5};
       int expand = 5;
       System.out.println("Array elements before expand: "+Arrays.toString(a));
       try{
       a = expandArray(a, expand);
       System.out.println("Array elements after expand: "+Arrays.toString(a));
       }
       catch(IllegalArgumentException e){
           System.out.println(e);
       }
   }
   public static int[] expandArray(int a[], int expand) throws IllegalArgumentException {
       if(a == null || expand <=0){
           throw new IllegalArgumentException("Invalid arguement passed. Array must have some values in it");
       }
       else {
           a = Arrays.copyOf(a, a.length + expand);
       }
       return a;
   }
}

Output:

Array elements before expand: [1, 2, 3, 4, 5]
Array elements after expand: [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]