Java Reference
In-Depth Information
The following is an example of a method that returns a boolean value.
EXAMPLE 7-4
In this example, we write a method that determines whether a string is a palindrome. A
string is a palindrome if it reads the same forward and backward. For example, the
strings "madam" and "789656987" are both palindromes.
The method isPalindrome takes a string as a parameter and returns true if the string is
a palindrome, false otherwise. Suppose that the String variable str refers to the
string. To be specific, suppose that str refers to the string "845548" . The length of this
string is 6 . Recall that the position of the first character of a string is 0 , the position of the
second character is 1 , and so on.
To determine whether the string str "madam" is a palindrome, first we compare the
character at position 0 with the character at position 4 . If these two characters are the
same, then we compare the character at position 1 with the character at position 3 ;if
these two characters are the same, then we compare the characters at position 2 and 2 .If
we find mismatched characters, the string str is not a palindrome, and the method
returns false . It follows that we need two variables, i and j ; i is initialized to 0 and j is
initialized to the position of the last character of the string. We then compare the
characters at positions i and j . If the characters at positions i and j are the same, we
then increment i and decrement j , and continue this process. If the characters at
positions i and j are not the same, then the method returns false . Note that we only
need to compare the characters in the first half of the string with the characters in the
second half of the string in the order described above. This discussion translates into the
following algorithm:
1. Find the length of the string. Because str is a String variable, we can
use the method length of the class String to find the length of the
string. Suppose len = str.length(); .
2. Set j = len - 1 . (Recall that in a string, the position of the first character
is 0 , the position of the second character is 1 , and so on. Therefore, the
position of the last character in the string str is len - 1 .)
3. Use a for loop to compare the characters in the first half of the string
with those in the second half. Now len specifies the length of the string,
so len - 1 specifies the position of the last character in the string.
Therefore, (len - 1) / 2 gives the position of the character immediately
in front of the midposition in the string. Initially, j is set to len - 1 and
we will use a variable, i ( for loop control variable), initialized to 0 .
After each iteration, i is incremented by 1 and j is decremented by 1 .
Therefore, when i is (len - 1) / 2 , the value of i gives the position of
the character immediately in front of the midposition in the string.
When i is at the last character of the first half of the string, j is at the
first character of the second half of the string. The required for loop is:
7
Search WWH ::




Custom Search