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 >();

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net