Question: /** * Converts a two's complement binary nubmer to signed decimal * * @param b The two's complement binary number * @return The equivalent decimal

/** * Converts a two's complement binary nubmer to signed decimal * * @param b The two's complement binary number * @return The equivalent decimal value * @exception IllegalArgumentException Parameter array length is longer than MAX_LENGTH. */ public static long binToSDec(boolean[] b) { // PROGRAM 1: Student must complete this method // return value is a placeholder, student should replace with correct return

// Example of throwing an IllegalArgumentException // Student must write code for the required exceptions in other methods. // If the exception condition is true, throw the exception if(b.length > MAX_LENGTH) { // If the condition is true, the exception will be thrown // and the method execution will stop. throw new IllegalArgumentException("parameter array is longer than " + MAX_LENGTH + " bits."); } // If the method execution reaches this point, the exception was // not thrown. // Write the rest of the method here. long n = 0; for (int i = 0; i < b.length; i++) { n *= 2; if (b[i]) { n++; } } return n;

}

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!