Question: Write two Java methods to convert a 2s complement binary number to decimal and vice versa. You must write JUnit tests to test the corresponding

Write two Java methods to convert a 2s complement binary number to decimal and vice versa. You must write JUnit tests to test the corresponding code. Test for different size binary numbers and decimals. DO NOT use any shortcuts or predefined methods other than Math class methods. The method declarations to use are also provided below. You may create helper methods to remove the redundancy but the method signatures of those below cannot be changed.

// Takes an array of bits to return the corresponding

// decimal equivalent.

public static int convert2sCompToDecimal(char[] bits)

// Takes a decimal and returns the 2s complement equivalent.

// Assume that the decimal value wont require more than 16 bits.

public static char[] convertDecimalTo2sComp(int decimal)

Heres a sample JUnit test for one of the methods above.

@Test public void testNegative2sComp() {

char data[] = {'1', '0', '1', '0', '0'};

assertEquals(-12, Convert.convert2sCompToDecimal(data)); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!