Repository: TheAlgorithms/Java
Pull request #5377 (2024-08-23 21:45:14)

refactor: `HexToOct`
Improving readability.
Adding 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/5377?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 46.31%. Comparing base [(`ce4eb55`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/ce4eb55e0e7c5216e2cba53217627f6bbe8b5083?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`6fabf24`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/6fabf249045ee16e632453f49c01ebc22a970ad4?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    #5377      +/-   ##
============================================
+ Coverage     46.26%   46.31%   +0.04%     
- Complexity     2866     2867       +1     
============================================
  Files           525      525              
  Lines         15410    15404       -6     
  Branches       2916     2916              
============================================
+ Hits           7130     7134       +4     
+ Misses         7981     7972       -9     
+ Partials        299      298       -1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5377?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/HexToOct.java b/src/main/java/com/thealgorithms/conversions/HexToOct.java
index 97a8be16..d3a672d3 100644
--- a/src/main/java/com/thealgorithms/conversions/HexToOct.java
+++ b/src/main/java/com/thealgorithms/conversions/HexToOct.java
@@ -1,7 +1,5 @@
 package com.thealgorithms.conversions;
 
-import java.util.Scanner;
-
 /**
  * Converts any Hexadecimal Number to Octal
  *
@@ -12,64 +10,53 @@ public final class HexToOct {
     }
 
     /**
-     * This method converts a Hexadecimal number to a decimal number
+     * Converts a Hexadecimal number to a Decimal number.
      *
-     * @param s The Hexadecimal Number
-     * @return The Decimal number
+     * @param hex The Hexadecimal number as a String.
+     * @return The Decimal equivalent as an integer.
      */
-    public static int hex2decimal(String s) {
-        String str = "0123456789ABCDEF";
-        s = s.toUpperCase();
-        int val = 0;
-        for (int i = 0; i < s.length(); i++) {
-            char a = s.charAt(i);
-            int n = str.indexOf(a);
-            val = 16 * val + n;
+    public static int hexToDecimal(String hex) {
+        String hexDigits = "0123456789ABCDEF";
+        hex = hex.toUpperCase();
+        int decimalValue = 0;
+
+        for (int i = 0; i < hex.length(); i++) {
+            char hexChar = hex.charAt(i);
+            int digitValue = hexDigits.indexOf(hexChar);
+            decimalValue = 16 * decimalValue + digitValue;
         }
-        return val;
+
+        return decimalValue;
     }
 
     /**
-     * This method converts a Decimal number to a octal number
+     * Converts a Decimal number to an Octal number.
      *
-     * @param q The Decimal Number
-     * @return The Octal number
+     * @param decimal The Decimal number as an integer.
+     * @return The Octal equivalent as an integer.
      */
-    public static int decimal2octal(int q) {
-        int now;
-        int i = 1;
-        int octnum = 0;
-        while (q > 0) {
-            now = q % 8;
-            octnum = (now * (int) (Math.pow(10, i))) + octnum;
-            q /= 8;
-            i++;
+    public static int decimalToOctal(int decimal) {
+        int octalValue = 0;
+        int placeValue = 1;
+
+        while (decimal > 0) {
+            int remainder = decimal % 8;
+            octalValue += remainder * placeValue;
+            decimal /= 8;
+            placeValue *= 10;
         }
-        octnum /= 10;
-        return octnum;
+
+        return octalValue;
     }
 
     /**
-     * Main method that gets the hex input from user and converts it into octal.
+     * Converts a Hexadecimal number to an Octal number.
      *
-     * @param args arguments
+     * @param hex The Hexadecimal number as a String.
+     * @return The Octal equivalent as an integer.
      */
-    public static void main(String[] args) {
-        String hexadecnum;
-        int decnum;
-        int octalnum;
-        Scanner scan = new Scanner(System.in);
-
-        System.out.print("Enter Hexadecimal Number : ");
-        hexadecnum = scan.nextLine();
-
-        // first convert hexadecimal to decimal
-        decnum = hex2decimal(hexadecnum); // Pass the string to the hex2decimal function and get the decimal form in
-        // variable decnum
-
-        // convert decimal to octal
-        octalnum = decimal2octal(decnum);
-        System.out.println("Number in octal: " + octalnum);
-        scan.close();
+    public static int hexToOctal(String hex) {
+        int decimalValue = hexToDecimal(hex);
+        return decimalToOctal(decimalValue);
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/conversions/HexToOctTest.java b/src/test/java/com/thealgorithms/conversions/HexToOctTest.java
index 5924aa31..b3f94b19 100644
--- a/src/test/java/com/thealgorithms/conversions/HexToOctTest.java
+++ b/src/test/java/com/thealgorithms/conversions/HexToOctTest.java
@@ -5,10 +5,29 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import org.junit.jupiter.api.Test;
 
 public class HexToOctTest {
+    @Test
+    public void testHexToDecimal() {
+        assertEquals(255, HexToOct.hexToDecimal("FF"));
+        assertEquals(16, HexToOct.hexToDecimal("10"));
+        assertEquals(0, HexToOct.hexToDecimal("0"));
+        assertEquals(4095, HexToOct.hexToDecimal("FFF"));
+    }
+
+    @Test
+    public void testDecimalToOctal() {
+        assertEquals(110, HexToOct.decimalToOctal(HexToOct.hexToDecimal("48")));
+        assertEquals(255, HexToOct.decimalToOctal(HexToOct.hexToDecimal("AD")));
+        assertEquals(377, HexToOct.decimalToOctal(255));
+        assertEquals(20, HexToOct.decimalToOctal(16));
+        assertEquals(0, HexToOct.decimalToOctal(0));
+        assertEquals(7777, HexToOct.decimalToOctal(4095));
+    }
 
     @Test
-    public void testHexToOct() {
-        assertEquals(110, HexToOct.decimal2octal(HexToOct.hex2decimal("48")));
-        assertEquals(255, HexToOct.decimal2octal(HexToOct.hex2decimal("AD")));
+    public void testHexToOctal() {
+        assertEquals(377, HexToOct.hexToOctal("FF"));
+        assertEquals(20, HexToOct.hexToOctal("10"));
+        assertEquals(0, HexToOct.hexToOctal("0"));
+        assertEquals(7777, HexToOct.hexToOctal("FFF"));
     }
 }