Repository: TheAlgorithms/Java
Pull request #5762 (2024-10-13 07:35:14)

Refactor BCD Conversion docs and add more 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/5762?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `92.30769%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 63.96%. Comparing base [(`6682c7c`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/6682c7c76e2bf016e2e7b51a6027ac30d354b13e?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`390b83d`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/390b83db12277e4fce2b658392d80fc02939009f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5762?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...m/thealgorithms/bitmanipulation/BcdConversion.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5762?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fbitmanipulation%2FBcdConversion.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9iaXRtYW5pcHVsYXRpb24vQmNkQ29udmVyc2lvbi5qYXZh) | 92.30% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5762?src=pr&el=tree&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    #5762   +/-   ##
=========================================
  Coverage     63.96%   63.96%           
- Complexity     4172     4174    +2     
=========================================
  Files           583      583           
  Lines         16320    16324    +4     
  Branches       3150     3151    +1     
=========================================
+ Hits          10439    10442    +3     
  Misses         5456     5456           
- Partials        425      426    +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5762?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/bitmanipulation/BcdConversion.java b/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java
index 3cf84118..e6bd3572 100644
--- a/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java
+++ b/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java
@@ -1,56 +1,80 @@
 package com.thealgorithms.bitmanipulation;
 
 /**
- * This class provides methods to convert between BCD (Binary-Coded Decimal) and binary.
+ * This class provides methods to convert between BCD (Binary-Coded Decimal) and decimal numbers.
  *
- * Binary-Coded Decimal (BCD) is a class of binary encodings of decimal numbers where each decimal digit is represented by a fixed number of binary digits, usually four or eight.
+ * BCD is a class of binary encodings of decimal numbers where each decimal digit is represented by a fixed number of binary digits, usually four or eight.
  *
  * For more information, refer to the
  * <a href="https://en.wikipedia.org/wiki/Binary-coded_decimal">Binary-Coded Decimal</a> Wikipedia page.
  *
  * <b>Example usage:</b>
  * <pre>
- * int binary = BcdConversion.bcdToBinary(0x1234);
- * System.out.println("BCD 0x1234 to binary: " + binary); // Output: 1234
+ * int decimal = BcdConversion.bcdToDecimal(0x1234);
+ * System.out.println("BCD 0x1234 to decimal: " + decimal); // Output: 1234
  *
- * int bcd = BcdConversion.binaryToBcd(1234);
- * System.out.println("Binary 1234 to BCD: " + Integer.toHexString(bcd)); // Output: 0x1234
+ * int bcd = BcdConversion.decimalToBcd(1234);
+ * System.out.println("Decimal 1234 to BCD: " + Integer.toHexString(bcd)); // Output: 0x1234
  * </pre>
  */
 public final class BcdConversion {
     private BcdConversion() {
     }
+
     /**
-     * Converts a BCD (Binary-Coded Decimal) number to binary.
+     * Converts a BCD (Binary-Coded Decimal) number to a decimal number.
+     * <p>Steps:
+     * <p>1. Validate the BCD number to ensure all digits are between 0 and 9.
+     * <p>2. Extract the last 4 bits (one BCD digit) from the BCD number.
+     * <p>3. Multiply the extracted digit by the corresponding power of 10 and add it to the decimal number.
+     * <p>4. Shift the BCD number right by 4 bits to process the next BCD digit.
+     * <p>5. Repeat steps 1-4 until the BCD number is zero.
      *
      * @param bcd The BCD number.
-     * @return The corresponding binary number.
+     * @return The corresponding decimal number.
+     * @throws IllegalArgumentException if the BCD number contains invalid digits.
      */
-    public static int bcdToBinary(int bcd) {
-        int binary = 0;
+    public static int bcdToDecimal(int bcd) {
+        int decimal = 0;
         int multiplier = 1;
+
+        // Validate BCD digits
         while (bcd > 0) {
-            int digit = bcd & 0xF; // Extract the last 4 bits (one BCD digit)
-            binary += digit * multiplier;
+            int digit = bcd & 0xF;
+            if (digit > 9) {
+                throw new IllegalArgumentException("Invalid BCD digit: " + digit);
+            }
+            decimal += digit * multiplier;
             multiplier *= 10;
-            bcd >>= 4; // Shift right by 4 bits to process the next BCD digit
+            bcd >>= 4;
         }
-        return binary;
+        return decimal;
     }
 
     /**
-     * Converts a binary number to BCD (Binary-Coded Decimal).
+     * Converts a decimal number to BCD (Binary-Coded Decimal).
+     * <p>Steps:
+     * <p>1. Check if the decimal number is within the valid range for BCD (0 to 9999).
+     * <p>2. Extract the last decimal digit from the decimal number.
+     * <p>3. Shift the digit to the correct BCD position and add it to the BCD number.
+     * <p>4. Remove the last decimal digit from the decimal number.
+     * <p>5. Repeat steps 2-4 until the decimal number is zero.
      *
-     * @param binary The binary number.
+     * @param decimal The decimal number.
      * @return The corresponding BCD number.
+     * @throws IllegalArgumentException if the decimal number is greater than 9999.
      */
-    public static int binaryToBcd(int binary) {
+    public static int decimalToBcd(int decimal) {
+        if (decimal < 0 || decimal > 9999) {
+            throw new IllegalArgumentException("Value out of bounds for BCD representation: " + decimal);
+        }
+
         int bcd = 0;
         int shift = 0;
-        while (binary > 0) {
-            int digit = binary % 10; // Extract the last decimal digit
-            bcd |= (digit << (shift * 4)); // Shift the digit to the correct BCD position
-            binary /= 10; // Remove the last decimal digit
+        while (decimal > 0) {
+            int digit = decimal % 10;
+            bcd |= (digit << (shift * 4));
+            decimal /= 10;
             shift++;
         }
         return bcd;


Test patch:
diff --git a/src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java b/src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java
index 85221d92..727b07eb 100644
--- a/src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java
+++ b/src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java
@@ -1,65 +1,85 @@
 package com.thealgorithms.bitmanipulation;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import org.junit.jupiter.api.Test;
 
-/**
- * Unit tests for the BcdConversion class.
- */
 public class BcdConversionTest {
 
-    /**
-     * Test the bcdToBinary method with a BCD number.
-     */
     @Test
-    public void testBcdToBinary() {
-        int binary = BcdConversion.bcdToBinary(0x1234);
-        assertEquals(1234, binary); // BCD 0x1234 should convert to binary 1234
+    public void testBcdToDecimal() {
+        int decimal = BcdConversion.bcdToDecimal(0x1234);
+        assertEquals(1234, decimal); // BCD 0x1234 should convert to decimal 1234
     }
 
-    /**
-     * Test the binaryToBcd method with a binary number.
-     */
     @Test
-    public void testBinaryToBcd() {
-        int bcd = BcdConversion.binaryToBcd(1234);
-        assertEquals(0x1234, bcd); // Binary 1234 should convert to BCD 0x1234
+    public void testDecimalToBcd() {
+        int bcd = BcdConversion.decimalToBcd(1234);
+        assertEquals(0x1234, bcd); // Decimal 1234 should convert to BCD 0x1234
     }
 
-    /**
-     * Test the bcdToBinary method with zero.
-     */
     @Test
-    public void testBcdToBinaryZero() {
-        int binary = BcdConversion.bcdToBinary(0x0);
-        assertEquals(0, binary); // BCD 0x0 should convert to binary 0
+    public void testBcdToDecimalZero() {
+        int decimal = BcdConversion.bcdToDecimal(0x0);
+        assertEquals(0, decimal); // BCD 0x0 should convert to decimal 0
     }
 
-    /**
-     * Test the binaryToBcd method with zero.
-     */
     @Test
-    public void testBinaryToBcdZero() {
-        int bcd = BcdConversion.binaryToBcd(0);
-        assertEquals(0x0, bcd); // Binary 0 should convert to BCD 0x0
+    public void testDecimalToBcdZero() {
+        int bcd = BcdConversion.decimalToBcd(0);
+        assertEquals(0x0, bcd); // Decimal 0 should convert to BCD 0x0
     }
 
-    /**
-     * Test the bcdToBinary method with a single digit BCD number.
-     */
     @Test
-    public void testBcdToBinarySingleDigit() {
-        int binary = BcdConversion.bcdToBinary(0x7);
-        assertEquals(7, binary); // BCD 0x7 should convert to binary 7
+    public void testBcdToDecimalSingleDigit() {
+        int decimal = BcdConversion.bcdToDecimal(0x7);
+        assertEquals(7, decimal); // BCD 0x7 should convert to decimal 7
     }
 
-    /**
-     * Test the binaryToBcd method with a single digit binary number.
-     */
     @Test
-    public void testBinaryToBcdSingleDigit() {
-        int bcd = BcdConversion.binaryToBcd(7);
-        assertEquals(0x7, bcd); // Binary 7 should convert to BCD 0x7
+    public void testDecimalToBcdSingleDigit() {
+        int bcd = BcdConversion.decimalToBcd(7);
+        assertEquals(0x7, bcd); // Decimal 7 should convert to BCD 0x7
+    }
+
+    @Test
+    public void testBcdToDecimalMaxValue() {
+        int decimal = BcdConversion.bcdToDecimal(0x9999);
+        assertEquals(9999, decimal); // BCD 0x9999 should convert to decimal 9999
+    }
+
+    @Test
+    public void testDecimalToBcdMaxValue() {
+        int bcd = BcdConversion.decimalToBcd(9999);
+        assertEquals(0x9999, bcd); // Decimal 9999 should convert to BCD 0x9999
+    }
+
+    @Test
+    public void testBcdToDecimalInvalidHighDigit() {
+        // Testing invalid BCD input where one of the digits is > 9
+        assertThrows(IllegalArgumentException.class, () -> {
+            BcdConversion.bcdToDecimal(0x123A); // Invalid BCD, 'A' is not a valid digit
+        });
+    }
+
+    @Test
+    public void testDecimalToBcdInvalidValue() {
+        // Testing conversion for numbers greater than 9999, which cannot be represented in BCD
+        assertThrows(IllegalArgumentException.class, () -> {
+            BcdConversion.decimalToBcd(10000); // 10000 is too large for BCD representation
+        });
+    }
+
+    @Test
+    public void testBcdToDecimalLeadingZeroes() {
+        int decimal = BcdConversion.bcdToDecimal(0x0234);
+        assertEquals(234, decimal); // BCD 0x0234 should convert to decimal 234, ignoring leading zero
+    }
+
+    @Test
+    public void testDecimalToBcdLeadingZeroes() {
+        int bcd = BcdConversion.decimalToBcd(234);
+        assertEquals(0x0234, bcd); // Decimal 234 should convert to BCD 0x0234
     }
 }