Java Reference
In-Depth Information
13
public static void main(String[] args)
14
{
15
int [] array = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
16
String results = someMethod(array, 0 );
17
System.out.println(results);
18
}
19
} // end class SomeClass
18.14 (Palindromes) A palindrome is a string that is spelled the same way forward and backward.
Some examples of palindromes are “radar,” “able was i ere i saw elba” and (if spaces are ignored) “a
man a plan a canal panama.” Write a recursive method testPalindrome that returns boolean value
true if the string stored in the array is a palindrome and false otherwise. The method should ignore
spaces and punctuation in the string.
18.15 (Eight Queens) A puzzler for chess buffs is the Eight Queens problem, which asks: Is it pos-
sible to place eight queens on an empty chessboard so that no queen is “attacking” any other (i.e.,
no two queens are in the same row, in the same column or along the same diagonal)? For instance,
if a queen is placed in the upper-left corner of the board, no other queens could be placed in any of
the marked squares shown in Fig. 18.20. Solve the problem recursively. [ Hint: Your solution should
begin with the first column and look for a location in that column where a queen can be placed—
initially, place the queen in the first row. The solution should then recursively search the remaining
columns. In the first few columns, there will be several locations where a queen may be placed. Take
the first available location. If a column is reached with no possible location for a queen, the program
should return to the previous column, and move the queen in that column to a new row. This con-
tinuous backing up and trying new alternatives is an example of recursive backtracking.]
18.16 (Print an Array) Write a recursive method printArray that displays all the elements in an
array of integers, separated by spaces.
*
*
* *
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
Fig. 18.20 | Squares eliminated by placing a queen in the upper-left corner of a chessboard.
18.17 (Print an Array Backward) Write a recursive method stringReverse that takes a character
array containing a string as an argument and prints the string backward. [ Hint: Use String method
toCharArray , which takes no arguments, to get a char array containing the characters in the String .]
18.18 (Find the Minimum Value in an Array) Write a recursive method recursiveMinimum that
determines the smallest element in an array of integers. The method should return when it receives
an array of one element.
18.19 (Fractals) Repeat the fractal pattern in Section 18.9 to form a star. Begin with five lines (see
Fig. 18.21) instead of one, where each line is a different arm of the star. Apply the “Lo feather frac-
tal” pattern to each arm of the star.
 
Search WWH ::




Custom Search