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:
parent
fccd141014
commit
6f521145cc
@ -1,17 +1,23 @@
|
|||||||
package com.thealgorithms.maths;
|
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 {
|
public final class Average {
|
||||||
|
|
||||||
|
// Prevent instantiation of this utility class
|
||||||
private Average() {
|
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
|
* @param numbers an array of {@code double} values
|
||||||
* @return mean of given numbers
|
* @return the average of the given numbers
|
||||||
|
* @throws IllegalArgumentException if the input array is {@code null} or empty
|
||||||
*/
|
*/
|
||||||
public static double average(double[] numbers) {
|
public static double average(double[] numbers) {
|
||||||
if (numbers == null || numbers.length == 0) {
|
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
|
* @param numbers an array of {@code int} values
|
||||||
* value limit
|
* @return the average of the given numbers
|
||||||
* @return average value
|
* @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) {
|
if (numbers == null || numbers.length == 0) {
|
||||||
throw new IllegalArgumentException("Numbers array cannot be empty or null");
|
throw new IllegalArgumentException("Numbers array cannot be empty or null");
|
||||||
}
|
}
|
||||||
@ -39,6 +45,6 @@ public final class Average {
|
|||||||
for (int number : numbers) {
|
for (int number : numbers) {
|
||||||
sum += number;
|
sum += number;
|
||||||
}
|
}
|
||||||
return (int) (sum / numbers.length);
|
return (double) (sum / numbers.length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user