From d948390db5027abed861685cf886a048f820b55b Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Sat, 3 Aug 2024 11:51:22 +0300 Subject: [PATCH 1/7] 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. --- .../java/com/thealgorithms/maths/Average.java | 40 ++++++++++++------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/Average.java b/src/main/java/com/thealgorithms/maths/Average.java index 1c632cf0a65e..b6ef35096b1b 100644 --- a/src/main/java/com/thealgorithms/maths/Average.java +++ b/src/main/java/com/thealgorithms/maths/Average.java @@ -1,22 +1,24 @@ package com.thealgorithms.maths; /** - * Calculate average of a list of numbers + * Utility class for calculating the average of a list of numbers. */ public final class Average { + + // Private constructor to prevent instantiation private Average() { + throw new UnsupportedOperationException("Utility class cannot be instantiated"); } /** - * Calculate average of a list of numbers + * Calculates the average of a double array. * - * @param numbers array to store numbers - * @return mean of given numbers + * @param numbers the array of numbers + * @return the mean of the given numbers + * @throws IllegalArgumentException if the input array is null or empty */ public static double average(double[] numbers) { - if (numbers == null || numbers.length == 0) { - throw new IllegalArgumentException("Numbers array cannot be empty or null"); - } + validateInput(numbers); double sum = 0; for (double number : numbers) { sum += number; @@ -25,20 +27,30 @@ public static double average(double[] numbers) { } /** - * find average value of an int array + * Calculates the average of an int array. * - * @param numbers the array contains element and the sum does not excess long - * value limit - * @return average value + * @param numbers the array of numbers, ensuring the sum does not exceed long value limits + * @return the average value + * @throws IllegalArgumentException if the input array is null or empty */ public static int average(int[] numbers) { - if (numbers == null || numbers.length == 0) { - throw new IllegalArgumentException("Numbers array cannot be empty or null"); - } + validateInput(numbers); long sum = 0; for (int number : numbers) { sum += number; } return (int) (sum / numbers.length); } + + /** + * Validates the input array. + * + * @param numbers the array of numbers + * @throws IllegalArgumentException if the input array is null or empty + */ + private static void validateInput(Object numbers) { + if (numbers == null || ((Object[]) numbers).length == 0) { + throw new IllegalArgumentException("Numbers array cannot be null or empty"); + } + } } From 13262f575b1e7863e029cbcc2478df6777aadae8 Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Sat, 3 Aug 2024 12:40:41 +0300 Subject: [PATCH 2/7] Minor Update Average.java --- .../java/com/thealgorithms/maths/Average.java | 37 +++++++------------ 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/Average.java b/src/main/java/com/thealgorithms/maths/Average.java index b6ef35096b1b..8c8bb803c469 100644 --- a/src/main/java/com/thealgorithms/maths/Average.java +++ b/src/main/java/com/thealgorithms/maths/Average.java @@ -1,24 +1,25 @@ package com.thealgorithms.maths; /** - * Utility class for calculating the average of a list of numbers. + * Utility class for calculating the average of numeric arrays. */ public final class Average { // Private constructor to prevent instantiation private Average() { - throw new UnsupportedOperationException("Utility class cannot be instantiated"); } /** * Calculates the average of a double array. * - * @param numbers the array of numbers + * @param numbers an array of doubles * @return the mean of the given numbers - * @throws IllegalArgumentException if the input array is null or empty + * @throws IllegalArgumentException if the array is null or empty */ - public static double average(double[] numbers) { - validateInput(numbers); + public static double calculateAverage(double[] numbers) { + if (numbers == null || numbers.length == 0) { + throw new IllegalArgumentException("Array cannot be null or empty."); + } double sum = 0; for (double number : numbers) { sum += number; @@ -29,28 +30,18 @@ public static double average(double[] numbers) { /** * Calculates the average of an int array. * - * @param numbers the array of numbers, ensuring the sum does not exceed long value limits - * @return the average value - * @throws IllegalArgumentException if the input array is null or empty + * @param numbers an array of integers + * @return the mean of the given numbers + * @throws IllegalArgumentException if the array is null or empty */ - public static int average(int[] numbers) { - validateInput(numbers); + public static int calculateAverage(int[] numbers) { + if (numbers == null || numbers.length == 0) { + throw new IllegalArgumentException("Array cannot be null or empty."); + } long sum = 0; for (int number : numbers) { sum += number; } return (int) (sum / numbers.length); } - - /** - * Validates the input array. - * - * @param numbers the array of numbers - * @throws IllegalArgumentException if the input array is null or empty - */ - private static void validateInput(Object numbers) { - if (numbers == null || ((Object[]) numbers).length == 0) { - throw new IllegalArgumentException("Numbers array cannot be null or empty"); - } - } } From 94b678940b8535eb8bb7ffbdb1a0818a4589dde5 Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Sat, 3 Aug 2024 12:45:58 +0300 Subject: [PATCH 3/7] Change To Average.java --- .../java/com/thealgorithms/maths/Average.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/Average.java b/src/main/java/com/thealgorithms/maths/Average.java index 8c8bb803c469..9307143bafeb 100644 --- a/src/main/java/com/thealgorithms/maths/Average.java +++ b/src/main/java/com/thealgorithms/maths/Average.java @@ -1,24 +1,27 @@ package com.thealgorithms.maths; /** - * Utility class for calculating the average of numeric arrays. + * 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 { - // Private constructor to prevent instantiation + // Prevent instantiation of this utility class private Average() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated."); } /** - * Calculates the average of a double array. + * Computes the average of a {@code double} array. * - * @param numbers an array of doubles - * @return the mean of the given numbers - * @throws IllegalArgumentException if the array is null or empty + * @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 calculateAverage(double[] numbers) { + public static double computeAverage(double[] numbers) { if (numbers == null || numbers.length == 0) { - throw new IllegalArgumentException("Array cannot be null or empty."); + throw new IllegalArgumentException("Array must not be null or empty."); } double sum = 0; for (double number : numbers) { @@ -28,15 +31,15 @@ public static double calculateAverage(double[] numbers) { } /** - * Calculates the average of an int array. + * Computes the average of an {@code int} array. * - * @param numbers an array of integers - * @return the mean of the given numbers - * @throws IllegalArgumentException if the array is null or empty + * @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 calculateAverage(int[] numbers) { + public static int computeAverage(int[] numbers) { if (numbers == null || numbers.length == 0) { - throw new IllegalArgumentException("Array cannot be null or empty."); + throw new IllegalArgumentException("Array must not be null or empty."); } long sum = 0; for (int number : numbers) { From 1aad83d3283485959c84cb2c028adbfdb16f340c Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Sat, 3 Aug 2024 12:48:24 +0300 Subject: [PATCH 4/7] Mnr Average.java --- src/main/java/com/thealgorithms/maths/Average.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/Average.java b/src/main/java/com/thealgorithms/maths/Average.java index 9307143bafeb..3c817b797b1b 100644 --- a/src/main/java/com/thealgorithms/maths/Average.java +++ b/src/main/java/com/thealgorithms/maths/Average.java @@ -19,7 +19,7 @@ private Average() { * @return the average of the given numbers * @throws IllegalArgumentException if the input array is {@code null} or empty */ - public static double computeAverage(double[] numbers) { + public static double average(double[] numbers) { if (numbers == null || numbers.length == 0) { throw new IllegalArgumentException("Array must not be null or empty."); } @@ -37,7 +37,7 @@ public static double computeAverage(double[] numbers) { * @return the average of the given numbers * @throws IllegalArgumentException if the input array is {@code null} or empty */ - public static int computeAverage(int[] numbers) { + public static int average(int[] numbers) { if (numbers == null || numbers.length == 0) { throw new IllegalArgumentException("Array must not be null or empty."); } From e24b3432ccf657980a75caefe27ea303f8481aee Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Sat, 3 Aug 2024 12:48:55 +0300 Subject: [PATCH 5/7] Update_Average.java From 0a045827b8c51a03f9d44eab65fc3c9c3d10e471 Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Sun, 4 Aug 2024 16:58:07 +0300 Subject: [PATCH 6/7] Fix Average.java 1. throw new IllegalArgumentException("Numbers array cannot be empty or null"); 2. int --> double --- src/main/java/com/thealgorithms/maths/Average.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/Average.java b/src/main/java/com/thealgorithms/maths/Average.java index 3c817b797b1b..a996e78056fd 100644 --- a/src/main/java/com/thealgorithms/maths/Average.java +++ b/src/main/java/com/thealgorithms/maths/Average.java @@ -21,7 +21,7 @@ private Average() { */ public static double average(double[] numbers) { if (numbers == null || numbers.length == 0) { - throw new IllegalArgumentException("Array must not be null or empty."); + throw new IllegalArgumentException("Numbers array cannot be empty or null"); } double sum = 0; for (double number : numbers) { @@ -37,9 +37,9 @@ public static double average(double[] numbers) { * @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("Array must not be null or empty."); + throw new IllegalArgumentException("Numbers array cannot be empty or null"); } long sum = 0; for (int number : numbers) { From 5581299cd48c10388df395956f08d87a6e685feb Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Sun, 4 Aug 2024 17:04:11 +0300 Subject: [PATCH 7/7] fix2.java return(double).. --- src/main/java/com/thealgorithms/maths/Average.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/maths/Average.java b/src/main/java/com/thealgorithms/maths/Average.java index a996e78056fd..6b9c20162da1 100644 --- a/src/main/java/com/thealgorithms/maths/Average.java +++ b/src/main/java/com/thealgorithms/maths/Average.java @@ -45,6 +45,6 @@ public static double average(int[] numbers) { for (int number : numbers) { sum += number; } - return (int) (sum / numbers.length); + return (double) (sum / numbers.length); } }