Java Reference
In-Depth Information
b)
All generic method declarations have a type-parameter section that immediately pre-
cedes the method name.
c)
A generic method can be overloaded by another generic method with the same method
name but different method parameters.
d)
A type parameter can be declared only once in the type-parameter section but can ap-
pear more than once in the method's parameter list.
e)
Type-parameter names among different generic methods must be unique.
f)
The scope of a generic class's type parameter is the entire class except its static mem-
bers.
20.2
Fill in the blanks in each of the following:
a)
and enable you to specify, with a single method declaration, a set of
related methods, or with a single class declaration, a set of related types, respectively.
b)
A type-parameter section is delimited by
.
c)
A generic method's can be used to specify the method's argument types, to
specify the method's return type and to declare variables within the method.
d)
The statement " Stack objectStack = new Stack(); " indicates that objectStack stores
.
e)
In a generic class declaration, the class name is followed by a(n)
.
f)
The syntax
specifies that the upper bound of a wildcard is type T .
Answers to Self-Review Exercises
20.1 a) False. Generic and nongeneric methods can have the 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 nongeneric 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.
20.2 a) Generic methods, generic classes. b) angle brackets ( < and > ). c) type parameters. d) a
raw type. e) type-parameter section. f) ? extends T .
Exercises
20.3
(Explain Notation) Explain the use of the following notation in a Java program:
public class Array<T> { }
20.4 (Generic Method selectionSort ) Write a generic method selectionSort based on the
sort program of Fig. 19.4. 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 se-
lectionSort , so that you can use method compareTo to compare the objects of the type that T rep-
resents.]
20.5 (Overloaded Generic Method printArray ) Overload generic method printArray of
Fig. 20.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 highSub-
script . If either is out of range, the overloaded printArray method should throw an InvalidSub-
scriptException ; 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 .
 
Search WWH ::




Custom Search