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

1) Create an array of size 10 by calling the createArray method. STore the retur

ID: 3689793 • Letter: 1

Question

1) Create an array of size 10 by calling the createArray method. STore the return value into the variable name arr. Make sure arr has the correct type. 2) Call fillArray, passing in the array arr. Note that this is a void function. 3) After calling fillArray, calll the method sum, passing in the array arr. Store the return value into a variable named aSum. 4) Print aSum to the console. public class EC{ Public static int[] createArray(int size){ return new int[size]; } public static void fillArray(int[] nums){ for (int i = 0; i < nums.length;i++){ num[i]=i; } } public static int sum(int[] nums){ int sum=0; } public static void main(String[] args){ } }

Explanation / Answer

EC.java


import java.util.Scanner;

public class EC{
public static int[] createArray(int size){
   return new int[size];
   }
public static void fillArray(int[] nums){
   for (int i = 0; i < nums.length;i++){
       nums[i]=i;
       }
}
public static int sum(int[] nums){
   int sum=0;
   for(int i=0; i<nums.length; i++){
       sum = sum + nums[i];
   }
   return sum;
}
public static void main(String[] args){
   Scanner scan = new Scanner(System.in);
   System.out.println("Please enter the size of an array");
   int n = scan.nextInt();
   int arr[] = createArray(n);
   fillArray(arr);
   int totalValue = sum(arr);
   System.out.println("The sum of all array elements is : "+totalValue);
}
}

Output:

Please enter the size of an array
6
The sum of all array elements is : 15