1
1
package com .thealgorithms .maths ;
2
2
3
3
/**
4
- * Utility class for calculating the average of numeric arrays.
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 {
7
9
8
- // Private constructor to prevent instantiation
10
+ // Prevent instantiation of this utility class
9
11
private Average () {
12
+ throw new UnsupportedOperationException ("This is a utility class and cannot be instantiated." );
10
13
}
11
14
12
15
/**
13
- * Calculates the average of a double array.
16
+ * Computes the average of a {@code double} array.
14
17
*
15
- * @param numbers an array of doubles
16
- * @return the mean of the given numbers
17
- * @throws IllegalArgumentException if the array is null or empty
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
18
21
*/
19
- public static double calculateAverage (double [] numbers ) {
22
+ public static double computeAverage (double [] numbers ) {
20
23
if (numbers == null || numbers .length == 0 ) {
21
- throw new IllegalArgumentException ("Array cannot be null or empty." );
24
+ throw new IllegalArgumentException ("Array must not be null or empty." );
22
25
}
23
26
double sum = 0 ;
24
27
for (double number : numbers ) {
@@ -28,15 +31,15 @@ public static double calculateAverage(double[] numbers) {
28
31
}
29
32
30
33
/**
31
- * Calculates the average of an int array.
34
+ * Computes the average of an {@code int} array.
32
35
*
33
- * @param numbers an array of integers
34
- * @return the mean of the given numbers
35
- * @throws IllegalArgumentException if the array is null or empty
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
36
39
*/
37
- public static int calculateAverage (int [] numbers ) {
40
+ public static int computeAverage (int [] numbers ) {
38
41
if (numbers == null || numbers .length == 0 ) {
39
- throw new IllegalArgumentException ("Array cannot be null or empty." );
42
+ throw new IllegalArgumentException ("Array must not be null or empty." );
40
43
}
41
44
long sum = 0 ;
42
45
for (int number : numbers ) {
0 commit comments