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: • "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" → 0 • "1" → 1 • "01" 1 • "0000" • "111111111" →9 • "1101010010110" → 7

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%
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
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
Transcribed Image 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
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]
[LINE9]
Transcribed Image 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] [LINE9]
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education