Question: 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

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!