Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribedjoker1234 answered this

Was this answer helpful?

1

0

200 answers

changes :

1)you have to use parametrized class definition for e.g class Set where T specifies trhe type.

so when you instantiate like for e.g Set set=new Set(10) .....T will be replaced by Ineteger.

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 elements;

/**

* 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 union(Set set) throws SetException {

Set unionSet = new Set(this.maxSize + set.maxSize);

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 intersection(Set set) throws SetException {

Set intersectionSet = new Set(Math.min(this.cardinality, set.cardinality));

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

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

Excel As Your Database

Authors: Paul Cornell

1st Edition

1590597516, 978-1590597514

More Books

Students also viewed these Databases questions

Question

What is the organizational design of Home2 Suites by Hilton?

Answered: 1 week ago

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago