From 6f521145cce64822ac31331a33e48a9a748670c9 Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Sun, 4 Aug 2024 21:15:54 +0300 Subject: [PATCH] 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).. --- .../java/com/thealgorithms/maths/Average.java | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/Average.java b/src/main/java/com/thealgorithms/maths/Average.java index 1c632cf0..6b9c2016 100644 --- a/src/main/java/com/thealgorithms/maths/Average.java +++ b/src/main/java/com/thealgorithms/maths/Average.java @@ -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); } }