mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 09:06:51 +08:00
Enhance docs, remove main, add tests in AnytoAny (#5916)
This commit is contained in:
@ -789,6 +789,7 @@
|
|||||||
* conversions
|
* conversions
|
||||||
* [AffineConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/AffineConverterTest.java)
|
* [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)
|
* [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)
|
* [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)
|
* [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)
|
* [BinaryToOctalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/BinaryToOctalTest.java)
|
||||||
|
@ -1,35 +1,68 @@
|
|||||||
package com.thealgorithms.conversions;
|
package com.thealgorithms.conversions;
|
||||||
|
|
||||||
import java.util.Scanner;
|
/**
|
||||||
|
* A utility class for converting numbers from any base to any other base.
|
||||||
// given a source number , source base, destination base, this code can give you the destination
|
*
|
||||||
// number.
|
* This class provides a method to convert a source number from a given base
|
||||||
// sn ,sb,db ---> ()dn . this is what we have to do .
|
* to a destination number in another base. Valid bases range from 2 to 10.
|
||||||
|
*/
|
||||||
public final class AnytoAny {
|
public final class AnytoAny {
|
||||||
private AnytoAny() {
|
private AnytoAny() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
/**
|
||||||
Scanner scn = new Scanner(System.in);
|
* Converts a number from a source base to a destination base.
|
||||||
int sn = scn.nextInt();
|
*
|
||||||
int sb = scn.nextInt();
|
* @param sourceNumber The number in the source base (as an integer).
|
||||||
int db = scn.nextInt();
|
* @param sourceBase The base of the source number (between 2 and 10).
|
||||||
int m = 1;
|
* @param destBase The base to which the number should be converted (between 2 and 10).
|
||||||
int dec = 0;
|
* @throws IllegalArgumentException if the bases are not between 2 and 10.
|
||||||
int dn = 0;
|
* @return The converted number in the destination base (as an integer).
|
||||||
while (sn != 0) {
|
*/
|
||||||
dec = dec + (sn % 10) * m;
|
public static int convertBase(int sourceNumber, int sourceBase, int destBase) {
|
||||||
m *= sb;
|
if (sourceBase < 2 || sourceBase > 10 || destBase < 2 || destBase > 10) {
|
||||||
sn /= 10;
|
throw new IllegalArgumentException("Bases must be between 2 and 10.");
|
||||||
}
|
}
|
||||||
m = 1;
|
|
||||||
while (dec != 0) {
|
int decimalValue = toDecimal(sourceNumber, sourceBase);
|
||||||
dn = dn + (dec % db) * m;
|
return fromDecimal(decimalValue, destBase);
|
||||||
m *= 10;
|
}
|
||||||
dec /= db;
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
}
|
}
|
||||||
System.out.println(dn);
|
return decimalValue;
|
||||||
scn.close();
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user