Skip to content

refactor: Enhance docs, remove main, add tests in AnytoAny #5916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 26, 2024
3 changes: 3 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@
* [BresenhamLine](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/BresenhamLine.java)
* [ConvexHull](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/ConvexHull.java)
* [GrahamScan](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/GrahamScan.java)
* [MidpointEllipse](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/MidpointEllipse.java)
* [Point](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/Point.java)
* graph
* [StronglyConnectedComponentOptimized](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/graph/StronglyConnectedComponentOptimized.java)
Expand Down Expand Up @@ -779,6 +780,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)
Expand Down Expand Up @@ -967,6 +969,7 @@
* [BresenhamLineTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/BresenhamLineTest.java)
* [ConvexHullTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/ConvexHullTest.java)
* [GrahamScanTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java)
* [MidpointEllipseTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/MidpointEllipseTest.java)
* graph
* [StronglyConnectedComponentOptimizedTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/graph/StronglyConnectedComponentOptimizedTest.java)
* greedyalgorithms
Expand Down
83 changes: 58 additions & 25 deletions src/main/java/com/thealgorithms/conversions/AnytoAny.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
48 changes: 48 additions & 0 deletions src/test/java/com/thealgorithms/conversions/AnytoAnyTest.java
Original file line number Diff line number Diff line change
@@ -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");
}
}