mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 17:56:02 +08:00
refactor: DecimalToAnyBase
(#5343)
This commit is contained in:
@ -1,69 +1,69 @@
|
|||||||
package com.thealgorithms.conversions;
|
package com.thealgorithms.conversions;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Class that provides methods to convert a decimal number to a string representation
|
||||||
|
* in any specified base between 2 and 36.
|
||||||
|
*
|
||||||
* @author Varun Upadhyay (<a href="https://github.com/varunu28">...</a>)
|
* @author Varun Upadhyay (<a href="https://github.com/varunu28">...</a>)
|
||||||
*/
|
*/
|
||||||
// Driver Program
|
|
||||||
public final class DecimalToAnyBase {
|
public final class DecimalToAnyBase {
|
||||||
|
private static final int MIN_BASE = 2;
|
||||||
|
private static final int MAX_BASE = 36;
|
||||||
|
private static final char ZERO_CHAR = '0';
|
||||||
|
private static final char A_CHAR = 'A';
|
||||||
|
private static final int DIGIT_OFFSET = 10;
|
||||||
|
|
||||||
private DecimalToAnyBase() {
|
private DecimalToAnyBase() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
/**
|
||||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
* Converts a decimal number to a string representation in the specified base.
|
||||||
System.out.println("Enter the decimal input below: ");
|
* For example, converting the decimal number 10 to base 2 would return "1010".
|
||||||
int decInput = Integer.parseInt(br.readLine());
|
*
|
||||||
System.out.println();
|
* @param decimal the decimal number to convert
|
||||||
|
* @param base the base to convert to (must be between {@value #MIN_BASE} and {@value #MAX_BASE})
|
||||||
|
* @return the string representation of the number in the specified base
|
||||||
|
* @throws IllegalArgumentException if the base is out of the supported range
|
||||||
|
*/
|
||||||
|
public static String convertToAnyBase(int decimal, int base) {
|
||||||
|
if (base < MIN_BASE || base > MAX_BASE) {
|
||||||
|
throw new IllegalArgumentException("Base must be between " + MIN_BASE + " and " + MAX_BASE);
|
||||||
|
}
|
||||||
|
|
||||||
System.out.println("Enter the base below: ");
|
if (decimal == 0) {
|
||||||
int base = Integer.parseInt(br.readLine());
|
return String.valueOf(ZERO_CHAR);
|
||||||
System.out.println();
|
}
|
||||||
|
|
||||||
System.out.println("Decimal Input"
|
List<Character> digits = new ArrayList<>();
|
||||||
+ " is: " + decInput);
|
while (decimal > 0) {
|
||||||
System.out.println("Value of " + decInput + " in base " + base + " is: " + convertToAnyBase(decInput, base));
|
digits.add(convertToChar(decimal % base));
|
||||||
|
decimal /= base;
|
||||||
|
}
|
||||||
|
|
||||||
br.close();
|
StringBuilder result = new StringBuilder(digits.size());
|
||||||
|
for (int i = digits.size() - 1; i >= 0; i--) {
|
||||||
|
result.append(digits.get(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method produces a String value of any given input decimal in any
|
* Converts an integer value to its corresponding character in the specified base.
|
||||||
* base
|
* This method is used to convert values from 0 to 35 into their appropriate character representation.
|
||||||
|
* For example, 0-9 are represented as '0'-'9', and 10-35 are represented as 'A'-'Z'.
|
||||||
*
|
*
|
||||||
* @param inp Decimal of which we need the value in base in String format
|
* @param value the integer value to convert (should be less than the base value)
|
||||||
* @return string format of the converted value in the given base
|
* @return the character representing the value in the specified base
|
||||||
*/
|
*/
|
||||||
public static String convertToAnyBase(int inp, int base) {
|
private static char convertToChar(int value) {
|
||||||
ArrayList<Character> charArr = new ArrayList<>();
|
if (value >= 0 && value <= 9) {
|
||||||
|
return (char) (ZERO_CHAR + value);
|
||||||
while (inp > 0) {
|
|
||||||
charArr.add(reVal(inp % base));
|
|
||||||
inp /= base;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBuilder str = new StringBuilder(charArr.size());
|
|
||||||
|
|
||||||
for (Character ch : charArr) {
|
|
||||||
str.append(ch);
|
|
||||||
}
|
|
||||||
|
|
||||||
return str.reverse().toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method produces character value of the input integer and returns it
|
|
||||||
*
|
|
||||||
* @param num integer of which we need the character value of
|
|
||||||
* @return character value of input integer
|
|
||||||
*/
|
|
||||||
public static char reVal(int num) {
|
|
||||||
if (num >= 0 && num <= 9) {
|
|
||||||
return (char) (num + '0');
|
|
||||||
} else {
|
} else {
|
||||||
return (char) (num - 10 + 'A');
|
return (char) (A_CHAR + value - DIGIT_OFFSET);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
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;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.CsvSource;
|
||||||
|
|
||||||
|
public class DecimalToAnyBaseTest {
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@CsvSource({"0, 2, 0", "0, 16, 0", "0, 36, 0", "10, 2, 1010", "255, 16, FF", "100, 8, 144", "42, 2, 101010", "1234, 16, 4D2", "1234, 36, YA"})
|
||||||
|
void testConvertToAnyBase(int decimal, int base, String expected) {
|
||||||
|
assertEquals(expected, DecimalToAnyBase.convertToAnyBase(decimal, base));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBaseOutOfRange() {
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> DecimalToAnyBase.convertToAnyBase(10, 1));
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> DecimalToAnyBase.convertToAnyBase(10, 37));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user