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); } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.