Repository: TheAlgorithms/Java
Pull request #5332 (2024-08-15 22:09:19)

refactor: `DecimalToOctal`
Refactoring BinaryToHexadecimal.
Adding tests for incorrect cases.

<!--
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/5332?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 42.63%. Comparing base [(`a84a4a2`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/a84a4a29ed64c6e701859339d0c69d1cd5d4982c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`db1f444`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/db1f444f938baada14555a25dbe391d7546b856d?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    #5332      +/-   ##
============================================
+ Coverage     42.56%   42.63%   +0.07%     
- Complexity     2640     2643       +3     
============================================
  Files           522      522              
  Lines         15493    15491       -2     
  Branches       2948     2948              
============================================
+ Hits           6594     6605      +11     
+ Misses         8616     8603      -13     
  Partials        283      283              
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5332?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/DecimalToOctal.java b/src/main/java/com/thealgorithms/conversions/DecimalToOctal.java
index 4bc3a6e7..75687fc5 100644
--- a/src/main/java/com/thealgorithms/conversions/DecimalToOctal.java
+++ b/src/main/java/com/thealgorithms/conversions/DecimalToOctal.java
@@ -1,38 +1,38 @@
 package com.thealgorithms.conversions;
 
-import java.util.Scanner;
-
 /**
  * This class converts Decimal numbers to Octal Numbers
  */
 public final class DecimalToOctal {
+    private static final int OCTAL_BASE = 8;
+    private static final int INITIAL_OCTAL_VALUE = 0;
+    private static final int INITIAL_PLACE_VALUE = 1;
+
     private DecimalToOctal() {
     }
 
     /**
-     * Main Method
+     * Converts a decimal number to its octal equivalent.
      *
-     * @param args Command line Arguments
+     * @param decimal The decimal number to convert.
+     * @return The octal equivalent as an integer.
+     * @throws IllegalArgumentException if the decimal number is negative.
      */
+    public static int convertToOctal(int decimal) {
+        if (decimal < 0) {
+            throw new IllegalArgumentException("Decimal number cannot be negative.");
+        }
+
+        int octal = INITIAL_OCTAL_VALUE;
+        int placeValue = INITIAL_PLACE_VALUE;
 
-    // enter in a decimal value to get Octal output
-    public static void main(String[] args) {
-        Scanner sc = new Scanner(System.in);
-        int n;
-        int k;
-        int d;
-        int s = 0;
-        int c = 0;
-        System.out.print("Decimal number: ");
-        n = sc.nextInt();
-        k = n;
-        while (k != 0) {
-            d = k % 8;
-            s += d * (int) Math.pow(10, c++);
-            k /= 8;
+        while (decimal != 0) {
+            int remainder = decimal % OCTAL_BASE;
+            octal += remainder * placeValue;
+            decimal /= OCTAL_BASE;
+            placeValue *= 10;
         }
 
-        System.out.println("Octal equivalent:" + s);
-        sc.close();
+        return octal;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/conversions/DecimalToOctalTest.java b/src/test/java/com/thealgorithms/conversions/DecimalToOctalTest.java
new file mode 100644
index 00000000..7ff274f0
--- /dev/null
+++ b/src/test/java/com/thealgorithms/conversions/DecimalToOctalTest.java
@@ -0,0 +1,21 @@
+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;
+
+class DecimalToOctalTest {
+    @ParameterizedTest
+    @CsvSource({"0, 0", "7, 7", "8, 10", "10, 12", "64, 100", "83, 123", "7026, 15562"})
+    void testConvertToOctal(int decimal, int expectedOctal) {
+        assertEquals(expectedOctal, DecimalToOctal.convertToOctal(decimal));
+    }
+
+    @Test
+    void testConvertToOctalNegativeNumber() {
+        assertThrows(IllegalArgumentException.class, () -> DecimalToOctal.convertToOctal(-10));
+    }
+}