Complete Method

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

The complete method is below, suitable for copying and running:

Complete Method

class Tester
{
  public boolean test( String trial )
  {
    String lower = trial.toLowerCase();

    StringBuffer azBuffer  = new StringBuffer();

    for ( int j=0; j < lower.length(); j++ )
    {
       char c = lower.charAt(j);
       if ( c >= 'a' && c <= 'z' )
         azBuffer.append( c );
    }

    String forward  = azBuffer.toString();
    String backward = azBuffer.reverse().toString();

    if ( forward.equals( backward ) )
      return true;
    else
      return false;
  }
}

public class PalindromeTester
{
  public static void main ( String[] args )
  {
    Tester pTester = new Tester();
    String trial = "A man, a plan, a canal, Panama!" ;

    if ( pTester.test( trial ) )
      System.out.println( "Is a Palindrome" );
    else
      System.out.println( "Not a Palindrome" );
  }

}

QUESTION 13:

Could the test() method be written without constructing any Strings?