-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathAverage.java
50 lines (46 loc) · 1.63 KB
/
Average.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
package com.thealgorithms.maths;
/**
* A utility class for computing the average of numeric arrays.
* This class provides static methods to calculate the average of arrays
* of both {@code double} and {@code int} values.
*/
public final class Average {
// Prevent instantiation of this utility class
private Average() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated.");
}
/**
* Computes the average of a {@code double} array.
*
* @param numbers an array of {@code double} values
* @return the average of the given numbers
* @throws IllegalArgumentException if the input array is {@code null} or empty
*/
public static double average(double[] numbers) {
if (numbers == null || numbers.length == 0) {
throw new IllegalArgumentException("Numbers array cannot be empty or null");
}
double sum = 0;
for (double number : numbers) {
sum += number;
}
return sum / numbers.length;
}
/**
* Computes the average of an {@code int} array.
*
* @param numbers an array of {@code int} values
* @return the average of the given numbers
* @throws IllegalArgumentException if the input array is {@code null} or empty
*/
public static long average(int[] numbers) {
if (numbers == null || numbers.length == 0) {
throw new IllegalArgumentException("Numbers array cannot be empty or null");
}
long sum = 0;
for (int number : numbers) {
sum += number;
}
return sum / numbers.length;
}
}