Java Reference
In-Depth Information
program should use a queue to store the subdirectories under a directory. The
algorithm can be described as follows:
long getSize(File directory) {
long size = 0 ;
add directory to the queue;
while (queue is not empty) {
Remove an item from the queue into t;
if (t is a file)
size += t.length();
else
add all the files and subdirectories under t into the
queue;
}
return size;
}
***20.19
( Game: solution ratio for 24-point game ) When you pick four cards from a
deck of 52 cards for the 24-point game introduced in Programming Exercise
20.13, the four cards may not have a 24-point solution. What is the number
of all possible picks of four cards from 52 cards? Among all possible picks,
how many of them have 24-point solutions? What is the success ratio—that is,
(number of picks with solutions)/ (number of all possible picks of four cards)?
Write a program to find these answers.
*20.20
( Directory size ) Rewrite Programming Exercise 18.28 using a stack instead of
a queue.
*20.21
( Use Comparator ) Write the following generic method using selection sort
and a comparator.
public static <E> void selectionSort(E[] list,
Comparator<? super E> comparator)
Write a test program that creates an array of 10 GeometricObject s and
invokes this method using the GeometricObjectComparator introduced in
Listing 20.4 to sort the elements. Display the sorted elements. Use the follow-
ing statement to create the array.
GeometricObject[] list = { new Circle( 5 ), new Rectangle( 4 , 5 ),
new Circle( 5.5 ), new Rectangle( 2.4 , 5 ), new Circle( 0.5 ),
new Rectangle( 4 , 65), new Circle( 4.5 ), new Rectangle( 4.4 , 1 ),
new Circle( 6.5 ), new Rectangle( 4 , 5 )};
*20.22
( Nonrecursive Tower of Hanoi ) Implement the moveDisks method in Listing
18.8 using a stack instead of using recursion.
**20.23
( Evaluate expression ) Modify Listing 20.9 EvaluateExpression.java to add
operators ^ for exponent and % for modulus. For example, 3 ^ 2 is 9 and 3 % 2
is 1 . The ^ operator has the highest precedence and the % operator has the same
precedence as the * and / operators. Your program should prompt the user to
enter an expression. Here is a sample run of the program:
Enter an expression: (5 * 2 ^ 3 + 2 * 3 % 2) * 4
(5 * 2 ^ 3 + 2 * 3 % 2) * 4 = 160
 
 
Search WWH ::




Custom Search