102k views
1 vote
Define an array called myArray of length 120 type integer. Write a java program that fills this array with the values 5, 10, 15, 20, 25, ..... and then we need one method called processArray() that outputs the average value of this array. Then main method prints the first 10 elements of the array. note the array is filled by a method called fill my array()_____

User Magic Mick
by
8.6k points

1 Answer

4 votes

Final answer:

To define an array called myArray of length 120 and type integer in Java, you can use the int[] myArray = new int[120] statement. You can fill this array with values using a method called fillMyArray(). To calculate the average value of the array, you can use the processArray() method. The main method can print the first 10 elements of the array.

Step-by-step explanation:

To define an array called myArray in Java of length 120 and type integer, you can use the following code:

int[] myArray = new int[120];

To fill this array with the values 5, 10, 15, 20, 25, and so on, you can create a method called fillMyArray() that uses a loop to assign the values:

public static void fillMyArray() {
for (int i = 0; i < myArray.length; i++) {
myArray[i] = (i + 1) * 5;
}
}

To calculate the average value of the array, you can create a method called processArray() that sums up all the elements and divides by the length of the array:

public static void processArray() {
int sum = 0;
for (int i = 0; i < myArray.length; i++) {
sum += myArray[i];
}
double average = (double) sum / myArray.length;
System.out.println(average);
}

To print the first 10 elements of the array, you can add the following code in the main method:

for (int i = 0; i < 10; i++) {
System.out.println(myArray[i]);
}
User Kheyse
by
8.5k points