Refactor BinaryToDecimal class (#4135)
This commit is contained in:
parent
d160156003
commit
8798e042a8
@ -7,12 +7,12 @@ import java.util.Scanner;
|
|||||||
*/
|
*/
|
||||||
class BinaryToDecimal {
|
class BinaryToDecimal {
|
||||||
|
|
||||||
public static int binaryToDecimal(int binNum) {
|
public static long binaryToDecimal(long binNum) {
|
||||||
int binCopy, d, s = 0, power = 0;
|
long binCopy, d, s = 0, power = 0;
|
||||||
binCopy = binNum;
|
binCopy = binNum;
|
||||||
while (binCopy != 0) {
|
while (binCopy != 0) {
|
||||||
d = binCopy % 10;
|
d = binCopy % 10;
|
||||||
s += d * (int) Math.pow(2, power++);
|
s += d * (long) Math.pow(2, power++);
|
||||||
binCopy /= 10;
|
binCopy /= 10;
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
@ -26,7 +26,7 @@ class BinaryToDecimal {
|
|||||||
public static void main(String args[]) {
|
public static void main(String args[]) {
|
||||||
Scanner sc = new Scanner(System.in);
|
Scanner sc = new Scanner(System.in);
|
||||||
System.out.print("Binary number: ");
|
System.out.print("Binary number: ");
|
||||||
System.out.println("Decimal equivalent:" + binaryToDecimal(sc.nextInt()));
|
System.out.println("Decimal equivalent:" + binaryToDecimal(sc.nextLong()));
|
||||||
sc.close();
|
sc.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,12 +7,27 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||||||
public class BinaryToDecimalTest {
|
public class BinaryToDecimalTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
// Test converting binary to decimal
|
||||||
public void testBinaryToDecimal() {
|
public void testBinaryToDecimal() {
|
||||||
//zeros at the starting should be removed
|
// zeros at the starting should be removed
|
||||||
assertEquals(0, BinaryToDecimal.binaryToDecimal(0));
|
assertEquals(0, BinaryToDecimal.binaryToDecimal(0));
|
||||||
assertEquals(1, BinaryToDecimal.binaryToDecimal(1));
|
assertEquals(1, BinaryToDecimal.binaryToDecimal(1));
|
||||||
assertEquals(5, BinaryToDecimal.binaryToDecimal(101));
|
assertEquals(5, BinaryToDecimal.binaryToDecimal(101));
|
||||||
assertEquals(63, BinaryToDecimal.binaryToDecimal(111111));
|
assertEquals(63, BinaryToDecimal.binaryToDecimal(111111));
|
||||||
assertEquals(512, BinaryToDecimal.binaryToDecimal(1000000000));
|
assertEquals(512, BinaryToDecimal.binaryToDecimal(1000000000));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
// Test converting negative binary numbers
|
||||||
|
public void testNegativeBinaryToDecimal() {
|
||||||
|
assertEquals(-1, BinaryToDecimal.binaryToDecimal(-1));
|
||||||
|
assertEquals(-42, BinaryToDecimal.binaryToDecimal(-101010));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
// Test converting binary numbers with large values
|
||||||
|
public void testLargeBinaryToDecimal() {
|
||||||
|
assertEquals(262144L, BinaryToDecimal.binaryToDecimal(1000000000000000000L));
|
||||||
|
assertEquals(524287L, BinaryToDecimal.binaryToDecimal(1111111111111111111L));
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user