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

Java Recursive Grammar, Java Ids = { w : w is a legal Java identifier}, the task

ID: 3698351 • Letter: J

Question

Java Recursive Grammar, Java Ids = { w : w is a legal Java identifier}, the task is to implement and test the algorithm below, helper methods can be defined as needed. The pseudocode for a recursive valued method that determines whether a String is in JavaIds is what follows:

isId (in w : String) : boolean

// Returns true if w is a legal Java identifier;

// Otherwise returns false.

if (w is of length 1) { // base case

    if (w is a Java letter) {

        return true;

} else {

       return false; }

} else if (the last character of w is a Java letter or a digit) {

      return isId (w minus its last character) // Point X

} else {

    return false;

} // end if

Explanation / Answer

public class ValidIdentifier {

   public static boolean isId (String w, int index){
       if(w==null)
           return true;
       if(w.length()== index){
           return true;
       }
       char c = w.charAt(index);
      
       if(index == 0){
           if(!Character.isLetter(c) || c !='$' ||c!='_')
               return false;
       }
      
       if(!Character.isLetter(c) && !Character.isDigit(c) && c!='$' && c!='_')
           return false;
      
       return isId(w, index+1);
   }

   public static void main(String[] args) {
      
       String id = "efewrgf456546765y55 8`2`1q       qsq";
      
       System.out.println(isId(id, 0));
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote