Skip to content

Commit 8a89b42

Browse files
authored
refactor: AnyBaseToDecimal (#5357)
1 parent 3398c56 commit 8a89b42

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
lines changed

src/main/java/com/thealgorithms/conversions/AnyBaseToDecimal.java

+28-32
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,50 @@
33
/**
44
* @author Varun Upadhyay (<a href="https://github.com/varunu28">...</a>)
55
*/
6-
// Driver program
76
public final class AnyBaseToDecimal {
8-
private AnyBaseToDecimal() {
9-
}
7+
private static final int CHAR_OFFSET_FOR_DIGIT = '0';
8+
private static final int CHAR_OFFSET_FOR_UPPERCASE = 'A' - 10;
109

11-
public static void main(String[] args) {
12-
assert convertToDecimal("1010", 2) == Integer.valueOf("1010", 2);
13-
assert convertToDecimal("777", 8) == Integer.valueOf("777", 8);
14-
assert convertToDecimal("999", 10) == Integer.valueOf("999", 10);
15-
assert convertToDecimal("ABCDEF", 16) == Integer.valueOf("ABCDEF", 16);
16-
assert convertToDecimal("XYZ", 36) == Integer.valueOf("XYZ", 36);
10+
private AnyBaseToDecimal() {
1711
}
1812

1913
/**
20-
* Convert any radix to decimal number
14+
* Convert any radix to a decimal number.
2115
*
22-
* @param s the string to be convert
23-
* @param radix the radix
24-
* @return decimal of bits
25-
* @throws NumberFormatException if {@code bits} or {@code radix} is invalid
16+
* @param input the string to be converted
17+
* @param radix the radix (base) of the input string
18+
* @return the decimal equivalent of the input string
19+
* @throws NumberFormatException if the input string or radix is invalid
2620
*/
27-
public static int convertToDecimal(String s, int radix) {
28-
int num = 0;
29-
int pow = 1;
21+
public static int convertToDecimal(String input, int radix) {
22+
int result = 0;
23+
int power = 1;
3024

31-
for (int i = s.length() - 1; i >= 0; i--) {
32-
int digit = valOfChar(s.charAt(i));
25+
for (int i = input.length() - 1; i >= 0; i--) {
26+
int digit = valOfChar(input.charAt(i));
3327
if (digit >= radix) {
34-
throw new NumberFormatException("For input string " + s);
28+
throw new NumberFormatException("For input string: " + input);
3529
}
36-
num += valOfChar(s.charAt(i)) * pow;
37-
pow *= radix;
30+
result += digit * power;
31+
power *= radix;
3832
}
39-
return num;
33+
return result;
4034
}
4135

4236
/**
43-
* Convert character to integer
37+
* Convert a character to its integer value.
4438
*
45-
* @param c the character
46-
* @return represented digit of given character
47-
* @throws NumberFormatException if {@code ch} is not UpperCase or Digit
48-
* character.
39+
* @param character the character to be converted
40+
* @return the integer value represented by the character
41+
* @throws NumberFormatException if the character is not an uppercase letter or a digit
4942
*/
50-
public static int valOfChar(char c) {
51-
if (!(Character.isUpperCase(c) || Character.isDigit(c))) {
52-
throw new NumberFormatException("invalid character :" + c);
43+
private static int valOfChar(char character) {
44+
if (Character.isDigit(character)) {
45+
return character - CHAR_OFFSET_FOR_DIGIT;
46+
} else if (Character.isUpperCase(character)) {
47+
return character - CHAR_OFFSET_FOR_UPPERCASE;
48+
} else {
49+
throw new NumberFormatException("invalid character:" + character);
5350
}
54-
return Character.isDigit(c) ? c - '0' : c - 'A' + 10;
5551
}
5652
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 AnyBaseToDecimalTest {
11+
@ParameterizedTest
12+
@CsvSource({"1010, 2, 10", "777, 8, 511", "999, 10, 999", "ABCDEF, 16, 11259375", "XYZ, 36, 44027", "0, 2, 0", "A, 16, 10", "Z, 36, 35"})
13+
void testConvertToDecimal(String input, int radix, int expected) {
14+
assertEquals(expected, AnyBaseToDecimal.convertToDecimal(input, radix));
15+
}
16+
17+
@Test
18+
void testIncorrectInput() {
19+
assertThrows(NumberFormatException.class, () -> AnyBaseToDecimal.convertToDecimal("G", 16));
20+
assertThrows(NumberFormatException.class, () -> AnyBaseToDecimal.convertToDecimal("XYZ", 10));
21+
}
22+
}

0 commit comments

Comments
 (0)