Question
1. Declare and implement a class named BinaryStrings. The class will contain the following methods: incBinary(String b) returns b+1 in binary. //examples - incBinary(101)
1. Declare and implement a class named BinaryStrings. The class will contain the following methods:
incBinary(String b) – returns b+1 in binary.
//examples - incBinary("101") = "110", incBinary("111") = "1000"
decBinary(String b) – returns b-1 in binary assuming b contains at least one 1s.
//examples - decBinary("101") = "100", decBinary("100") = "11"
printBinary(int n) – prints 0 to n in binary calling incBinary() method repeatedly.
//examples - printBinary(15) must print 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111 in this order.
Note:
You will have to manipulate the given string parameter b directly without changing it to
numeric value like int or long in both incBinary and decBinary methods.
You are not allowed to use any built-in methods that change decimal to binary or binary to
decimal either.
.length(), .charAt(), .indexOf(), .lastIndexOf(), and .substring() are the only built-in methods
allowed.
2. Use the following menu in the main method:
i: increase binary
d: decrease binary
p: print binary
q: Quit
Select:
3. Test your program
Step by Step Solution
3.43 Rating (159 Votes )
There are 3 Steps involved in it
Step: 1
public class BinaryStrings Method to increment a binary string by 1 public String incBinaryString b int carry 1 char arr btoCharArray int index arrlen...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started