mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 02:04:31 +08:00
refactor: BinaryToDecimal
(#5330)
This commit is contained in:
@ -1,37 +1,33 @@
|
|||||||
package com.thealgorithms.conversions;
|
package com.thealgorithms.conversions;
|
||||||
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class converts a Binary number to a Decimal number
|
* This class converts a Binary number to a Decimal number
|
||||||
*/
|
*/
|
||||||
final class BinaryToDecimal {
|
final class BinaryToDecimal {
|
||||||
|
private static final int BINARY_BASE = 2;
|
||||||
|
|
||||||
private BinaryToDecimal() {
|
private BinaryToDecimal() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long binaryToDecimal(long binNum) {
|
|
||||||
long binCopy;
|
|
||||||
long d;
|
|
||||||
long s = 0;
|
|
||||||
long power = 0;
|
|
||||||
binCopy = binNum;
|
|
||||||
while (binCopy != 0) {
|
|
||||||
d = binCopy % 10;
|
|
||||||
s += d * (long) Math.pow(2, power++);
|
|
||||||
binCopy /= 10;
|
|
||||||
}
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main Method
|
* Converts a binary number to its decimal equivalent.
|
||||||
*
|
*
|
||||||
* @param args Command line arguments
|
* @param binaryNumber The binary number to convert.
|
||||||
|
* @return The decimal equivalent of the binary number.
|
||||||
|
* @throws IllegalArgumentException If the binary number contains digits other than 0 and 1.
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static long binaryToDecimal(long binaryNumber) {
|
||||||
Scanner sc = new Scanner(System.in);
|
long decimalValue = 0;
|
||||||
System.out.print("Binary number: ");
|
long power = 0;
|
||||||
System.out.println("Decimal equivalent:" + binaryToDecimal(sc.nextLong()));
|
|
||||||
sc.close();
|
while (binaryNumber != 0) {
|
||||||
|
long digit = binaryNumber % 10;
|
||||||
|
if (digit > 1) {
|
||||||
|
throw new IllegalArgumentException("Incorrect binary digit: " + digit);
|
||||||
|
}
|
||||||
|
decimalValue += (long) (digit * Math.pow(BINARY_BASE, power++));
|
||||||
|
binaryNumber /= 10;
|
||||||
|
}
|
||||||
|
return decimalValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
package com.thealgorithms.conversions;
|
package com.thealgorithms.conversions;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.CsvSource;
|
||||||
|
|
||||||
public class BinaryToDecimalTest {
|
public class BinaryToDecimalTest {
|
||||||
|
|
||||||
@ -30,4 +33,10 @@ public class BinaryToDecimalTest {
|
|||||||
assertEquals(262144L, BinaryToDecimal.binaryToDecimal(1000000000000000000L));
|
assertEquals(262144L, BinaryToDecimal.binaryToDecimal(1000000000000000000L));
|
||||||
assertEquals(524287L, BinaryToDecimal.binaryToDecimal(1111111111111111111L));
|
assertEquals(524287L, BinaryToDecimal.binaryToDecimal(1111111111111111111L));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@CsvSource({"2", "1234", "11112", "101021"})
|
||||||
|
void testNotCorrectBinaryInput(long binaryNumber) {
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> BinaryToDecimal.binaryToDecimal(binaryNumber));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user