From d80fd0c623306ad14bb76a02328e812e435725d2 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Sat, 17 Aug 2024 20:35:36 +0200 Subject: [PATCH] refactor: `DecimalToBinary` (#5339) --- .../conversions/DecimalToBinary.java | 84 ++++++++----------- .../conversions/DecimalToBinaryTest.java | 21 +++++ 2 files changed, 56 insertions(+), 49 deletions(-) create mode 100644 src/test/java/com/thealgorithms/conversions/DecimalToBinaryTest.java diff --git a/src/main/java/com/thealgorithms/conversions/DecimalToBinary.java b/src/main/java/com/thealgorithms/conversions/DecimalToBinary.java index 471724ff9..e8d033e00 100644 --- a/src/main/java/com/thealgorithms/conversions/DecimalToBinary.java +++ b/src/main/java/com/thealgorithms/conversions/DecimalToBinary.java @@ -1,63 +1,49 @@ package com.thealgorithms.conversions; -import java.util.Scanner; - /** - * This class converts a Decimal number to a Binary number + * This class provides methods to convert a decimal number to a binary number. */ final class DecimalToBinary { + private static final int BINARY_BASE = 2; + private static final int DECIMAL_MULTIPLIER = 10; + private DecimalToBinary() { } /** - * Main Method - * - * @param args Command Line Arguments + * Converts a decimal number to a binary number using a conventional algorithm. + * @param decimalNumber the decimal number to convert + * @return the binary representation of the decimal number */ - public static void main(String[] args) { - conventionalConversion(); - bitwiseConversion(); - } + public static int convertUsingConventionalAlgorithm(int decimalNumber) { + int binaryNumber = 0; + int position = 1; - /** - * This method converts a decimal number to a binary number using a - * conventional algorithm. - */ - public static void conventionalConversion() { - int n; - int b = 0; - int c = 0; - int d; - Scanner input = new Scanner(System.in); - System.out.printf("Conventional conversion.%n Enter the decimal number: "); - n = input.nextInt(); - while (n != 0) { - d = n % 2; - b = b + d * (int) Math.pow(10, c++); - n /= 2; - } // converting decimal to binary - System.out.println("\tBinary number: " + b); - input.close(); - } - - /** - * This method converts a decimal number to a binary number using a bitwise - * algorithm - */ - public static void bitwiseConversion() { - int n; - int b = 0; - int c = 0; - int d; - Scanner input = new Scanner(System.in); - System.out.printf("Bitwise conversion.%n Enter the decimal number: "); - n = input.nextInt(); - while (n != 0) { - d = (n & 1); - b += d * (int) Math.pow(10, c++); - n >>= 1; + while (decimalNumber > 0) { + int remainder = decimalNumber % BINARY_BASE; + binaryNumber += remainder * position; + position *= DECIMAL_MULTIPLIER; + decimalNumber /= BINARY_BASE; } - System.out.println("\tBinary number: " + b); - input.close(); + + return binaryNumber; + } + + /** + * Converts a decimal number to a binary number using a bitwise algorithm. + * @param decimalNumber the decimal number to convert + * @return the binary representation of the decimal number + */ + public static int convertUsingBitwiseAlgorithm(int decimalNumber) { + int binaryNumber = 0; + int position = 1; + + while (decimalNumber > 0) { + int leastSignificantBit = decimalNumber & 1; + binaryNumber += leastSignificantBit * position; + position *= DECIMAL_MULTIPLIER; + decimalNumber >>= 1; + } + return binaryNumber; } } diff --git a/src/test/java/com/thealgorithms/conversions/DecimalToBinaryTest.java b/src/test/java/com/thealgorithms/conversions/DecimalToBinaryTest.java new file mode 100644 index 000000000..f194951e9 --- /dev/null +++ b/src/test/java/com/thealgorithms/conversions/DecimalToBinaryTest.java @@ -0,0 +1,21 @@ +package com.thealgorithms.conversions; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +public class DecimalToBinaryTest { + + @ParameterizedTest + @CsvSource({"0, 0", "1, 1", "2, 10", "5, 101", "10, 1010", "15, 1111", "100, 1100100"}) + void testConvertUsingConventionalAlgorithm(int decimal, int expectedBinary) { + assertEquals(expectedBinary, DecimalToBinary.convertUsingConventionalAlgorithm(decimal)); + } + + @ParameterizedTest + @CsvSource({"0, 0", "1, 1", "2, 10", "5, 101", "10, 1010", "15, 1111", "100, 1100100"}) + void testConvertUsingBitwiseAlgorithm(int decimal, int expectedBinary) { + assertEquals(expectedBinary, DecimalToBinary.convertUsingBitwiseAlgorithm(decimal)); + } +}