Article

Chapter 13 Exercise Solutions Java Software Structures

by anonymous

EX 13.1       Define the concept of a set. List additional operations that might be considered for a set.

A set is a collection of elements with no duplicates.  Generally, the elements in sets have no particular order relative to each other and no inherent relationship.  Example additional operations that might be considered for a set might include viewing a random element from the set without removing it, checking to see if a particular subset of elements exist within the set, or an implementation of an intersection operation.

 

EX 13.2       The TreeSet class is built upon a backing instance of the TreeMap class. Discuss the advantages and disadvantages of this strategy for reuse.

 

By using a TreeMap to implement the TreeSet class, much of the common functionality can be shared. The TreeMap has more complex functionality, but by keeping it constrained to one set of values it eliminates the need for a second full implementation.

 

EX 13.3       Given the nature of a set, one could implement the Set interface using any one of a variety of other collections or data structures. Describe how you might implement the Set interface using a LinkedList. Discuss the advantages and disadvantages of this approach.

 

A LinkedList would be a fairly straightforward implementation, although not particularly efficient, strategy for a set. The add method would check for duplicates and only add unique items. Otherwise, most operations could come directly from the list implementation. However, the operations are not nearly as efficient as the tree-based solutions in the API.

 

 

EX 13.4       A bag is a very similar construct to a set except that duplicates are allowed in a bag. What changes would have to be made to extend a TreeSet to create an implementation of a bag?

The primary change would be to remove the duplicate restriction in the add method.  Since the addAll method, and then indirectly the union method, use the add method, this one change will allow duplicates.  The definitions of other methods would need to be examined as well.  For example, should the remove method delete the first occurrence of an element or all occurences of an element?

 

 

EX 13.5       Draw a UML diagram showing the relationships among the classes involved in the Product Sales example from this chapter.

 

The answer to this exercise is currently not provided.

 

 

EX 13.6       Draw a UML diagram showing the relationships among the classes involved in the User Management example from this chapter.

 

The answer to this exercise is currently not provided.

 

 

EX 13.7       Describe two hashing functions that might be appropriate for a data set organized by name (e.g. last name, first name, middle initial).

 

The answer to this exercise is currently not provided.

 

 

EX 13.8       Explain when it might be preferable to use a map instead of a set.

 

Maps are particularly helpful for relating keys and values, whereas sets focus on containment in one large group. If the goal is to look up information by some id, a map will serve better. A set would be better for, say, seeing if a product is part of the currently available items.

 


1 0
Free Homework App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers