Repository: TheAlgorithms/Java
Pull request #5392 (2024-08-25 19:38:11)

refactor: `DecimalToAnyUsingStack`
Class cleanup.
Readability improvements.
Added tests.

<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- 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 it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5392?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 47.49%. Comparing base [(`f3851e3`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/f3851e3adc9b6054b9868e0c454e5b7a52f6fccd?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`cb1469b`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/cb1469bc634bfb5cf0456493a53ab9ca291a8790?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5392      +/-   ##
============================================
+ Coverage     47.39%   47.49%   +0.09%     
- Complexity     2923     2930       +7     
============================================
  Files           524      524              
  Lines         15317    15314       -3     
  Branches       2907     2905       -2     
============================================
+ Hits           7260     7273      +13     
+ Misses         7757     7740      -17     
- Partials        300      301       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5392?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java b/src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java
index 41d1c640..ff6402c9 100644
--- a/src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java
+++ b/src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java
@@ -6,50 +6,33 @@ public final class DecimalToAnyUsingStack {
     private DecimalToAnyUsingStack() {
     }
 
-    public static void main(String[] args) {
-        assert convert(0, 2).equals("0");
-        assert convert(30, 2).equals("11110");
-        assert convert(30, 8).equals("36");
-        assert convert(30, 10).equals("30");
-        assert convert(30, 16).equals("1E");
-    }
-
     /**
-     * Convert decimal number to another radix
+     * Convert a decimal number to another radix.
      *
      * @param number the number to be converted
      * @param radix the radix
-     * @return another radix
-     * @throws ArithmeticException if <tt>number</tt> or <tt>radius</tt> is
-     * invalid
+     * @return the number represented in the new radix as a String
+     * @throws IllegalArgumentException if <tt>number</tt> is negative or <tt>radix</tt> is not between 2 and 16 inclusive
      */
-    private static String convert(int number, int radix) {
+    public static String convert(int number, int radix) {
+        if (number < 0) {
+            throw new IllegalArgumentException("Number must be non-negative.");
+        }
         if (radix < 2 || radix > 16) {
-            throw new ArithmeticException(String.format("Invalid input -> number:%d,radius:%d", number, radix));
+            throw new IllegalArgumentException(String.format("Invalid radix: %d. Radix must be between 2 and 16.", radix));
+        }
+
+        if (number == 0) {
+            return "0";
         }
-        char[] tables = {
-            '0',
-            '1',
-            '2',
-            '3',
-            '4',
-            '5',
-            '6',
-            '7',
-            '8',
-            '9',
-            'A',
-            'B',
-            'C',
-            'D',
-            'E',
-            'F',
-        };
+
+        char[] tables = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
+
         Stack<Character> bits = new Stack<>();
-        do {
+        while (number > 0) {
             bits.push(tables[number % radix]);
             number = number / radix;
-        } while (number != 0);
+        }
 
         StringBuilder result = new StringBuilder();
         while (!bits.isEmpty()) {


Test patch:
diff --git a/src/test/java/com/thealgorithms/stacks/DecimalToAnyUsingStackTest.java b/src/test/java/com/thealgorithms/stacks/DecimalToAnyUsingStackTest.java
new file mode 100644
index 00000000..4bd9f2af
--- /dev/null
+++ b/src/test/java/com/thealgorithms/stacks/DecimalToAnyUsingStackTest.java
@@ -0,0 +1,45 @@
+package com.thealgorithms.stacks;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.junit.jupiter.api.Test;
+
+class DecimalToAnyUsingStackTest {
+
+    @Test
+    void testConvertToBinary() {
+        assertEquals("0", DecimalToAnyUsingStack.convert(0, 2));
+        assertEquals("11110", DecimalToAnyUsingStack.convert(30, 2));
+    }
+
+    @Test
+    void testConvertToOctal() {
+        assertEquals("36", DecimalToAnyUsingStack.convert(30, 8));
+    }
+
+    @Test
+    void testConvertToDecimal() {
+        assertEquals("30", DecimalToAnyUsingStack.convert(30, 10));
+    }
+
+    @Test
+    void testConvertToHexadecimal() {
+        assertEquals("1E", DecimalToAnyUsingStack.convert(30, 16));
+    }
+
+    @Test
+    void testInvalidRadix() {
+        IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> DecimalToAnyUsingStack.convert(30, 1));
+        assertEquals("Invalid radix: 1. Radix must be between 2 and 16.", thrown.getMessage());
+
+        thrown = assertThrows(IllegalArgumentException.class, () -> DecimalToAnyUsingStack.convert(30, 17));
+        assertEquals("Invalid radix: 17. Radix must be between 2 and 16.", thrown.getMessage());
+    }
+
+    @Test
+    void testNegativeNumber() {
+        IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> DecimalToAnyUsingStack.convert(-30, 2));
+        assertEquals("Number must be non-negative.", thrown.getMessage());
+    }
+}