Skip to content

Commit 2905ccb

Browse files
authored
refactor: DecimalToAnyBase (#5343)
1 parent 404ad72 commit 2905ccb

File tree

2 files changed

+63
-40
lines changed

2 files changed

+63
-40
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,69 @@
11
package com.thealgorithms.conversions;
22

3-
import java.io.BufferedReader;
4-
import java.io.InputStreamReader;
53
import java.util.ArrayList;
4+
import java.util.List;
65

76
/**
7+
* Class that provides methods to convert a decimal number to a string representation
8+
* in any specified base between 2 and 36.
9+
*
810
* @author Varun Upadhyay (<a href="https://github.com/varunu28">...</a>)
911
*/
10-
// Driver Program
1112
public final class DecimalToAnyBase {
12-
private DecimalToAnyBase() {
13-
}
14-
15-
public static void main(String[] args) throws Exception {
16-
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
17-
System.out.println("Enter the decimal input below: ");
18-
int decInput = Integer.parseInt(br.readLine());
19-
System.out.println();
20-
21-
System.out.println("Enter the base below: ");
22-
int base = Integer.parseInt(br.readLine());
23-
System.out.println();
13+
private static final int MIN_BASE = 2;
14+
private static final int MAX_BASE = 36;
15+
private static final char ZERO_CHAR = '0';
16+
private static final char A_CHAR = 'A';
17+
private static final int DIGIT_OFFSET = 10;
2418

25-
System.out.println("Decimal Input"
26-
+ " is: " + decInput);
27-
System.out.println("Value of " + decInput + " in base " + base + " is: " + convertToAnyBase(decInput, base));
28-
29-
br.close();
19+
private DecimalToAnyBase() {
3020
}
3121

3222
/**
33-
* This method produces a String value of any given input decimal in any
34-
* base
23+
* Converts a decimal number to a string representation in the specified base.
24+
* For example, converting the decimal number 10 to base 2 would return "1010".
3525
*
36-
* @param inp Decimal of which we need the value in base in String format
37-
* @return string format of the converted value in the given base
26+
* @param decimal the decimal number to convert
27+
* @param base the base to convert to (must be between {@value #MIN_BASE} and {@value #MAX_BASE})
28+
* @return the string representation of the number in the specified base
29+
* @throws IllegalArgumentException if the base is out of the supported range
3830
*/
39-
public static String convertToAnyBase(int inp, int base) {
40-
ArrayList<Character> charArr = new ArrayList<>();
31+
public static String convertToAnyBase(int decimal, int base) {
32+
if (base < MIN_BASE || base > MAX_BASE) {
33+
throw new IllegalArgumentException("Base must be between " + MIN_BASE + " and " + MAX_BASE);
34+
}
4135

42-
while (inp > 0) {
43-
charArr.add(reVal(inp % base));
44-
inp /= base;
36+
if (decimal == 0) {
37+
return String.valueOf(ZERO_CHAR);
4538
}
4639

47-
StringBuilder str = new StringBuilder(charArr.size());
40+
List<Character> digits = new ArrayList<>();
41+
while (decimal > 0) {
42+
digits.add(convertToChar(decimal % base));
43+
decimal /= base;
44+
}
4845

49-
for (Character ch : charArr) {
50-
str.append(ch);
46+
StringBuilder result = new StringBuilder(digits.size());
47+
for (int i = digits.size() - 1; i >= 0; i--) {
48+
result.append(digits.get(i));
5149
}
5250

53-
return str.reverse().toString();
51+
return result.toString();
5452
}
5553

5654
/**
57-
* This method produces character value of the input integer and returns it
55+
* Converts an integer value to its corresponding character in the specified base.
56+
* This method is used to convert values from 0 to 35 into their appropriate character representation.
57+
* For example, 0-9 are represented as '0'-'9', and 10-35 are represented as 'A'-'Z'.
5858
*
59-
* @param num integer of which we need the character value of
60-
* @return character value of input integer
59+
* @param value the integer value to convert (should be less than the base value)
60+
* @return the character representing the value in the specified base
6161
*/
62-
public static char reVal(int num) {
63-
if (num >= 0 && num <= 9) {
64-
return (char) (num + '0');
62+
private static char convertToChar(int value) {
63+
if (value >= 0 && value <= 9) {
64+
return (char) (ZERO_CHAR + value);
6565
} else {
66-
return (char) (num - 10 + 'A');
66+
return (char) (A_CHAR + value - DIGIT_OFFSET);
6767
}
6868
}
6969
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.CsvSource;
9+
10+
public class DecimalToAnyBaseTest {
11+
12+
@ParameterizedTest
13+
@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"})
14+
void testConvertToAnyBase(int decimal, int base, String expected) {
15+
assertEquals(expected, DecimalToAnyBase.convertToAnyBase(decimal, base));
16+
}
17+
18+
@Test
19+
void testBaseOutOfRange() {
20+
assertThrows(IllegalArgumentException.class, () -> DecimalToAnyBase.convertToAnyBase(10, 1));
21+
assertThrows(IllegalArgumentException.class, () -> DecimalToAnyBase.convertToAnyBase(10, 37));
22+
}
23+
}

0 commit comments

Comments
 (0)