forked from HarryDulaney/intro-to-java-programming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExercise07_08.java
51 lines (41 loc) · 1.31 KB
/
Exercise07_08.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package ch_07;
import java.util.Scanner;
/**
* 7.8 (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.
*
* @author Harry Dulaney
*/
public class Exercise07_08 {
public static int average(int[] array) {
int elemInArray = array.length;
int sum = 0;
for (int i = 0; i < elemInArray; i++) {
sum += array[i];
}
return sum / elemInArray;
}
public static double average(double[] array) {
int elemInArray = array.length;
double sum = 0;
for (int i = 0; i < elemInArray; i++) {
sum += array[i];
}
double average = sum / elemInArray;
return average;
}
public static void main(String[] args) {
double[] values = new double[10];
Scanner input = new Scanner(System.in);
System.out.println("Please enter ten double values: ");
for (int i = 0; i < 10; i++) {
values[i] = input.nextDouble();
}
double toPrint = average(values);
System.out.println(toPrint);
}
}