Java Reference
In-Depth Information
7.
The
difference
of two collections is a new collection of the entries that would be left in one collection after remov-
ing those that also occur in the second. Add a method
difference
to the interface
BagInterface
for the ADT bag
that returns as a new bag the difference of the bag receiving the call to the method and the bag that is the method's
one argument. Include sufficient comments to fully specify the method.
Note that the difference of two bags might contain duplicate items. For example, if object
x
occurs five times
in one bag and twice in another, the difference of these bags contains
x
three times. Specifically, suppose that
bag1
and
bag2
are
Bag
objects, where
Bag
implements
BagInterface
;
bag1
contains the
String
objects
a
,
b
, and
c
; and
bag2
contains the
String
objects
b
,
b
,
d
, and
e
. After the statement
BagInterface leftOver1 = bag1.difference(bag2);
executes, the bag
leftOver1
contains the strings
a
and
c
. After the statement
BagInterface leftOver2 = bag2.difference(bag1);
executes, the bag
leftOver2
contains the strings
b
,
d
, and
e
. Note that
difference
does not affect the contents of
bag1
and
bag2
.
8.
Write code that accomplishes the following tasks: Consider two bags that can hold strings. One bag is named
letters
and contains several one-letter strings. The other bag is empty and is named
vowels
. One at a time,
remove a string from
letters
. If the string contains a vowel, place it into the bag
vowels
; otherwise, discard
the string. After you have checked all of the strings in
letters
, report the number of vowels in the bag
vowels
and the number of times each vowel appears in the bag.
9.
Write code that accomplishes the following tasks: Consider three bags that can hold strings. One bag is named
letters
and contains several one-letter strings. Another bag is named
vowels
and contains five strings, one for each vowel. The
third bag is empty and is named
consonants
. One at a time, remove a string from
letters
. Check whether the string is
in the bag
vowels
. If it is, discard the string. Otherwise, place it into the bag
consonants
. After you have checked all of
the strings in
letters
, report the number of consonants in the bag
consonants
and the number of times each conso-
nant appears in the bag.
P
ROJECTS
1.
As we mentioned in Segment 1.21, a set is a special bag that does not allow duplicates.
a.
Specify each operation for a set of objects by stating its purpose; by describing its parameters; and by writ-
ing preconditions, postconditions, and a pseudocode version of its header. Then write a Java interface,
SetInterface<T>
, for the set. Include
javadoc
-style comments in your code.
b.
Suppose the class
Set<T>
implements
SetInterface<T>
. Given an empty set that is an object of
Set<String>
and an object of the class
Bag<String>
that contains several strings, write statements at the
client level that create a set from the given bag.
2.
Imagine a pile of topic on your desk. Each book is so large and heavy that you can remove only the top one from
the pile. You cannot remove a book from under another one. Likewise, you can add another book to the pile only
by placing it on the top of the pile. You cannot add a book beneath another one.
If you represent topics by their titles alone, design a class that you can use to track the topics in the pile on
your desk. Specify each operation by stating its purpose, by describing its parameters, and by writing a pseudo-
code version of its header. Then write a Java interface for the pile's methods. Include
javadoc
-style comments in
your code.