Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA: •Write a recursive function that will test to see if the input String para

ID: 3841393 • Letter: J

Question

JAVA:

•Write a recursive function that will test to see if the input String parameter belongs to the following format:

•L = {S : S is in the form of AnB3n, for some n >= 0} where n and 3n are powers.

•Test your function in main function.

The class should have two methods:

•public static boolean IsIn(String w){ //body};

•public static void main(String[] args)

public static void main(String[] args){

       String str ="AABBBBBB";

       if(IsIn(str))

         System.out.println("The string " + str + " is in the language.");

       else

         System.out.println("The string " + str + " is in NOT the language.");

}

Explanation / Answer

import java.util.Scanner;

public class CheckLanguage
{
public static boolean checkB(String w1,int ctr)
{
int ctr1=0;
ctr=ctr*3;
boolean x=false;
if(w1.contains("A")||w1.contains("a"))
{
x=false;
}
else
{
char arr[]=w1.toCharArray();
for(int i=0;i<w1.length();i++)
{
if(arr[i]=='B'||arr[i]=='b')
{
ctr1++;
}
}
if(ctr1==ctr)
{
x=true;
}
}
return x;
}
public static boolean IsIn(String w)
{
int ctr=0,sm=0;
boolean y=false;
char arr[]=w.toCharArray();
for(int i=0;i<w.length();i++)
{
if(arr[i]=='A'||arr[i]=='a')
{
ctr++;
}
else
{
sm=i;
break;
}
}
y=checkB(w.substring(sm,w.length()),ctr);
return y;
}
public static void main(String args[])
{
String str1;
Scanner s = new Scanner(System.in);
System.out.println ("Enter the String:");
str1 = s.nextLine();
if(IsIn(str1))
System.out.println("The string " + str1 + " is in the language.");
else
System.out.println("The string " + str1 + " is in NOT the language.");
}
}