Question
Answered step-by-step

Please answer in 15 minutes   The goal of the method ...

Please answer in 15 minutes
 

The goal of the method binaryToDecimal here is to convert a binary number into a decimal. It receives a java LinkedList of Strings where each of its elements represents one of the digits (0 or 1) that form the number.

Binary numbers follow a positional schema where each digit represents if a power of two needs to be added to form the decimal number. Starting from right to left, the first digit represents 2^0, the second digit 2^1, the third 2^2, and so on. For example, the number 0110 represents the number 6 because:

0110 = 0*(2^3) + 1*(2^2) + 1*(2^1) + 0*(2^0)

The code shown below needs to be able to handle the case where one or more of the Strings within the array cannot be converted into a number. In case that exception is triggered, it needs to return -1 and print the message "One or more of the digits could not be converted into an integer".

You need to identify the wrong lines (by using the number of the line) and propose a new line that fixes it and, at the same time, you need to explain why are you fixing each of those lines of code.

E.g.

Line x) The error in this line is that _______, I would fix it with the following line _______ . I propose this fix because _____________

Examples of execution:

binaryToDecimal({0,1,1,1}) -> 7

binaryToDecimal({1,0,0,0,0}) -> 16

Hint: five lines present errors in this case (one error per line).

Code given to start:

1 public static int binaryToDecimal(LinkedList<String> binaryDigits) {
2   try {
3      int convertedInteger = 1;
4      int maxExponent = binaryDigits.length;
5      for(int i=0;i<=maxExponent;i++) {
6          String binaryDigitAsString = binaryDigits[i];
7          int binaryDigit = Integer.parseInt(binaryDigitAsString);
8          if(binaryDigit==1) {
9             convertedInteger+=Math.pow(2,i);
10          }
11      }
12      return convertedInteger;
13   }catch(IOException e){
14      System.out.println("One or more of the digits could not be converted into an integer");
15      return -1;
16   }
17}

Answer & Explanation
Verified Solved by verified expert
Rated Helpful

at, ultrices ac magna. Fusce dui lectus,

rem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Do

Unlock full access to Course Hero

Explore over 16 million step-by-step answers from our library

Subscribe to view answer
Step-by-step explanation

molestie consequa

l

, dictum vitae odio. Donec aliquet. Lor

et, consectetur adipiscing eli

ng elit. Nam lacinia pulvinar tortor ne

l

Fusce dui lectus, congue vel laoreet ac, dictum

pulvinar tortor nec

ur laoreet. Nam risus a

ec facilisis

usce dui lectus,

itur laoree

molestie co

l

ac, dictum vitae odio. Donec aliquet.

ctum vitae odio. Donec aliqu

dictum vitae o

molestie consequat, ultrices ac m

usce dui lectus

l

onec

l

at, ultrices ac magna. Fusce dui lectus, con

, dictum

l

a. Fusce dui lectus, co

dictum vitae odio. Donec aliquet. Lorem ipsum

con

pulvin

l

ongue ve

sum dolor sit

l

l

gue

Student review
100% (1 rating)
Thorough explanation