Write Two Java Methods Convert 2 S Complement Binary Number Decimal Vice Versa Must Write Q43875125
Write two Java methods to convert a 2’s complement binary numberto decimal and vice versa. You must write JUnit tests to test thecorresponding code. Test for different size binary numbers anddecimals. DO NOT use any shortcuts or predefined methods other thanMath class methods. The method declarations to use are alsoprovided below. You may create helper methods to remove theredundancy but the method signatures of those below cannot bechanged.
// 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 won’t require more than 16bits.
public static char[] convertDecimalTo2sComp(int decimal)
Here’s 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)); }
Expert Answer
Answer to Write two Java methods to convert a 2’s complement binary number to decimal and vice versa. You must write JUnit tests…
OR