From 3187b1f99c9a652d802265edf69421b137ea2344 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Sun, 25 Aug 2024 22:01:52 +0200 Subject: [PATCH] refactor: `DecimalToAnyUsingStack` (#5392) --- .../stacks/DecimalToAnyUsingStack.java | 53 +++++++------------ .../stacks/DecimalToAnyUsingStackTest.java | 45 ++++++++++++++++ 2 files changed, 63 insertions(+), 35 deletions(-) create mode 100644 src/test/java/com/thealgorithms/stacks/DecimalToAnyUsingStackTest.java diff --git a/src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java b/src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java index 41d1c640..ff6402c9 100644 --- a/src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java +++ b/src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java @@ -6,50 +6,33 @@ public final class DecimalToAnyUsingStack { private DecimalToAnyUsingStack() { } - public static void main(String[] args) { - assert convert(0, 2).equals("0"); - assert convert(30, 2).equals("11110"); - assert convert(30, 8).equals("36"); - assert convert(30, 10).equals("30"); - assert convert(30, 16).equals("1E"); - } - /** - * Convert decimal number to another radix + * Convert a decimal number to another radix. * * @param number the number to be converted * @param radix the radix - * @return another radix - * @throws ArithmeticException if number or radius is - * invalid + * @return the number represented in the new radix as a String + * @throws IllegalArgumentException if number is negative or radix is not between 2 and 16 inclusive */ - private static String convert(int number, int radix) { - if (radix < 2 || radix > 16) { - throw new ArithmeticException(String.format("Invalid input -> number:%d,radius:%d", number, radix)); + public static String convert(int number, int radix) { + if (number < 0) { + throw new IllegalArgumentException("Number must be non-negative."); } - char[] tables = { - '0', - '1', - '2', - '3', - '4', - '5', - '6', - '7', - '8', - '9', - 'A', - 'B', - 'C', - 'D', - 'E', - 'F', - }; + if (radix < 2 || radix > 16) { + throw new IllegalArgumentException(String.format("Invalid radix: %d. Radix must be between 2 and 16.", radix)); + } + + if (number == 0) { + return "0"; + } + + char[] tables = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; + Stack bits = new Stack<>(); - do { + while (number > 0) { bits.push(tables[number % radix]); number = number / radix; - } while (number != 0); + } StringBuilder result = new StringBuilder(); while (!bits.isEmpty()) { diff --git a/src/test/java/com/thealgorithms/stacks/DecimalToAnyUsingStackTest.java b/src/test/java/com/thealgorithms/stacks/DecimalToAnyUsingStackTest.java new file mode 100644 index 00000000..4bd9f2af --- /dev/null +++ b/src/test/java/com/thealgorithms/stacks/DecimalToAnyUsingStackTest.java @@ -0,0 +1,45 @@ +package com.thealgorithms.stacks; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +class DecimalToAnyUsingStackTest { + + @Test + void testConvertToBinary() { + assertEquals("0", DecimalToAnyUsingStack.convert(0, 2)); + assertEquals("11110", DecimalToAnyUsingStack.convert(30, 2)); + } + + @Test + void testConvertToOctal() { + assertEquals("36", DecimalToAnyUsingStack.convert(30, 8)); + } + + @Test + void testConvertToDecimal() { + assertEquals("30", DecimalToAnyUsingStack.convert(30, 10)); + } + + @Test + void testConvertToHexadecimal() { + assertEquals("1E", DecimalToAnyUsingStack.convert(30, 16)); + } + + @Test + void testInvalidRadix() { + IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> DecimalToAnyUsingStack.convert(30, 1)); + assertEquals("Invalid radix: 1. Radix must be between 2 and 16.", thrown.getMessage()); + + thrown = assertThrows(IllegalArgumentException.class, () -> DecimalToAnyUsingStack.convert(30, 17)); + assertEquals("Invalid radix: 17. Radix must be between 2 and 16.", thrown.getMessage()); + } + + @Test + void testNegativeNumber() { + IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> DecimalToAnyUsingStack.convert(-30, 2)); + assertEquals("Number must be non-negative.", thrown.getMessage()); + } +}