Java Reference
In-Depth Information
}
return true;
}
public boolean isPalindrome(String s) {
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
if (Character.isLetter(c)) {
sb.append(c);
}
}
String forward = sb.toString().toLowerCase();
String backward = sb.reverse().toString().toLowerCase();
return forward.equals(backward);
}
}
The implementations will not be surprising to anyone with a Java background. The Groovy
implementation, shown in the next listing, is somewhat shorter.
Listing 6.3. The Groovy implementation of the UtilityMethods interface
There is, in fact, a subtle bug in the implementation of the isPrime method. The tests
will detect it and give me a chance to explain the trap.
Search WWH ::




Custom Search