In computing, a bit is a 0 or 1 value. We define a bit string to be a possibly empty string which consists of bits. Example bit strings are: • "0" • "1" • "01" • "0000" • "111111111" • "1101010010110"...

Write code to fill in Lines 1 through 9
In computing, a bit is a 0 or 1 value. We define a bit string to be a possibly empty string which<br>consists of bits. Example bit strings are:<br>•

Extracted text: In computing, a bit is a 0 or 1 value. We define a bit string to be a possibly empty string which consists of bits. Example bit strings are: • "0" • "1" • "01" • "0000" • "111111111" • "1101010010110" Given a bit string, it is natural to want to count the number of 0 bits or the number of 1 bits in the string. Assume we wish to count the number of 1 bits. For the example strings, the counts would be: • "0" → 0 • "1" → 1 • "01" → 1 • "0000" → 0 "111111111" 9 "1101010010110" 7
Counting the number of 1 bits in a bit string s can be accomplished in Java by first initializing an<br>integer counter n to 0. Next, we implement a loop which iterates over each character c of s and<br>when c is '1' we add 1 to n. When c is '0' we do nothing. Here is the implementation of that<br>algorithm:<br>private int countOnes(String s) {<br>int n = 0;<br>for (int i = 0; i < s.length(); ++i) {<br>if (s.charAt(i) == '1') {<br>++n;<br>}<br>return n;<br>This problem is also amenable to being solved using a recursive method rather than an iterative<br>method (one that employs a loop). Your job is to select the correct pieces of code from those<br>available and arrange them in proper order to implement a recursive method with the same method<br>signature.<br>private int countOnes(String s) {<br>[LINE1]<br>[LINE2]<br>[LINE3]<br>[LINE4]<br>[LINE5]<br>[LINE6]<br>[LINE7]<br>[LINE8]<br>[LINE9]<br>

Extracted text: Counting the number of 1 bits in a bit string s can be accomplished in Java by first initializing an integer counter n to 0. Next, we implement a loop which iterates over each character c of s and when c is '1' we add 1 to n. When c is '0' we do nothing. Here is the implementation of that algorithm: private int countOnes(String s) { int n = 0; for (int i = 0; i < s.length();="" ++i)="" {="" if="" (s.charat(i)="=" '1')="" {="" ++n;="" }="" return="" n;="" this="" problem="" is="" also="" amenable="" to="" being="" solved="" using="" a="" recursive="" method="" rather="" than="" an="" iterative="" method="" (one="" that="" employs="" a="" loop).="" your="" job="" is="" to="" select="" the="" correct="" pieces="" of="" code="" from="" those="" available="" and="" arrange="" them="" in="" proper="" order="" to="" implement="" a="" recursive="" method="" with="" the="" same="" method="" signature.="" private="" int="" countones(string="" s)="" {="" [line1]="" [line2]="" [line3]="" [line4]="" [line5]="" [line6]="" [line7]="" [line8]="">

Jun 10, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here