Repository: TheAlgorithms/Java
Pull request #3020 (2022-04-19 22:55:10)

Code refactor for AbsoluteMax improvements
### **Describe your change:**

- [ ] Add an algorithm?
- [x] Fix a bug or typo in an existing algorithm?
- [x] Documentation change?

#### References

<!-- Add any reference to previous pull-request or issue -->

See: https://github.com/TheAlgorithms/Java/issues/3019

<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [x] All functions and variable names follow Java naming conventions.
- [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.



Patch:
diff --git a/src/main/java/com/thealgorithms/maths/AbsoluteMax.java b/src/main/java/com/thealgorithms/maths/AbsoluteMax.java
index b67b5991..76430db7 100644
--- a/src/main/java/com/thealgorithms/maths/AbsoluteMax.java
+++ b/src/main/java/com/thealgorithms/maths/AbsoluteMax.java
@@ -2,35 +2,31 @@ package com.thealgorithms.maths;
 
 import java.util.Arrays;
 
-/**
- * description:
- *
- * <p>
- * absMax([0, 5, 1, 11]) = 11, absMax([3 , -10, -2]) = -10
- */
 public class AbsoluteMax {
 
-    public static void main(String[] args) {
-        int[] testnums = {-2, 0, 16};
-        assert absMax(testnums) == 16;
-
-        int[] numbers = {3, -10, -2};
-        System.out.println("absMax(" + Arrays.toString(numbers) + ") = " + absMax(numbers));
-    }
-
     /**
-     * get the value, return the absolute max value
+     * Compares the numbers given as arguments to get the absolute max value.
      *
-     * @param numbers contains elements
-     * @return the absolute max value
+     * @param numbers The numbers to compare
+     * @return The absolute max value
      */
-    public static int absMax(int[] numbers) {
-        int absMaxValue = numbers[0];
-        for (int i = 1, length = numbers.length; i < length; ++i) {
-            if (Math.abs(numbers[i]) > Math.abs(absMaxValue)) {
-                absMaxValue = numbers[i];
-            }
+    public static int getMaxValue(int... numbers) {
+        if (numbers.length == 0) {
+            throw new IllegalArgumentException("Numbers array cannot be empty");
         }
-        return absMaxValue;
+
+        var absMaxWrapper = new Object() {
+            int value = numbers[0];
+        };
+
+        Arrays.stream(numbers)
+                .skip(1)
+                .forEach(number -> {
+                    if (Math.abs(number) > Math.abs(absMaxWrapper.value)) {
+                        absMaxWrapper.value = number;
+                    }
+                });
+
+        return absMaxWrapper.value;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/AbsoluteMaxTest.java b/src/test/java/com/thealgorithms/maths/AbsoluteMaxTest.java
new file mode 100644
index 00000000..a68c1a81
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/AbsoluteMaxTest.java
@@ -0,0 +1,21 @@
+package com.thealgorithms.maths;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+public class AbsoluteMaxTest {
+
+    @Test
+    void testGetMaxValue() {
+        assertEquals(16, AbsoluteMax.getMaxValue(-2, 0, 16));
+        assertEquals(-10, AbsoluteMax.getMaxValue(3, -10, -2));
+    }
+
+    @Test
+    void testGetMaxValueWithNoArguments() {
+        Exception exception = assertThrows(IllegalArgumentException.class, () -> AbsoluteMax.getMaxValue());
+        assertEquals("Numbers array cannot be empty", exception.getMessage());
+    }
+}