Java Reference
In-Depth Information
problem for all simpler inputs. Then you only need to figure out how you can turn
the solutions with simpler inputs into a solution for the whole problem.
To illustrate the method of recursion, let us consider the following problem. We
want to test whether a sentence is a palindromeȌa string that is equal to itself
when you reverse all characters. Typical examples of palindromes are
ȗ
A man, a plan, a canalȌPanama!
ȗ
Go hang a salami, I'm a lasagna hog
and, of course, the oldest palindrome of all:
ȗ
Madam, I'm Adam
When testing for a palindrome, we match upper- and lowercase letters, and ignore
all spaces and punctuation marks.
We want to implement the isPalindrome method in the following class:
public class Sentence
{
/**
Constructs a sentence.
@param aText a string containing all characters of the
sentence
*/
public Sentence(String aText)
{
text = aText;
}
/**
Tests whether this sentence is a palindrome.
@return true if this sentence is a palindrome, false
otherwise
*/
public boolean isPalindrome()
{
È
}
private String text;
}
Search WWH ::




Custom Search