Skip to content

Commit 13262f5

Browse files
Minor Update Average.java
1 parent d948390 commit 13262f5

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed
Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
package com.thealgorithms.maths;
22

33
/**
4-
* Utility class for calculating the average of a list of numbers.
4+
* Utility class for calculating the average of numeric arrays.
55
*/
66
public final class Average {
77

88
// Private constructor to prevent instantiation
99
private Average() {
10-
throw new UnsupportedOperationException("Utility class cannot be instantiated");
1110
}
1211

1312
/**
1413
* Calculates the average of a double array.
1514
*
16-
* @param numbers the array of numbers
15+
* @param numbers an array of doubles
1716
* @return the mean of the given numbers
18-
* @throws IllegalArgumentException if the input array is null or empty
17+
* @throws IllegalArgumentException if the array is null or empty
1918
*/
20-
public static double average(double[] numbers) {
21-
validateInput(numbers);
19+
public static double calculateAverage(double[] numbers) {
20+
if (numbers == null || numbers.length == 0) {
21+
throw new IllegalArgumentException("Array cannot be null or empty.");
22+
}
2223
double sum = 0;
2324
for (double number : numbers) {
2425
sum += number;
@@ -29,28 +30,18 @@ public static double average(double[] numbers) {
2930
/**
3031
* Calculates the average of an int array.
3132
*
32-
* @param numbers the array of numbers, ensuring the sum does not exceed long value limits
33-
* @return the average value
34-
* @throws IllegalArgumentException if the input array is null or empty
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
3536
*/
36-
public static int average(int[] numbers) {
37-
validateInput(numbers);
37+
public static int calculateAverage(int[] numbers) {
38+
if (numbers == null || numbers.length == 0) {
39+
throw new IllegalArgumentException("Array cannot be null or empty.");
40+
}
3841
long sum = 0;
3942
for (int number : numbers) {
4043
sum += number;
4144
}
4245
return (int) (sum / numbers.length);
4346
}
44-
45-
/**
46-
* Validates the input array.
47-
*
48-
* @param numbers the array of numbers
49-
* @throws IllegalArgumentException if the input array is null or empty
50-
*/
51-
private static void validateInput(Object numbers) {
52-
if (numbers == null || ((Object[]) numbers).length == 0) {
53-
throw new IllegalArgumentException("Numbers array cannot be null or empty");
54-
}
55-
}
5647
}

0 commit comments

Comments
 (0)