Skip to content

Commit 6d3da37

Browse files
committed
docs(maths): improve JavaDoc for Average utility class
1 parent 49f9e1a commit 6d3da37

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

src/main/java/com/thealgorithms/maths/Average.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
/**
44
* 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+
*
6+
* <p>This class provides static methods to calculate the arithmetic mean
7+
* of arrays of both {@code double} and {@code int} values. It also offers
8+
* a Stream-based alternative for modern, declarative usage.
9+
*
10+
* <p>All methods guard against {@code null} or empty inputs.
711
*/
812
public final class Average {
913

@@ -13,11 +17,14 @@ private Average() {
1317
}
1418

1519
/**
16-
* Computes the average of a {@code double} array.
20+
* Computes the arithmetic mean of a {@code double} array.
1721
*
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
22+
* <p>The average is calculated as the sum of all elements divided
23+
* by the number of elements: {@code avg = Σ(numbers[i]) / n}.
24+
*
25+
* @param numbers a non-null, non-empty array of {@code double} values
26+
* @return the arithmetic mean of the given numbers
27+
* @throws IllegalArgumentException if {@code numbers} is {@code null} or empty
2128
*/
2229
public static double average(double[] numbers) {
2330
if (numbers == null || numbers.length == 0) {
@@ -31,11 +38,14 @@ public static double average(double[] numbers) {
3138
}
3239

3340
/**
34-
* Computes the average of an {@code int} array.
41+
* Computes the arithmetic mean of an {@code int} array.
42+
*
43+
* <p>The sum is accumulated in a {@code long} to prevent integer overflow
44+
* when processing large arrays or large values.
3545
*
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
46+
* @param numbers a non-null, non-empty array of {@code int} values
47+
* @return the arithmetic mean as a {@code long} (truncated toward zero)
48+
* @throws IllegalArgumentException if {@code numbers} is {@code null} or empty
3949
*/
4050
public static long average(int[] numbers) {
4151
if (numbers == null || numbers.length == 0) {

0 commit comments

Comments
 (0)