Repository: TheAlgorithms/Java
Pull request #5330 (2024-08-15 21:53:36)

refactor: `BinaryToDecimal`
Refactoring BinaryToDecimal.
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/5330?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.56%. 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 [(`6ccca5c`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/6ccca5ca7557a375f2ea5609afa25fb7d0ef7d75?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    #5330   +/-   ##
=========================================
  Coverage     42.56%   42.56%           
  Complexity     2640     2640           
=========================================
  Files           522      522           
  Lines         15493    15490    -3     
  Branches       2948     2949    +1     
=========================================
  Hits           6594     6594           
+ Misses         8616     8612    -4     
- Partials        283      284    +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5330?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/BinaryToDecimal.java b/src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java
index 67b815ab..36c0790e 100644
--- a/src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java
+++ b/src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java
@@ -1,37 +1,33 @@
 package com.thealgorithms.conversions;
 
-import java.util.Scanner;
-
 /**
  * This class converts a Binary number to a Decimal number
  */
 final class BinaryToDecimal {
-    private BinaryToDecimal() {
-    }
+    private static final int BINARY_BASE = 2;
 
-    public static long binaryToDecimal(long binNum) {
-        long binCopy;
-        long d;
-        long s = 0;
-        long power = 0;
-        binCopy = binNum;
-        while (binCopy != 0) {
-            d = binCopy % 10;
-            s += d * (long) Math.pow(2, power++);
-            binCopy /= 10;
-        }
-        return s;
+    private BinaryToDecimal() {
     }
 
     /**
-     * Main Method
+     * Converts a binary number to its decimal equivalent.
      *
-     * @param args Command line arguments
+     * @param binaryNumber The binary number to convert.
+     * @return The decimal equivalent of the binary number.
+     * @throws IllegalArgumentException If the binary number contains digits other than 0 and 1.
      */
-    public static void main(String[] args) {
-        Scanner sc = new Scanner(System.in);
-        System.out.print("Binary number: ");
-        System.out.println("Decimal equivalent:" + binaryToDecimal(sc.nextLong()));
-        sc.close();
+    public static long binaryToDecimal(long binaryNumber) {
+        long decimalValue = 0;
+        long power = 0;
+
+        while (binaryNumber != 0) {
+            long digit = binaryNumber % 10;
+            if (digit > 1) {
+                throw new IllegalArgumentException("Incorrect binary digit: " + digit);
+            }
+            decimalValue += (long) (digit * Math.pow(BINARY_BASE, power++));
+            binaryNumber /= 10;
+        }
+        return decimalValue;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java b/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java
index 2471f919..9045d100 100644
--- a/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java
+++ b/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java
@@ -1,8 +1,11 @@
 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 BinaryToDecimalTest {
 
@@ -30,4 +33,10 @@ public class BinaryToDecimalTest {
         assertEquals(262144L, BinaryToDecimal.binaryToDecimal(1000000000000000000L));
         assertEquals(524287L, BinaryToDecimal.binaryToDecimal(1111111111111111111L));
     }
+
+    @ParameterizedTest
+    @CsvSource({"2", "1234", "11112", "101021"})
+    void testNotCorrectBinaryInput(long binaryNumber) {
+        assertThrows(IllegalArgumentException.class, () -> BinaryToDecimal.binaryToDecimal(binaryNumber));
+    }
 }