1
1
package com .thealgorithms .maths ;
2
2
3
3
/**
4
- * Calculate average of a list of numbers
4
+ * A utility class for computing the average of numeric arrays.
5
+ * This class provides static methods to calculate the average of arrays
6
+ * of both {@code double} and {@code int} values.
5
7
*/
6
8
public final class Average {
9
+
10
+ // Prevent instantiation of this utility class
7
11
private Average () {
12
+ throw new UnsupportedOperationException ("This is a utility class and cannot be instantiated." );
8
13
}
9
14
10
15
/**
11
- * Calculate average of a list of numbers
16
+ * Computes the average of a {@code double} array.
12
17
*
13
- * @param numbers array to store numbers
14
- * @return mean of given numbers
18
+ * @param numbers an array of {@code double} values
19
+ * @return the average of the given numbers
20
+ * @throws IllegalArgumentException if the input array is {@code null} or empty
15
21
*/
16
22
public static double average (double [] numbers ) {
17
23
if (numbers == null || numbers .length == 0 ) {
@@ -25,20 +31,20 @@ public static double average(double[] numbers) {
25
31
}
26
32
27
33
/**
28
- * find average value of an int array
34
+ * Computes the average of an {@code int} array.
29
35
*
30
- * @param numbers the array contains element and the sum does not excess long
31
- * value limit
32
- * @return average value
36
+ * @param numbers an array of {@code int} values
37
+ * @return the average of the given numbers
38
+ * @throws IllegalArgumentException if the input array is {@code null} or empty
33
39
*/
34
- public static int average (int [] numbers ) {
40
+ public static double average (int [] numbers ) {
35
41
if (numbers == null || numbers .length == 0 ) {
36
42
throw new IllegalArgumentException ("Numbers array cannot be empty or null" );
37
43
}
38
44
long sum = 0 ;
39
45
for (int number : numbers ) {
40
46
sum += number ;
41
47
}
42
- return (int ) (sum / numbers .length );
48
+ return (double ) (sum / numbers .length );
43
49
}
44
50
}
0 commit comments