Java Reference
In-Depth Information
16.7
Briefly answer the following questions:
a) What is the primary difference between a
Set
and a
Map
?
b) What happens when you add a primitive type (e.g.,
double
) value to a collection?
c) Can you print all the elements in a collection without using an
Iterator
? If yes, how?
16.8
Explain briefly the operation of each of the following
Iterator
-related methods:
a)
iterator
b)
hasNext
c)
next
16.9
Explain briefly the operation of each of the following methods of class
HashMap
:
a)
put
b)
get
c)
isEmpty
d)
containsKey
e)
keySet
16.10
Determine whether each of the following statements is
true
or
false
. If
false
, explain why.
a) Elements in a
Collection
must be sorted in ascending order before a
binarySearch
may
be performed.
b) Method
first
gets the first element in a
TreeSet
.
c) A
List
created with
Arrays
method
asList
is resizable.
16.11
Explain the operation of each of the following methods of the
Properties
class:
a)
load
b)
store
c)
getProperty
d)
list
16.12
Rewrite lines 16-25 in Fig. 16.3 to be more concise by using the
asList
method and the
LinkedList
constructor that takes a
Collection
argument.
16.13
(Duplicate Elimination)
Write a program that reads in a series of first names and eliminates
duplicates by storing them in a
Set
. Allow the user to search for a first name.
16.14
(Counting Letters)
Modify the program of Fig. 16.18 to count the number of occurrences
of each letter rather than of each word. For example, the string
"HELLO
THERE"
contains two
H
s, three
E
s, two
L
s, one
O
, one
T
and one
R
. Display the results.
16.15
(Color Chooser)
Use a
HashMap
to create a reusable class for choosing one of the 13 pre-
defined colors in class
Color
. The names of the colors should be used as keys, and the predefined
Color
objects should be used as values. Place this class in a package that can be imported into any
Java program. Use your new class in an application that allows the user to select a color and draw a
shape in that color.
16.16
(Counting Duplicate Words)
Write a program that determines and prints the number of
duplicate words in a sentence. Treat uppercase and lowercase letters the same. Ignore punctuation.
16.17
(Inserting Elements in a
LinkedList
in Sorted Order)
Write a program that inserts 25 ran-
dom integers from 0 to 100 in order into a
LinkedList
object. The program should sort the ele-
ments, then calculate the sum of the elements and the floating-point average of the elements.
16.18
(Copying and Reversing
LinkedList
s)
Write a program that creates a
LinkedList
object of
10 characters, then creates a second
LinkedList
object containing a copy of the first list, but in re-
verse order.
16.19
(Prime Numbers and Prime Factors)
Write a program that takes a whole number input
from a user and determines whether it's prime. If the number is not prime, display its unique prime