Repository: TheAlgorithms/Java
Pull request #5916 (2024-10-20 14:19:32)

refactor: Enhance docs, remove main, add tests in AnytoAny
<!--
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/5916?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 `94.44444%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 66.90%. Comparing base [(`e499d3b`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/e499d3b63e9cb3dc711ddd5d4a2fc3faba9b3d46?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`a61051d`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/a61051de651a3ef19f8b7ea50c7c64428515549a?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/5916?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...n/java/com/thealgorithms/conversions/AnytoAny.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5916?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fconversions%2FAnytoAny.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9jb252ZXJzaW9ucy9Bbnl0b0FueS5qYXZh) | 94.44% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5916?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    #5916      +/-   ##
============================================
+ Coverage     66.80%   66.90%   +0.10%     
- Complexity     4501     4508       +7     
============================================
  Files           611      611              
  Lines         16958    16957       -1     
  Branches       3275     3275              
============================================
+ Hits          11328    11345      +17     
+ Misses         5182     5163      -19     
- Partials        448      449       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5916?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/DIRECTORY.md b/DIRECTORY.md
index 2b47ae08..bb952a26 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -789,6 +789,7 @@
           * conversions
             * [AffineConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/AffineConverterTest.java)
             * [AnyBaseToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/AnyBaseToDecimalTest.java)
+            * [AnytoAnyTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/AnytoAnyTest.java)
             * [BinaryToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java)
             * [BinaryToHexadecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/BinaryToHexadecimalTest.java)
             * [BinaryToOctalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/BinaryToOctalTest.java)
diff --git a/src/main/java/com/thealgorithms/conversions/AnytoAny.java b/src/main/java/com/thealgorithms/conversions/AnytoAny.java
index 801e4930..e7bdbc2b 100644
--- a/src/main/java/com/thealgorithms/conversions/AnytoAny.java
+++ b/src/main/java/com/thealgorithms/conversions/AnytoAny.java
@@ -1,35 +1,68 @@
 package com.thealgorithms.conversions;
 
-import java.util.Scanner;
-
-// given a source number , source base, destination base, this code can give you the destination
-// number.
-// sn ,sb,db ---> ()dn  .   this is what we have to do    .
-
+/**
+ * A utility class for converting numbers from any base to any other base.
+ *
+ * This class provides a method to convert a source number from a given base
+ * to a destination number in another base. Valid bases range from 2 to 10.
+ */
 public final class AnytoAny {
     private AnytoAny() {
     }
 
-    public static void main(String[] args) {
-        Scanner scn = new Scanner(System.in);
-        int sn = scn.nextInt();
-        int sb = scn.nextInt();
-        int db = scn.nextInt();
-        int m = 1;
-        int dec = 0;
-        int dn = 0;
-        while (sn != 0) {
-            dec = dec + (sn % 10) * m;
-            m *= sb;
-            sn /= 10;
+    /**
+     * Converts a number from a source base to a destination base.
+     *
+     * @param sourceNumber The number in the source base (as an integer).
+     * @param sourceBase The base of the source number (between 2 and 10).
+     * @param destBase The base to which the number should be converted (between 2 and 10).
+     * @throws IllegalArgumentException if the bases are not between 2 and 10.
+     * @return The converted number in the destination base (as an integer).
+     */
+    public static int convertBase(int sourceNumber, int sourceBase, int destBase) {
+        if (sourceBase < 2 || sourceBase > 10 || destBase < 2 || destBase > 10) {
+            throw new IllegalArgumentException("Bases must be between 2 and 10.");
+        }
+
+        int decimalValue = toDecimal(sourceNumber, sourceBase);
+        return fromDecimal(decimalValue, destBase);
+    }
+
+    /**
+     * Converts a number from a given base to its decimal representation (base 10).
+     *
+     * @param number The number in the original base.
+     * @param base The base of the given number.
+     * @return The decimal representation of the number.
+     */
+    private static int toDecimal(int number, int base) {
+        int decimalValue = 0;
+        int multiplier = 1;
+
+        while (number != 0) {
+            decimalValue += (number % 10) * multiplier;
+            multiplier *= base;
+            number /= 10;
         }
-        m = 1;
-        while (dec != 0) {
-            dn = dn + (dec % db) * m;
-            m *= 10;
-            dec /= db;
+        return decimalValue;
+    }
+
+    /**
+     * Converts a decimal (base 10) number to a specified base.
+     *
+     * @param decimal The decimal number to convert.
+     * @param base The destination base for conversion.
+     * @return The number in the specified base.
+     */
+    private static int fromDecimal(int decimal, int base) {
+        int result = 0;
+        int multiplier = 1;
+
+        while (decimal != 0) {
+            result += (decimal % base) * multiplier;
+            multiplier *= 10;
+            decimal /= base;
         }
-        System.out.println(dn);
-        scn.close();
+        return result;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/conversions/AnytoAnyTest.java b/src/test/java/com/thealgorithms/conversions/AnytoAnyTest.java
new file mode 100644
index 00000000..cdc01218
--- /dev/null
+++ b/src/test/java/com/thealgorithms/conversions/AnytoAnyTest.java
@@ -0,0 +1,48 @@
+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;
+
+public class AnytoAnyTest {
+
+    @Test
+    void testValidConversions() {
+        assertEquals(101, AnytoAny.convertBase(5, 10, 2), "Decimal 5 should convert to binary 101");
+        assertEquals(2, AnytoAny.convertBase(2, 2, 10), "Binary 10 should convert to decimal 2");
+        assertEquals(6, AnytoAny.convertBase(110, 2, 8), "Binary 110 should convert to octal 6");
+        assertEquals(111, AnytoAny.convertBase(7, 10, 2), "Decimal 7 should convert to binary 111");
+    }
+
+    @Test
+    void testDecimalToBinary() {
+        assertEquals(1101, AnytoAny.convertBase(13, 10, 2), "Decimal 13 should convert to binary 1101");
+        assertEquals(0, AnytoAny.convertBase(0, 10, 2), "Decimal 0 should convert to binary 0");
+    }
+
+    @Test
+    void testBinaryToDecimal() {
+        assertEquals(13, AnytoAny.convertBase(1101, 2, 10), "Binary 1101 should convert to decimal 13");
+        assertEquals(0, AnytoAny.convertBase(0, 2, 10), "Binary 0 should convert to decimal 0");
+    }
+
+    @Test
+    void testOctalToDecimal() {
+        assertEquals(8, AnytoAny.convertBase(10, 8, 10), "Octal 10 should convert to decimal 8");
+        assertEquals(65, AnytoAny.convertBase(101, 8, 10), "Octal 101 should convert to decimal 65");
+    }
+
+    @Test
+    void testInvalidBases() {
+        assertThrows(IllegalArgumentException.class, () -> AnytoAny.convertBase(5, 1, 10), "Source base less than 2 should throw IllegalArgumentException");
+
+        assertThrows(IllegalArgumentException.class, () -> AnytoAny.convertBase(5, 10, 11), "Destination base greater than 10 should throw IllegalArgumentException");
+    }
+
+    @Test
+    void testLargeNumberConversion() {
+        assertEquals(1111101000, AnytoAny.convertBase(1000, 10, 2), "Decimal 1000 should convert to binary 1111101000");
+        assertEquals(1750, AnytoAny.convertBase(1000, 10, 8), "Decimal 1000 should convert to octal 1750");
+    }
+}