Skip to content

Commit 742a188

Browse files
authored
Merge branch 'master' into feat/flash_sort
2 parents 824492e + 6f52114 commit 742a188

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
package com.thealgorithms.maths;
22

33
/**
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.
57
*/
68
public final class Average {
9+
10+
// Prevent instantiation of this utility class
711
private Average() {
12+
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated.");
813
}
914

1015
/**
11-
* Calculate average of a list of numbers
16+
* Computes the average of a {@code double} array.
1217
*
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
1521
*/
1622
public static double average(double[] numbers) {
1723
if (numbers == null || numbers.length == 0) {
@@ -25,20 +31,20 @@ public static double average(double[] numbers) {
2531
}
2632

2733
/**
28-
* find average value of an int array
34+
* Computes the average of an {@code int} array.
2935
*
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
3339
*/
34-
public static int average(int[] numbers) {
40+
public static double average(int[] numbers) {
3541
if (numbers == null || numbers.length == 0) {
3642
throw new IllegalArgumentException("Numbers array cannot be empty or null");
3743
}
3844
long sum = 0;
3945
for (int number : numbers) {
4046
sum += number;
4147
}
42-
return (int) (sum / numbers.length);
48+
return (double) (sum / numbers.length);
4349
}
4450
}

0 commit comments

Comments
 (0)