- Engineering
- Computer Science
- create a class for a set of integers similar to...
Question: create a class for a set of integers similar to...
Question details
Create a class for a “set” of integers, similar to IntLinkedBag.java from the textbook, except that only one copy of a value can be stored (no duplicates). You may directly copy any appropriate code from IntLinkedBag.java. Include all of the same methods given in problem #1 above, and answer the given questions in your report. **Use LinkedList to solve this question**
a. A “main” method creates two sets and demonstrates all of the
methods below.
b. Modify the “add” method such that it does not add duplicate
elements. Use the “countOccurrences” method to determine whether
the element already exists.
QUESTION: What is the Big-O time for this entire algorithm,
including the operations for “countOccurrences”? Briefly explain
your answer.
c. A public “print” method prints all of the values in the set in
the order in which they are stored (index #0 is first). Below is an
example of calling “print” for a set with the values {3, 1, 0,
2}:
3 1 0 2
QUESTION: What is the Big-O time for this method? Briefly explain
your answer.
d. A private “get” method receives an index for an item in the set
and returns that element. For a set with the values 3, 1, 0, and 2,
calling “get(2)” would return 0. Throw RuntimeException if the
index is invalid (see the “clone” method in IntArrayBag.java for an
example).
QUESTION: What is the Big-O time for this method? Briefly explain
your answer.
e. A public static “intersection” method receives two sets and
returns a new set that is the intersection (all common elements).
Note that this is similar to the format of the “union” method in
the sample program. For your algorithm, use the “get” method from
above to get elements from one set and the “countOccurrences”
method to determine whether that element is in the second set. For
example, the intersection sets {3, 1, 0, 2} and {1, 3, 4} would
consist of the values 1 and 3 (in any order). If there are no
common elements, the returned set should be empty (size of
zero).
Solution by an expert tutor
