Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

4. Consider a member method for the Set ADT called singletons(). Given a set A = {XO, X1,...,xn), the singleton set of A is a

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
4. Consider a member method for the Set ADT called singletons(). Given a set A = {XO, X1,...,xn), the singleton set of A is a collection of sets (i.e. a set of sets), each containing only one element of A: S = {{xo}, {x1},...., {x}}. The prototype for the method is as follows: public Set> singletonSets() Add this method to the Set interface, then implement it in the StaticSet and DynamicSet classes. For the DynamicSet class, simply return the result generated by the StaticSet instance. Test your code! Hint: Set Set> result = new StaticSet>(this.size()); @Override public Set> singletonSets() { return null; 3 public interface Set extends Iterable 1 MOOOOOO public boolean add(E obj); public boolean isMember(E obj) public boolean remove(E obj); public boolean is Empty(); public int size(); public void clear(); public Set union(Set S2): public Set difference(Set 52); public Set intersection(Set S2): public boolean isSubSet(Set S2): public boolean equals(Set $2); public Set> singletonsets (: 16 import java.util.Iterator;0 public class StaticSet implements Set { // current number of elements in set, private int currentSize; w 100 // array of elements private E elements[]; private static final int DEFAULT_SET_SIZE = 10; WNEO00 public staticSet(int maxCapacity) { if (maxCapacity implements Iterator { private int current Position; public SetIterator() this.currentPosition = 0; 6 CO O public class DynamicSet implements Set { // static set private StaticSet theSet; // current max capacity private int maxCapacity; private static final int DEFAULT_SET_SIZE = 10; public DynamicSet(int initialCapacity) { this.theSet = new StaticSet(initialCapacity); this.maxCapacity = initialCapacity; @Override public Iterator iterator() { return theSet.iterator(); @Override public boolean add(E obj) { if (this.isMember(obj)) // Avoid extending the set unnecessarily return false; if (this.maxCapacity == this.theSet.size()) this.maxCapacity *= 2; StaticSet newSet = new StaticSet(this.maxCapacity); copySet(theSet, newSet); this.theSet = newSet; return this.theSet.add(obj); private void copySet(Set src, Set dst) { for (object obj: src) dst.add(CE) obi); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

explain what is meant by experiential learning

Answered: 1 week ago

Question

identify the main ways in which you learn

Answered: 1 week ago