Repository: TheAlgorithms/Java
Pull request #5339 (2024-08-17 10:03:11)

refactor: `DecimalToBinary`
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/5339?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 43.02%. Comparing base [(`7c58b19`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/7c58b190c8ebd375cb87f873e0acfdc21282d7e8?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`ca5c11b`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/ca5c11bebeaf747ea539608988641353be86506d?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    #5339      +/-   ##
============================================
+ Coverage     42.87%   43.02%   +0.14%     
- Complexity     2661     2666       +5     
============================================
  Files           522      522              
  Lines         15483    15474       -9     
  Branches       2950     2950              
============================================
+ Hits           6638     6657      +19     
+ Misses         8560     8533      -27     
+ Partials        285      284       -1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5339?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/DecimalToBinary.java b/src/main/java/com/thealgorithms/conversions/DecimalToBinary.java
index 471724ff..e8d033e0 100644
--- a/src/main/java/com/thealgorithms/conversions/DecimalToBinary.java
+++ b/src/main/java/com/thealgorithms/conversions/DecimalToBinary.java
@@ -1,63 +1,49 @@
 package com.thealgorithms.conversions;
 
-import java.util.Scanner;
-
 /**
- * This class converts a Decimal number to a Binary number
+ * This class provides methods to convert a decimal number to a binary number.
  */
 final class DecimalToBinary {
+    private static final int BINARY_BASE = 2;
+    private static final int DECIMAL_MULTIPLIER = 10;
+
     private DecimalToBinary() {
     }
 
     /**
-     * Main Method
-     *
-     * @param args Command Line Arguments
+     * Converts a decimal number to a binary number using a conventional algorithm.
+     * @param decimalNumber the decimal number to convert
+     * @return the binary representation of the decimal number
      */
-    public static void main(String[] args) {
-        conventionalConversion();
-        bitwiseConversion();
-    }
+    public static int convertUsingConventionalAlgorithm(int decimalNumber) {
+        int binaryNumber = 0;
+        int position = 1;
 
-    /**
-     * This method converts a decimal number to a binary number using a
-     * conventional algorithm.
-     */
-    public static void conventionalConversion() {
-        int n;
-        int b = 0;
-        int c = 0;
-        int d;
-        Scanner input = new Scanner(System.in);
-        System.out.printf("Conventional conversion.%n Enter the decimal number: ");
-        n = input.nextInt();
-        while (n != 0) {
-            d = n % 2;
-            b = b + d * (int) Math.pow(10, c++);
-            n /= 2;
-        } // converting decimal to binary
-        System.out.println("\tBinary number: " + b);
-        input.close();
+        while (decimalNumber > 0) {
+            int remainder = decimalNumber % BINARY_BASE;
+            binaryNumber += remainder * position;
+            position *= DECIMAL_MULTIPLIER;
+            decimalNumber /= BINARY_BASE;
+        }
+
+        return binaryNumber;
     }
 
     /**
-     * This method converts a decimal number to a binary number using a bitwise
-     * algorithm
+     * Converts a decimal number to a binary number using a bitwise algorithm.
+     * @param decimalNumber the decimal number to convert
+     * @return the binary representation of the decimal number
      */
-    public static void bitwiseConversion() {
-        int n;
-        int b = 0;
-        int c = 0;
-        int d;
-        Scanner input = new Scanner(System.in);
-        System.out.printf("Bitwise conversion.%n Enter the decimal number: ");
-        n = input.nextInt();
-        while (n != 0) {
-            d = (n & 1);
-            b += d * (int) Math.pow(10, c++);
-            n >>= 1;
+    public static int convertUsingBitwiseAlgorithm(int decimalNumber) {
+        int binaryNumber = 0;
+        int position = 1;
+
+        while (decimalNumber > 0) {
+            int leastSignificantBit = decimalNumber & 1;
+            binaryNumber += leastSignificantBit * position;
+            position *= DECIMAL_MULTIPLIER;
+            decimalNumber >>= 1;
         }
-        System.out.println("\tBinary number: " + b);
-        input.close();
+        return binaryNumber;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/conversions/DecimalToBinaryTest.java b/src/test/java/com/thealgorithms/conversions/DecimalToBinaryTest.java
new file mode 100644
index 00000000..f194951e
--- /dev/null
+++ b/src/test/java/com/thealgorithms/conversions/DecimalToBinaryTest.java
@@ -0,0 +1,21 @@
+package com.thealgorithms.conversions;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+public class DecimalToBinaryTest {
+
+    @ParameterizedTest
+    @CsvSource({"0, 0", "1, 1", "2, 10", "5, 101", "10, 1010", "15, 1111", "100, 1100100"})
+    void testConvertUsingConventionalAlgorithm(int decimal, int expectedBinary) {
+        assertEquals(expectedBinary, DecimalToBinary.convertUsingConventionalAlgorithm(decimal));
+    }
+
+    @ParameterizedTest
+    @CsvSource({"0, 0", "1, 1", "2, 10", "5, 101", "10, 1010", "15, 1111", "100, 1100100"})
+    void testConvertUsingBitwiseAlgorithm(int decimal, int expectedBinary) {
+        assertEquals(expectedBinary, DecimalToBinary.convertUsingBitwiseAlgorithm(decimal));
+    }
+}