Skip to content

Commit 3187b1f

Browse files
authored
refactor: DecimalToAnyUsingStack (#5392)
1 parent 25b8010 commit 3187b1f

File tree

2 files changed

+62
-34
lines changed

2 files changed

+62
-34
lines changed

src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java

+17-34
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,33 @@ public final class DecimalToAnyUsingStack {
66
private DecimalToAnyUsingStack() {
77
}
88

9-
public static void main(String[] args) {
10-
assert convert(0, 2).equals("0");
11-
assert convert(30, 2).equals("11110");
12-
assert convert(30, 8).equals("36");
13-
assert convert(30, 10).equals("30");
14-
assert convert(30, 16).equals("1E");
15-
}
16-
179
/**
18-
* Convert decimal number to another radix
10+
* Convert a decimal number to another radix.
1911
*
2012
* @param number the number to be converted
2113
* @param radix the radix
22-
* @return another radix
23-
* @throws ArithmeticException if <tt>number</tt> or <tt>radius</tt> is
24-
* invalid
14+
* @return the number represented in the new radix as a String
15+
* @throws IllegalArgumentException if <tt>number</tt> is negative or <tt>radix</tt> is not between 2 and 16 inclusive
2516
*/
26-
private static String convert(int number, int radix) {
17+
public static String convert(int number, int radix) {
18+
if (number < 0) {
19+
throw new IllegalArgumentException("Number must be non-negative.");
20+
}
2721
if (radix < 2 || radix > 16) {
28-
throw new ArithmeticException(String.format("Invalid input -> number:%d,radius:%d", number, radix));
22+
throw new IllegalArgumentException(String.format("Invalid radix: %d. Radix must be between 2 and 16.", radix));
23+
}
24+
25+
if (number == 0) {
26+
return "0";
2927
}
30-
char[] tables = {
31-
'0',
32-
'1',
33-
'2',
34-
'3',
35-
'4',
36-
'5',
37-
'6',
38-
'7',
39-
'8',
40-
'9',
41-
'A',
42-
'B',
43-
'C',
44-
'D',
45-
'E',
46-
'F',
47-
};
28+
29+
char[] tables = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
30+
4831
Stack<Character> bits = new Stack<>();
49-
do {
32+
while (number > 0) {
5033
bits.push(tables[number % radix]);
5134
number = number / radix;
52-
} while (number != 0);
35+
}
5336

5437
StringBuilder result = new StringBuilder();
5538
while (!bits.isEmpty()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.thealgorithms.stacks;
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+
8+
class DecimalToAnyUsingStackTest {
9+
10+
@Test
11+
void testConvertToBinary() {
12+
assertEquals("0", DecimalToAnyUsingStack.convert(0, 2));
13+
assertEquals("11110", DecimalToAnyUsingStack.convert(30, 2));
14+
}
15+
16+
@Test
17+
void testConvertToOctal() {
18+
assertEquals("36", DecimalToAnyUsingStack.convert(30, 8));
19+
}
20+
21+
@Test
22+
void testConvertToDecimal() {
23+
assertEquals("30", DecimalToAnyUsingStack.convert(30, 10));
24+
}
25+
26+
@Test
27+
void testConvertToHexadecimal() {
28+
assertEquals("1E", DecimalToAnyUsingStack.convert(30, 16));
29+
}
30+
31+
@Test
32+
void testInvalidRadix() {
33+
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> DecimalToAnyUsingStack.convert(30, 1));
34+
assertEquals("Invalid radix: 1. Radix must be between 2 and 16.", thrown.getMessage());
35+
36+
thrown = assertThrows(IllegalArgumentException.class, () -> DecimalToAnyUsingStack.convert(30, 17));
37+
assertEquals("Invalid radix: 17. Radix must be between 2 and 16.", thrown.getMessage());
38+
}
39+
40+
@Test
41+
void testNegativeNumber() {
42+
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> DecimalToAnyUsingStack.convert(-30, 2));
43+
assertEquals("Number must be non-negative.", thrown.getMessage());
44+
}
45+
}

0 commit comments

Comments
 (0)