go to previous page   go to home page   go to next page

Answer:

What does "Xtail".charAt(0) evaluate to? X — the head of the string.

What does "Xtail".substring(1) evaluate to? tail — the tail of the string.


Java Implementation

public class EqualsTester
{
  public static boolean myEquals( String strA, String strB )
  {
    if ( strA.isEmpty() && strB.isEmpty() ) 
      return true;
    else if ( strA.isEmpty() || strB.isEmpty() ) 
      return false;
    else if ( strA.charAt(0) != strB.charAt(0) )
      return false;
    else
      return myEquals( strA.substring(1), strB.substring(1));
  }
   
  public static void main (String[] args)
  {
    String strA = "applecart";
    String strB = "appleCart";
    
    if ( myEquals( strA, strB ) )
      System.out.println(  "\"" + strA + "\" == \"" + strB  + "\"");
    else
      System.out.println(  "\"" + strA + "\" != \"" + strB  + "\"");
    
  }
}

Java Detail: "\"" is the string consisting of one " (the backslash is needed to escape that character).


QUESTION 20:

Here is the first part of the recursive definition:

  1. If StringA has no characters and StringB has no characters, then the strings ARE equal.

Does this part of the code correctly implement that rule:

    if ( strA.isEmpty() && strB.isEmpty() ) 
      return true;

 


go to previous page   go to home page   go to next page