Java Reference
In-Depth Information
P ROGRAMMING T IP
Writing Java statements that test a class's methods will help you to fully understand the specifications for the
methods. Obviously, you must understand a method before you can implement it correctly. If you are also
the class designer, your use of the class might help you see desirable changes to your design or its documen-
tation. You will save time if you make these revisions before you have implemented the class. Since you
must write a program that tests your implementation sometime, why not get additional benefits from the task
by writing it now instead of later?
E XERCISES
1.
Specify each method of the class PiggyBank , as given in Listing 1-3, by stating the method's purpose; by
describing its parameters; and by writing preconditions, postconditions, and a pseudocode version of its header.
Then write a Java interface for these methods that includes javadoc -style comments.
2.
Suppose that groceryBag is a bag filled to its capacity with 10 strings that name various groceries. Write Java state-
ments that remove and count all occurrences of "soup" in groceryBag . Do not remove any other strings from the
bag. Report the number of times that "soup" occurred in the bag. Accommodate the possibility that groceryBag
does not contain any occurrence of "soup" .
3.
Given groceryBag , as described in Exercise 2, what effect does the operation groceryBag.toArray() have on
groceryBag ?
4.
Given groceryBag , as described in Exercise 2, write some Java statements that create an array of the distinct strings
that are in this bag. That is, if "soup" occurs three times in groceryBag , it should only appear once in your array.
After you have finished creating this array, the contents of groceryBag should be unchanged.
5.
The union of two collections consists of their contents combined into a new collection. Add a method union to the
interface BagInterface for the ADT bag that returns as a new bag the union 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 union of two bags might contain duplicate items. For example, if object x occurs five times in one
bag and twice in another, the union of these bags contains x seven 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<String> everything = bag1.union(bag2);
executes, the bag everything contains the strings a , b , b , b , c , d , and e . Note that union does not affect the con-
tents of bag1 and bag2 .
6.
The intersection of two collections is a new collection of the entries that occur in both collections. That is, it con-
tains the overlapping entries. Add a method intersection to the interface BagInterface for the ADT bag that
returns as a new bag the intersection 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 intersection of two bags might contain duplicate items. For example, if object x occurs five
times in one bag and twice in another, the intersection of these bags contains x twice. 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<String> commonItems = bag1.intersection(bag2);
executes, the bag commonItems contains only the string b . If b had occurred in bag1 twice, commonItems would
have contained two occurrences of b , since bag2 also contains two occurrences of b . Note that intersection does
not affect the contents of bag1 and bag2 .
 
Search WWH ::




Custom Search