Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

An ADT of a Set can use a Bag since order is not important in a set BUT it canno

ID: 3772490 • Letter: A

Question

An ADT of a Set can use a Bag since order is not important in a set BUT it cannot have any duplicates and needs to have a Union (combine the current Set object with a Set object passed to it).

a) Write the class Set that inherits the Bag and adds the Union function

b) Now write the implementation of the Set but have the add only add an element if it does not already exist in the set. Otherwise have it call the Bag’s add. Have the other Set functions call the Bag’s functions. Don’t forget the Union function

Explanation / Answer

class Bag{ List list = new ArrayList(); void add(Object num){ list.add(num); } } class Set extends Bag{ void union(Set set){ for(Object o : set.list){ this.list.add(o); } } void add(Object num){ if(!list.contains(num)){ add(num); } } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote