mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 00:54:32 +08:00
refactor: DecimalToBinary
(#5339)
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user