In: Computer Science
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the number of binary strings of length n that do not have two consecutive 0’s. For example, for n = 4, the number of binary strings of length 4 that do not contain two consecutive 0’s is 8: 1111, 1110, 1101, 1011, 1010, 0111, 0110, 0101. For this problem, your method needs to return only the number of such strings, not the strings themselves. You may assume that the integer specified in the parameter is positive. Looking at the example above will give you a hint about how such strings start. The method should be static and embedded in a class called Recursion. This class should also have a main method. In this case, we will call the main method with an argument, the number of bits n. This argument will be in args[0]. You should convert it to an int using the Integer.parseInt method. Look this method up in the Java documentation to see what it does
import java.util.Scanner;
public class Recursion {
static Scanner scanner=new Scanner(System.in);
public static void main(String[] args) {
int no=scanner.nextInt();
int
count=countOfBinaryStrings(no);
System.out.println("the count is
"+count);
}
private static int countOfBinaryStrings(int n)
{
System.out.println("enter the
binary strings length of"+ n);
String[] binaryarray=new
String[2*n];
for(int i=0;i<2*n;i++)
{
String
binary="";
binary=scanner.next();
if(binary.length()<n)
{
binaryarray[i]=binary;
for(int
j=0;j<binaryarray.length;j++)
{
for(int
k=0;k<binaryarray[j].length();k++)
{
if(binary.charAt(n-1)==binaryarray[j].charAt(0))
{
System.out.println("your entered string is not allowed");
}
else
binaryarray[i]=binary;
}
}
}
else
{
System.out.println("please enter the binary
string length less than" +n);
binary=scanner.next();
binaryarray[i]=binary;
}
}
int count=0;
for(int i=0;i<binaryarray.length
;i++)
{
System.out.println(binaryarray[i]);
}
return count;
}
}