I know i can't pass arrays the way I'm doing it. Would I need to pass by reference if so how? The question is at the bottom for reference.
import java.util.Scanner;
public class MethodsArrays {
public static int[] fillArray() {
Scanner scan = new Scanner(System.in);
int size = scan.nextInt();
int array[] = new int[size];
int pos=0;
for(int i=0; i<array.length; i++) {
pos=i+1;
System.out.println("Enter element " + pos);
array[i]=scan.nextInt();
}
return array;
}
public static int sumArray(int [] array) {
int sum=0;
for(int i=0; i<array.length-1; i++) {
sum=array[i]+array[i+1];
}
return sum;
}
public static int avgArray(int [] array) {
int avg=0;
int sum = sumArray(array);
avg = sumArray(array)/array.length-1;
return avg;
}
public static void printArray(int [] array) {
for(int i=0; i<array.length; i++) {
System.out.print(array[i] + " ");
}
}
public static void main(String[] args) {
fillArray();
System.out.println("Sum=" + sumArray(array));
System.out.println("Average=" + avgArray(array));
printArray(array);
}
}
Write a Java program, called MethodsArrays that has 4 static methods called fillArray(), sumArray(), avgArray(), and printArray(). The fillArray() method should be called from the main method. The fillArray() method should use a Scanner to take in a number representing the length of the array and then read in numbers to fill the array. The sumArray() method should take an int array as its input parameter and returns an integer value that is the sum of all the elements in the array. The avgArray() method should take an int array as its input parameter and returns an integer value that is the average of all the elements in the array. The printArray() method should take an int array as its input parameter and has no return value. It should then print out the elements of the array on the same line separated by a space (“ “). All methods should work for integer arrays.
fillArray()returns ...fillArray()in a variable namedarrayso the variable name actually exists and you can use it in the following calls.sum=array[i]+array[i+1];What is this supposed to do?