Skip to content

Instantly share code, notes, and snippets.

@gitaficionado
Created February 2, 2018 23:30
Show Gist options
  • Save gitaficionado/cbfad90584f9eb5c091fae699d459316 to your computer and use it in GitHub Desktop.
Save gitaficionado/cbfad90584f9eb5c091fae699d459316 to your computer and use it in GitHub Desktop.
Write two overloaded methods that return the average of an array with the following headers: public static int average(int[] array) public static double average(double[] array) Write a test program that prompts the user to enter ten double values, invokes this method, and displays the average value.
/*
(Average an array) Write two overloaded methods that return the average of an
array with the following headers:
public static int average(int[] array)
public static double average(double[] array)
Write a test program that prompts the user to enter ten double values, invokes this
method, and displays the average value.
*/
import java.util.Scanner;
public class AverageAnArray {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double[] list2 = new double[10];
System.out.print("Enter 10 double values: ");
int i = 0;
while (i < ______.length) {
list2[i] = input.nextDouble();
i++;
}
System.out.println(average(list2));
}
public static int average(int[] array) {
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
return sum / array._______;
}
public static double average(double[] array) {
double sum = 0;
for (int i = 0; i < array.length; i++) {
sum += ________[i];
}
return sum / ______.length;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment