mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
Update Average.java (#5309)
* Update Average.java
- Made the constructor throw an UnsupportedOperationException to prevent instantiation, making it explicit that this is a utility class.
- Added a private validateInput method to handle validation, reducing code duplication and improving readability.
- Consistent exception messages and handling for both methods.
- Improved comments to be more descriptive and follow JavaDoc conventions.
- Enhanced code readability and maintained consistent formatting.
* Minor Update Average.java
* Change To Average.java
* Mnr Average.java
* Update_Average.java
* Fix Average.java
1. throw new IllegalArgumentException("Numbers array cannot be empty or null");
2. int --> double
* fix2.java
return(double)..
This commit is contained in:
@@ -1,17 +1,23 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
/**
|
||||
* Calculate average of a list of numbers
|
||||
* A utility class for computing the average of numeric arrays.
|
||||
* This class provides static methods to calculate the average of arrays
|
||||
* of both {@code double} and {@code int} values.
|
||||
*/
|
||||
public final class Average {
|
||||
|
||||
// Prevent instantiation of this utility class
|
||||
private Average() {
|
||||
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate average of a list of numbers
|
||||
* Computes the average of a {@code double} array.
|
||||
*
|
||||
* @param numbers array to store numbers
|
||||
* @return mean of given numbers
|
||||
* @param numbers an array of {@code double} values
|
||||
* @return the average of the given numbers
|
||||
* @throws IllegalArgumentException if the input array is {@code null} or empty
|
||||
*/
|
||||
public static double average(double[] numbers) {
|
||||
if (numbers == null || numbers.length == 0) {
|
||||
@@ -25,13 +31,13 @@ public final class Average {
|
||||
}
|
||||
|
||||
/**
|
||||
* find average value of an int array
|
||||
* Computes the average of an {@code int} array.
|
||||
*
|
||||
* @param numbers the array contains element and the sum does not excess long
|
||||
* value limit
|
||||
* @return average value
|
||||
* @param numbers an array of {@code int} values
|
||||
* @return the average of the given numbers
|
||||
* @throws IllegalArgumentException if the input array is {@code null} or empty
|
||||
*/
|
||||
public static int average(int[] numbers) {
|
||||
public static double average(int[] numbers) {
|
||||
if (numbers == null || numbers.length == 0) {
|
||||
throw new IllegalArgumentException("Numbers array cannot be empty or null");
|
||||
}
|
||||
@@ -39,6 +45,6 @@ public final class Average {
|
||||
for (int number : numbers) {
|
||||
sum += number;
|
||||
}
|
||||
return (int) (sum / numbers.length);
|
||||
return (double) (sum / numbers.length);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user