Java Reference
In-Depth Information
Exercise P17.13. Write a generic static method
print
that prints the
elements of any object that implements the
Iterable<E>
interface. The
elements should be separated by commas. Place your method into an
appropriate utility class.
Additional programming exercises are available in WileyPLUS.
784
785
PROGRAMMING EXERCISES
Project 17.1. Design and implement a generic version of the
DataSet
class of
Chapter 9
that can be used to analyze data of any class that
implements the
Measurable
interface. Make the Measurable interface
generic as well. Supply an
addAll
method that lets you add all values
from another data set with a compatible type. Supply a generic
Measurer<T>
interface to allow the analysis of data whose classes
don't implement the
Measurable
type.
Project 17.2. Turn the
PriorityQueue
class of
Chapter 16
into a
generic class. As with the
TreeSet
class of the standard library, allow
a
Comparator
to compare queue elements. If no comparator is
supplied, assume that the element type implements the
Comparable
interface.
ANSWERS TO SELF-CHECK QUESTIONS
1.
HashMap<String, Integer>
2. It uses inheritance.
3.
new Pair<String, String>(ÐHelloÑ, ÐWorldÑ)
4.
ListIterator<E>
is now a generic type. Its interface depends on the
element type of the linked list.
5. The output depends on the definition of the
toString
method in the
Bank-Account
class.