1. Declare and implement a class named BinaryStrings. The class will contain the following methods: incBinary(String b)...
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") = "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
Introduction To Java Programming And Data Structures Comprehensive Version
ISBN: 9780136520238
12th Edition
Authors: Y. Daniel Liang