Question
Assignment1: Implementing Sets MUST BE DONE WITH JAVA A set is a collection of elements in which no element occurs more than once (distinct elements).
Assignment1: Implementing Sets
MUST BE DONE WITH JAVA
A set is a collection of elements in which no element occurs more than once (distinct elements). We can implement a simple set as a linked list to store the items in the set.
Fundamental Set operations are add(E element), remove(E element), union(Set otherSet), intersection(Set otherSet) and difference(Set otherSet)
Implement the MySet class as depicted by the figure below.
Node
Element: E
Next: Node
------------------------------------------------------
<> MyList
^^^^^^^^^^^^^^^^^^^^^
MySet
head
tail
size
+ MySet()
+ MySet(arr : E[])
+ MySet(otherList : List)
+ Boolean Add(element:E): boolean
+ Boolean Remove(element:E): boolean
+ union(otherSet: MySet): MySet
+ intersection(otherSet: MySet): MySet
+ difference(otherSet: MySet): MySet
The class should implement the MyList interface (all abstract methods defined in MyList should be implemented in MySet) and should contain three constructors to create an empty set, create a set from an array of objects or create a set from a List (ArrayList, LinkedList).
Define and implement the following methods in MySet:
Boolean add(E element) : add element to the set, return true if element is added
Boolean remove(E element): remove element from the set, return true if element is found and removed
MySet union(MySet otherSet): return a set containing elements from both sets
MySet Intersection(MySet otherSet): return the intersection of two sets
MySet Difference(MySet otherSet): return the difference of two sets
Write a test program that creates Three MySet instances, set1, set2 and set3 and show that the three constructors and all methods implemented in MySet class are working properly.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started