Repository: TheAlgorithms/Java
Pull request #5923 (2024-10-21 06:18:31)

refactor: Enhance docs, remove `main`. add more tests in `HexaDecimal…
…ToDecimal`

<!--
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/5923?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 66.82%. Comparing base [(`e499d3b`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/e499d3b63e9cb3dc711ddd5d4a2fc3faba9b3d46?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`85f07ff`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/85f07ff14262097dc6be5f03896cad3b9ba61b36?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    #5923      +/-   ##
============================================
+ Coverage     66.80%   66.82%   +0.02%     
  Complexity     4501     4501              
============================================
  Files           611      611              
  Lines         16958    16953       -5     
  Branches       3275     3276       +1     
============================================
+ Hits          11328    11329       +1     
+ Misses         5182     5175       -7     
- Partials        448      449       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5923?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/conversions/HexaDecimalToDecimal.java b/src/main/java/com/thealgorithms/conversions/HexaDecimalToDecimal.java
index 003781da..2cf6024d 100644
--- a/src/main/java/com/thealgorithms/conversions/HexaDecimalToDecimal.java
+++ b/src/main/java/com/thealgorithms/conversions/HexaDecimalToDecimal.java
@@ -1,39 +1,45 @@
 package com.thealgorithms.conversions;
 
-import java.util.Scanner;
-
+/**
+ * Utility class for converting a hexadecimal string to its decimal representation.
+ * <p>
+ * A hexadecimal number uses the base-16 numeral system, with the following characters:
+ * <ul>
+ *   <li>Digits: 0-9</li>
+ *   <li>Letters: A-F (case-insensitive)</li>
+ * </ul>
+ * Each character represents a power of 16. For example:
+ * <pre>
+ *   Hexadecimal "A1" = 10*16^1 + 1*16^0 = 161 (decimal)
+ * </pre>
+ *
+ * <p>This class provides a method to perform the conversion without using built-in Java utilities.</p>
+ */
 public final class HexaDecimalToDecimal {
     private HexaDecimalToDecimal() {
     }
 
-    // convert hexadecimal to decimal
+    /**
+     * Converts a hexadecimal string to its decimal integer equivalent.
+     * <p>The input string is case-insensitive, and must contain valid hexadecimal characters [0-9, A-F].</p>
+     *
+     * @param hex the hexadecimal string to convert
+     * @return the decimal integer representation of the input hexadecimal string
+     * @throws IllegalArgumentException if the input string contains invalid characters
+     */
     public static int getHexaToDec(String hex) {
         String digits = "0123456789ABCDEF";
         hex = hex.toUpperCase();
         int val = 0;
+
         for (int i = 0; i < hex.length(); i++) {
             int d = digits.indexOf(hex.charAt(i));
+            if (d == -1) {
+                throw new IllegalArgumentException("Invalid hexadecimal character: " + hex.charAt(i));
+            }
             val = 16 * val + d;
         }
-        return val;
-    }
 
-    // Main method gets the hexadecimal input from user and converts it into Decimal output.
-    public static void main(String[] args) {
-        String hexaInput;
-        int decOutput;
-        Scanner scan = new Scanner(System.in);
-
-        System.out.print("Enter Hexadecimal Number : ");
-        hexaInput = scan.nextLine();
-
-        // convert hexadecimal to decimal
-        decOutput = getHexaToDec(hexaInput);
-        /*
-    Pass the string to the getHexaToDec function
-    and it returns the decimal form in the variable decOutput.
-         */
-        System.out.println("Number in Decimal: " + decOutput);
-        scan.close();
+        return val;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/conversions/HexaDecimalToDecimalTest.java b/src/test/java/com/thealgorithms/conversions/HexaDecimalToDecimalTest.java
index c9c2ab21..d0d6b400 100644
--- a/src/test/java/com/thealgorithms/conversions/HexaDecimalToDecimalTest.java
+++ b/src/test/java/com/thealgorithms/conversions/HexaDecimalToDecimalTest.java
@@ -1,14 +1,37 @@
 package com.thealgorithms.conversions;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
 
 public class HexaDecimalToDecimalTest {
 
-    @Test
-    public void testhexaDecimalToDecimal() {
-        assertEquals(161, HexaDecimalToDecimal.getHexaToDec("A1"));
-        assertEquals(428, HexaDecimalToDecimal.getHexaToDec("1ac"));
+    @ParameterizedTest
+    @CsvSource({
+        "A1, 161", // Simple case with two characters
+        "1AC, 428", // Mixed-case input
+        "0, 0", // Single zero
+        "F, 15", // Single digit
+        "10, 16", // Power of 16
+        "FFFF, 65535", // Max 4-character hex
+        "7FFFFFFF, 2147483647" // Max positive int value
+    })
+    public void
+    testValidHexaToDecimal(String hexInput, int expectedDecimal) {
+        assertEquals(expectedDecimal, HexaDecimalToDecimal.getHexaToDec(hexInput));
+    }
+
+    @ParameterizedTest
+    @CsvSource({
+        "G", // Invalid character
+        "1Z", // Mixed invalid input
+        "123G", // Valid prefix with invalid character
+        "#$%" // Non-hexadecimal symbols
+    })
+    public void
+    testInvalidHexaToDecimal(String invalidHex) {
+        assertThrows(IllegalArgumentException.class, () -> HexaDecimalToDecimal.getHexaToDec(invalidHex));
     }
 }