Question: Complete the Java method convertBinaryStrToDecimal(String s) to convert an input of binary string s to the equivalent decimal number using for loop. For example,

Complete the Java method convertBinaryStrToDecimal(String s) to convert an input of binary string s to the equivalent decimal number using "for loop". For example, after you call the method convert BinaryStrToDecimal("1010"). it will return the decimal value 10.0. Note: "1010" is just an example. The method convertBinaryStr To Decimal(String s) should work for any binary string s. Note1: you can only use "for loop". It is not allowed to use "while loop" or "do while loop" in this question. Note2: You are not allowed to use the method Integer.parseInt(String s, int radix). Hint: First, define a decimal number with value 0. Then, you can use a for loop to iterate to the given binary string. If the character at an index i is '1', then add 2^(s.length() - i - 1) to the result decimal number. Also, you can use Math.pow(a, b) to calculate a^b. Please complete the following starter code: public static double convertBinaryStrToDecimal(String s) { // code below
Step by Step Solution
There are 3 Steps involved in it
java public static double convertBinaryStrToDecimalString s double decimal 00 for ... View full answer
Get step-by-step solutions from verified subject matter experts
