Internet and Web Resources

Answers to Self Review Exercises

18.1

a) False. Generic methods and non-generic methods can have same method name. A generic method can overload another generic method with the same method name but different method parameters. A generic method also can be overloaded by providing non-generic methods with the same method name and number of arguments. b) False. All generic method declarations have a type parameter section that immediately precedes the method's return type. c) True. d) True. e) False. Type parameter names among different generic methods need not be unique. f) True.

18.2

a) Generic methods, generic classes. b) angle brackets (< and >). c) type parameters. d) a raw type. e) type parameter section. f) ? extends E .

Exercises

18.3

Explain the use of the following notation in a Java program:

public class Array< T > { }

18.4

Write a generic method selectionSort based on the sort program of Fig. 16.6 and Fig. 16.7. Write a test program that inputs, sorts and outputs an Integer array and a Float array. [Hint: Use < T extends Comparable< T > > in the type parameter section for method selectionSort, so that you can use method compareTo to compare the objects of two generic types T.]

18.5

Overload generic method printArray of Fig. 18.3 so that it takes two additional integer arguments, lowSubscript and highSubscript. A call to this method prints only the designated portion of the array. Validate lowSubscript and highSubscript. If either is out-of-range, or if highSubscript is less than or equal to lowSubscript, the overloaded printArray method should throw an InvalidSubscriptException; otherwise, printArray should return the number of elements printed. Then modify main to exercise both versions of printArray on arrays integerArray, doubleArray and characterArray. Test all capabilities of both versions of printArray.

18.6

Overload generic method printArray of Fig. 18.3 with a non-generic version that specifically prints an array of strings in neat, tabular format, as shown in the sample output that follows:

Array stringArray contains: one two three four five six seven eight  

 
18.7

Write a simple generic version of method isEqualTo that compares its two arguments with the equals method and returns TRue if they are equal and false otherwise. Use this generic method in a program that calls isEqualTo with a variety of built-in types, such as Object or Integer. What result do you get when you attempt to run this program?

18.8

Write a generic class Pair which has two type parametersF and S, each represents the type of the first and second element of the pair respectively. Add get and set methods for the first and second elements of the pair. [Hint: The class header should be public class Pair< F, S >.]

18.9

Convert classes TReeNode and tree from Fig. 17.17 into generic classes. To insert an object in a tree, the object must be compared to the objects in existing treeNodes. For this reason, classes treeNode and tree should specify Comparable< E > as the upper bound of each class's type parameter. After modifying classes treeNode and tree, write a test application that creates three tree objectsone that stores Integers, one that stores Doubles and one that stores Strings. Insert 10 values into each tree. Then output the preorder, inorder and postorder traversals for each tree.

18.10

Modify your test program from Exercise 18.9 to use a generic method named testTree to test the three TRee objects. The method should be called three timesonce for each TRee object.

18.11

How can generic methods be overloaded?

18.12

The compiler performs a matching process to determine which method to call when a method is invoked. Under what circumstances does an attempt to make a match result in a compile-time error?

18.13

Explain why a Java program might use the statement

ArrayList< Employee > workerList = new ArrayList< Employee >();

Категории