i copied from worksheet::: Strings of the form A\"B\". The Symbol A\"B\" is a st
ID: 3616850 • Letter: I
Question
i copied from worksheet:::Strings of the form A"B".
The Symbol A"B" is a standard notation for the string that consistsof n consecutive A's followed by n consecutives B's.
Write two methods: iterative and recursive, to recongize this kindof strings.
The pseudocode for a recognition method for this languagefollows:
isAnBn(w)
//return true if w is of the form A"B";
// otherwise return false.
if(the length of w is zero)
return true
else if (w begins with the character A and ends with the characterB)
return isAnBn(w minus its first and last characters)
else
return false
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
note: I think professor wanted do two methods and use messageaccept or reject for system.out.print "accept" and system.out.print"reject" as well.
Can anyone help?
here code, i beleive i did big flaw oh well.
import java.util.Scanner;
public class LabE_1
{
private int isAnBn;
private int BB;
public LabE_1(int isAnBn, int BB)
{
this.isAnBn = isAnBn;
this.BB = BB;
}
public static void main(String [] args)
{
Scanner input = newScanner(System.in);
System.out.print("Enter the A/Bcharacter: ");
int w = input.nextInt();
LabE_1 Character = newLabE_1(isAnBn, w);
Character.w();
}
public static boolean isAnBn
{
if( w > 0)
returntrue;
else if(w.isAnBn &&w.BB)
returnisAnBn(w-isAnBn && BB);
else
returnfalse;
}
public static boolean BB
{
if( w > 0)
returntrue;
else if(w.isAnBn &&w.BB)
returnisAnBn(w-isAnBn && BB);
else
returnfalse;
}
}
Explanation / Answer
//Hope this will helpyou. import java.util.Scanner; public class LabE_1 { public static boolean rec_isAnBn(String s1) { int len; len = s1.length(); if(len==0) return true; else if(s1.charAt(0) == 'A' && s1.charAt(len-1)=='B') returnrec_isAnBn(s1.substring(1,len-1)); else return false; } public static boolean not_rec_isAnBn(String s1) { int len,i; len = s1.length(); if(len%2 != 0) return false; for(i=0;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.