Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write two Java methods to convert a 2s complement binary numberto decimal and vice versa. You must write JUnit tests to test thecorresponding code. Test

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 predefinedmethods other than Math class methods. The method declarations touse are also provided below. You may create helper methods toremove the redundancy but the method signatures of those belowcannot be changed.

// Takes an array of bits to returnthe corresponding

// decimal equivalent.
public static int convert2sCompToDecimal(char[] bits)

// Takes a decimal and returns the 2scomplement equivalent.

// Assume that the decimalvalue won’t require more than 16 bits.

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));

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Computer Performance Engineering 10th European Workshop Epew 2013 Venice Italy September 17 2013 Proceedings

Authors: Maria Simonetta Balsamo ,William Knottenbelt ,Andrea Marin

2013 Edition

3642407242, 978-3642407246

More Books

Students also viewed these Programming questions