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

program in java. wn headers: public static int average(intl array) public static

ID: 3599487 • Letter: P

Question

program in java.

wn headers: public static int average(intl array) public static double average(doublel] array) Write a program that prompts the user to enter ten integer values into an array named listl, invokes the appropriate method and displays the average value. The program then prompts the user to enter ten double values into an array named list2, invokes the appropriate method and displays the average value. Cwindows system321cmd.exe Average arrays Enter 10 integer values: 2 4 5 1 26 8459 The average of the ten integers is 4 Enter 10 double values: 2.5 1.2 4.5 8.1 2.37.5 7.5 8.4 6.9 9.5 The average of the ten doubles is 5.839999999999999 ress any key to continue .

Explanation / Answer

import java.util.Scanner;


public class Average {

public static int average(int[] array) {
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
return sum/array.length;
}
  
public static double average(double[] array) {
double sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
return sum/array.length;
}
  
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
  
int[] list1 = new int[10];
System.out.print("Enter 10 integer values: ");
for (int i = 0; i < list1.length; i++) {
list1[i] = sc.nextInt();
}
System.out.println(" The avergae of the ten integers is " + average(list1));
  
double[] list2 = new double[10];
System.out.print("Enter 10 integer values: ");
for (int i = 0; i < list1.length; i++) {
list2[i] = sc.nextDouble();
}
System.out.println(" The avergae of the ten double is " + average(list2));
  
sc.close();
}
}


Sample execution

Enter 10 integer values: 2 4 5 1 2 6 8 4 5 9

The avergae of the ten integers is 4

Enter 10 integer values: 2.5 1.2 4.5 8.1 2.3 7.5 7.5 8.4 6.9 9.5

The avergae of the ten double is 5.839999999999999