Question
How to change this set code from integer to Generic? public class IntegerSet { private int cardinality; private final int maxSize; private final int[] elements;
How to change this set code from integer to Generic?
public class IntegerSet {
private int cardinality; private final int maxSize; private final int[] elements;
/** * No arg constructor * * @param maxSize */ IntegerSet(int maxSize) { this.cardinality = 0; this.maxSize = maxSize; this.elements = new int[maxSize]; }
/** * * @param set to take the union with * @return the union of the two sets * @throws SetException */ public IntSet union(IntSet set) throws SetException { IntSet unionSet = new IntSet(this.maxSize + set.maxSize);
int marker1 = 0, marker2 = 0;
while (marker1
for (; marker1
for (; marker2
return unionSet; }
/** * * @param set to take the intersection with * @return the intersection of the two sets * @throws SetException */ public IntSet intersection(IntSet set) throws SetException { IntSet intersectionSet = new IntSet(Math.min(this.cardinality, set.cardinality));
int marker1 = 0, marker2 = 0;
while (marker1
return intersectionSet;
}
/** * * @param newElement to be inserted * @throws SetException */ public void insert(int newElement) throws SetException { if (cardinality >= maxSize) { throw new SetException("cannot insert..set full"); } else if (!contains(newElement)) { int marker = cardinality;
while (marker > 0 && newElement
/** * * @param testElement to be checked * @return true if element exists */ public boolean contains(int testElement) { int marker;
for (marker = 0; marker
/** * Clear the set */ public void clear() { cardinality = 0; }
/** * * @return the cardinality of the set */ public int getCardinality() { return this.cardinality; }
/** * Print the content of the set */ public void print() { for (int marker = 0; marker
}
Expert Answer
joker1234 answered this
Was this answer helpful?
1
0
200 answers
changes :
1)you have to use parametrized class definition for e.g class Set
so when you instantiate like for e.g Set
2)you have to use arraylist insread of traditional array since it does not work with generic types
3)you have to make sure the The Types you use your set class with ,have compareTo() method either implemented already or you have to implement incase oif user defined types i.e they must implement Comparable Interface.
usage of compareTo(obj2)
obj1.compareTo(obj2)
return -1 when obj1 returns 0 when obj1==obj2 returns 1 when obj1>2 ------------------------------- import java.util.ArrayList; public class Set private int cardinality; private int maxSize; private ArrayList /** * No arg constructor * * @param maxSize */ Set(int maxSize) { this.cardinality = 0; this.maxSize = maxSize; this.elements = new ArrayList elements.ensureCapacity(maxSize); } /** * * @param set to take the union with * @return the union of the two sets * @throws SetException */ public Set Set int marker1 = 0, marker2 = 0; while (marker1 if ( elements.get(marker1)).compareTo(set.elements.get(marker1)) unionSet.insert(elements.get(marker1)); marker1++; } else if (elements.get(marker1).compareTo(set.elements.get(marker1))==-1) { unionSet.insert(elements.get(marker1)); marker1++; marker2++; } else { unionSet.insert(set.elements.get(marker2)); marker2++; } } for (; marker1 unionSet.insert(elements.get(marker1)); } for (; marker2 unionSet.insert(set.elements.get(marker2)); } return unionSet; } /** * * @param set to take the intersection with * @return the intersection of the two sets * @throws SetException */ public Set Set int marker1 = 0, marker2 = 0; while (marker1 if (elements.get(marker1).compareTo(set.elements.get(marker1))==-1) { marker1++; } else if (elements.get(marker1).compareTo(set.elements.get(marker1))==0) { intersectionSet.insert(this.elements.get(marker1)); marker1++; marker2++; } else { marker2++; } } return intersectionSet; } /** * * @param newElement to be inserted * @throws SetException */ public void insert(T newElement) throws SetException { if (cardinality >= maxSize) { throw new SetException("cannot insert..set full"); } else if (!contains(newElement)) { int marker = cardinality; while (marker > 0 && newElement.compareTo(elements.get(marker-1))==-1) { elements.set(marker,elements.get(marker - 1)); --marker; } elements.set(marker, newElement) ; cardinality++; } } /** * * @param testElement to be checked * @return true if element exists */ public boolean contains(T testElement) { int marker; for (marker = 0; marker if (testElement.equals(elements.get(marker))) { return true; } } return false; } /** * Clear the set */ public void clear() { cardinality = 0; } /** * * @return the cardinality of the set */ public int getCardinality() { return this.cardinality; } /** * Print the content of the set */ public void print() { for (int marker = 0; marker System.out.print(elements.get(marker) + " "); } System.out.println(); } }
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