Skip to content

Commit 687d251

Browse files
authored
Enhance docs, remove main, add tests in AnytoAny (#5916)
1 parent 4e46002 commit 687d251

File tree

3 files changed

+107
-25
lines changed

3 files changed

+107
-25
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,7 @@
789789
* conversions
790790
* [AffineConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/AffineConverterTest.java)
791791
* [AnyBaseToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/AnyBaseToDecimalTest.java)
792+
* [AnytoAnyTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/AnytoAnyTest.java)
792793
* [BinaryToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java)
793794
* [BinaryToHexadecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/BinaryToHexadecimalTest.java)
794795
* [BinaryToOctalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/BinaryToOctalTest.java)
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,68 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
5-
// given a source number , source base, destination base, this code can give you the destination
6-
// number.
7-
// sn ,sb,db ---> ()dn . this is what we have to do .
8-
3+
/**
4+
* A utility class for converting numbers from any base to any other base.
5+
*
6+
* This class provides a method to convert a source number from a given base
7+
* to a destination number in another base. Valid bases range from 2 to 10.
8+
*/
99
public final class AnytoAny {
1010
private AnytoAny() {
1111
}
1212

13-
public static void main(String[] args) {
14-
Scanner scn = new Scanner(System.in);
15-
int sn = scn.nextInt();
16-
int sb = scn.nextInt();
17-
int db = scn.nextInt();
18-
int m = 1;
19-
int dec = 0;
20-
int dn = 0;
21-
while (sn != 0) {
22-
dec = dec + (sn % 10) * m;
23-
m *= sb;
24-
sn /= 10;
13+
/**
14+
* Converts a number from a source base to a destination base.
15+
*
16+
* @param sourceNumber The number in the source base (as an integer).
17+
* @param sourceBase The base of the source number (between 2 and 10).
18+
* @param destBase The base to which the number should be converted (between 2 and 10).
19+
* @throws IllegalArgumentException if the bases are not between 2 and 10.
20+
* @return The converted number in the destination base (as an integer).
21+
*/
22+
public static int convertBase(int sourceNumber, int sourceBase, int destBase) {
23+
if (sourceBase < 2 || sourceBase > 10 || destBase < 2 || destBase > 10) {
24+
throw new IllegalArgumentException("Bases must be between 2 and 10.");
25+
}
26+
27+
int decimalValue = toDecimal(sourceNumber, sourceBase);
28+
return fromDecimal(decimalValue, destBase);
29+
}
30+
31+
/**
32+
* Converts a number from a given base to its decimal representation (base 10).
33+
*
34+
* @param number The number in the original base.
35+
* @param base The base of the given number.
36+
* @return The decimal representation of the number.
37+
*/
38+
private static int toDecimal(int number, int base) {
39+
int decimalValue = 0;
40+
int multiplier = 1;
41+
42+
while (number != 0) {
43+
decimalValue += (number % 10) * multiplier;
44+
multiplier *= base;
45+
number /= 10;
2546
}
26-
m = 1;
27-
while (dec != 0) {
28-
dn = dn + (dec % db) * m;
29-
m *= 10;
30-
dec /= db;
47+
return decimalValue;
48+
}
49+
50+
/**
51+
* Converts a decimal (base 10) number to a specified base.
52+
*
53+
* @param decimal The decimal number to convert.
54+
* @param base The destination base for conversion.
55+
* @return The number in the specified base.
56+
*/
57+
private static int fromDecimal(int decimal, int base) {
58+
int result = 0;
59+
int multiplier = 1;
60+
61+
while (decimal != 0) {
62+
result += (decimal % base) * multiplier;
63+
multiplier *= 10;
64+
decimal /= base;
3165
}
32-
System.out.println(dn);
33-
scn.close();
66+
return result;
3467
}
3568
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.thealgorithms.conversions;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class AnytoAnyTest {
9+
10+
@Test
11+
void testValidConversions() {
12+
assertEquals(101, AnytoAny.convertBase(5, 10, 2), "Decimal 5 should convert to binary 101");
13+
assertEquals(2, AnytoAny.convertBase(2, 2, 10), "Binary 10 should convert to decimal 2");
14+
assertEquals(6, AnytoAny.convertBase(110, 2, 8), "Binary 110 should convert to octal 6");
15+
assertEquals(111, AnytoAny.convertBase(7, 10, 2), "Decimal 7 should convert to binary 111");
16+
}
17+
18+
@Test
19+
void testDecimalToBinary() {
20+
assertEquals(1101, AnytoAny.convertBase(13, 10, 2), "Decimal 13 should convert to binary 1101");
21+
assertEquals(0, AnytoAny.convertBase(0, 10, 2), "Decimal 0 should convert to binary 0");
22+
}
23+
24+
@Test
25+
void testBinaryToDecimal() {
26+
assertEquals(13, AnytoAny.convertBase(1101, 2, 10), "Binary 1101 should convert to decimal 13");
27+
assertEquals(0, AnytoAny.convertBase(0, 2, 10), "Binary 0 should convert to decimal 0");
28+
}
29+
30+
@Test
31+
void testOctalToDecimal() {
32+
assertEquals(8, AnytoAny.convertBase(10, 8, 10), "Octal 10 should convert to decimal 8");
33+
assertEquals(65, AnytoAny.convertBase(101, 8, 10), "Octal 101 should convert to decimal 65");
34+
}
35+
36+
@Test
37+
void testInvalidBases() {
38+
assertThrows(IllegalArgumentException.class, () -> AnytoAny.convertBase(5, 1, 10), "Source base less than 2 should throw IllegalArgumentException");
39+
40+
assertThrows(IllegalArgumentException.class, () -> AnytoAny.convertBase(5, 10, 11), "Destination base greater than 10 should throw IllegalArgumentException");
41+
}
42+
43+
@Test
44+
void testLargeNumberConversion() {
45+
assertEquals(1111101000, AnytoAny.convertBase(1000, 10, 2), "Decimal 1000 should convert to binary 1111101000");
46+
assertEquals(1750, AnytoAny.convertBase(1000, 10, 8), "Decimal 1000 should convert to octal 1750");
47+
}
48+
}

0 commit comments

Comments
 (0)