SlideShare a Scribd company logo
Complete the class ArraySet-1.java which implements the SetADT.java interface. Implement only
the methods
public Set<E> union(Set<E> set)
public Set<E> intersection(Set<E> set)
ArraySet-1.java
public class ArraySet<E> implements Set<E>, Iterable<E> {
private static Random rand = new Random();
private final int DEFAULT_CAPACITY = 100;
private final int NOT_FOUND = -1;
private int count; // the current number of elements in the set
private E[] contents;
private int modCount;
public ArraySet() {
count = 0;
modCount = 0;
contents = (E[])(new Object[DEFAULT_CAPACITY]);
}
public ArraySet(int initialCapacity) {
count = 0;
contents = (E[])(new Object[initialCapacity]);
}
public boolean add(E element) {
boolean result = false;
if (!(contains(element))) {
if (size() == contents.length) {
expandCapacity();
}
contents[count] = element;
count += 1;
result = true;
}
modCount += 1;
return result;
}
public void addAll(Set<E> set) {
Iterator<E> scan = set.iterator();
while (scan.hasNext()) {
add (scan.next());
}
modCount += 1;
}
public E removeRandom() throws NoSuchElementException {
if (isEmpty()) {
throw new NoSuchElementException("ArraySet");
}
int choice = rand.nextInt(count);
E result = contents[choice];
contents[choice] = contents[count-1]; // fill the gap
contents[count-1] = null;
count--;
modCount += 1;
return result;
}
public E remove(E target) throws NoSuchElementException, NoSuchElementException {
int search = NOT_FOUND;
if (isEmpty()) {
throw new NoSuchElementException("ArraySet");
}
for (int index=0; index < count && search == NOT_FOUND; index += 1) {
if (contents[index].equals(target)) {
search = index;
}
}
if (search == NOT_FOUND) {
throw new NoSuchElementException();
}
E result = contents[search];
contents[search] = contents[count-1];
contents[count-1] = null;
count--;
modCount += 1;
return result;
}
public Set<E> union(Set<E> set) {
// Implement this method
return null; // Replace when implementinging this method
}
public Set<E> difference(Set<E> set) {
ArraySet<E> diff = new ArraySet<E>();
for (int index = 0; index < count; index += 1) {
if (!(set.contains(contents[index]))) {
diff.add (contents[index]);
}
}
return diff;
}
public Set<E> intersection(Set<E> set) {
// Implement this method
return null; // Replace when implmenting this method
}
public boolean contains (E target) {
int search = NOT_FOUND;
for (int index=0; index < count && search == NOT_FOUND; index += 1) {
if (contents[index].equals(target)) {
search = index;
}
}
return (search != NOT_FOUND);
}
public boolean equals(Set<E> set) {
boolean result = false;
ArraySet<E> temp1 = new ArraySet<E>();
ArraySet<E> temp2 = new ArraySet<E>();
E element;
if (size() == set.size()) {
temp1.addAll(this);
temp2.addAll(set);
Iterator<E> scan = set.iterator();
while (scan.hasNext()) {
element = scan.next();
if (temp1.contains(element)) {
temp1.remove(element);
temp2.remove(element);
}
}
result = (temp1.isEmpty() && temp2.isEmpty());
}
return result;
}
public boolean isEmpty() {
return (count == 0);
}
public int size() {
return count;
}
public Iterator<E> iterator() {
return new ArraySetIterator();
}
public String toString() {
String result = "";
for (int index=0; index < count; index += 1) {
result = result + contents[index].toString() + "n";
}
return result;
}
private void expandCapacity() {
E[] larger = (E[])(new Object[contents.length*2]);
for (int index=0; index < contents.length; index += 1) {
larger[index] = contents[index];
}
contents = larger;
}
private class ArraySetIterator implements Iterator<E> {
int iteratorModCount;
int current;
public ArraySetIterator() {
iteratorModCount = modCount;
current = 0;
}
public boolean hasNext() throws ConcurrentModificationException {
if (iteratorModCount != modCount) {
throw new ConcurrentModificationException();
}
return (current < count);
}
public E next() throws ConcurrentModificationException {
if (!hasNext()) {
throw new NoSuchElementException();
}
current += 1;
return contents[current - 1];
}
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
}
}
SetADT.java
public interface SetADT<T>
{
/**
* Adds one element to this set, ignoring duplicates.
*
* @param element the element to be added to this set
* @return true if the element was successfully added
*/
public boolean add(T element);
/**
* Removes and returns a random element from this set.
*
* @return a random element from this set
*/
public T removeRandom();
/**
* Removes and returns the specified element from this set.
*
* @param element the element to be removed from this list
* @return the element just removed from this list
*/
public T remove(T element);
/**
* Returns the union of this set and the parameter
*
* @param set the set to be unioned with this set
* @return a set that is the union of this set and the parameter
*/
public SetADT<T> union(SetADT<T> set);
/**
* Returns true if this set contains the parameter
*
* @param target the element being sought in this set
* @return true if this set contains the parameter
*/
public boolean contains(T target);
/**
* Returns true if this set and the parameter contain exactly
* the same elements
*
* @param set the set to be compared with this set
* @return true if this set and the parameter contain exactly
* the same elements
*/
public boolean equals(SetADT<T> set);
/**
* Returns true if this set contains no elements
*
* @return true if this set contains no elements
*/
public boolean isEmpty();
/**
* Returns the number of elements in this set
*
* @return the integer number of elements in this set
*/
public int size();
/**
* Returns an iterator for the elements in this set
*
* @return an iterator for the elements in this set
*/
public Iterator<T> iterator();
/**
* Returns a string representation of this set
*
* @return a string representation of this set
*/
public String toString();
}

More Related Content

PDF
Here is what I got so far, I dont know how to write union, interse.pdf
PDF
I really need help with the code for this in Java.Set operations u.pdf
PDF
package lab7 public class SetOperations public static.pdf
PDF
1 The goal is to implement DataStructuresArrayStack accor.pdf
PDF
6. Generics. Collections. Streams
PDF
package ADTs public interface CollectionADTltTgt .pdf
PDF
I need help creating a parametized JUnit test case for the following.pdf
PDF
Please create the appropriate JUnit test cases to thoroughly.pdf
Here is what I got so far, I dont know how to write union, interse.pdf
I really need help with the code for this in Java.Set operations u.pdf
package lab7 public class SetOperations public static.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf
6. Generics. Collections. Streams
package ADTs public interface CollectionADTltTgt .pdf
I need help creating a parametized JUnit test case for the following.pdf
Please create the appropriate JUnit test cases to thoroughly.pdf

Similar to Complete the class ArraySet1java which implements the SetA.pdf (19)

PPTX
Set data structure 2
PDF
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
PPTX
Set data structure
PDF
This file contains a complete array-based MultiSet, but not the code.pdf
DOCX
JAVA - Design a data structure IntSet that can hold a set of integers-.docx
PDF
this file has a complete array-based MultiSet, but not the code need.pdf
PDF
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
PDF
Refer to my progress on this assignment belowIn this problem you w.pdf
DOCX
A and B are sets. Create at set D with properties D (A B) where D A.docx
PDF
Suppose that you and I are friends on Facebook. and you want to figur.pdf
PDF
we using java code DynamicArrayjava Replace all .pdf
PDF
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
PDF
public class DoubleArraySeq implements Cloneable {    Priva.pdf
PDF
Hello need help on this lab- what you need to do is add a code to Arra.pdf
PPTX
Lecture 6 disjoint set
PDF
Header file for an array-based implementation of the ADT bag. @f.pdf
PPTX
Overlap Add, Overlap Save(digital signal processing)
PDF
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
DOCX
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
Set data structure 2
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
Set data structure
This file contains a complete array-based MultiSet, but not the code.pdf
JAVA - Design a data structure IntSet that can hold a set of integers-.docx
this file has a complete array-based MultiSet, but not the code need.pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
Refer to my progress on this assignment belowIn this problem you w.pdf
A and B are sets. Create at set D with properties D (A B) where D A.docx
Suppose that you and I are friends on Facebook. and you want to figur.pdf
we using java code DynamicArrayjava Replace all .pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
public class DoubleArraySeq implements Cloneable {    Priva.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdf
Lecture 6 disjoint set
Header file for an array-based implementation of the ADT bag. @f.pdf
Overlap Add, Overlap Save(digital signal processing)
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx

More from abbecindia (20)

PDF
Computer connects to the internet through an a Internet Ser.pdf
PDF
Complete the Programming of a Simple ObjectOriented Applica.pdf
PDF
Complete the following tasks 1 Install and run an Apache s.pdf
PDF
Computational question 5 ABC company offered to sell goo.pdf
PDF
Complete the following statements 1 The umbilical cord con.pdf
PDF
Computer Organization Q1 Consider the following RISCV loop.pdf
PDF
Complete the table with types of phagocytes found at various.pdf
PDF
Complete the table How do the following organs act as barri.pdf
PDF
Computer Organization Q7 Using an 8bit Register perform t.pdf
PDF
Computer Science Illuminated book Lab Chapter12 Using the .pdf
PDF
Consider a relation R with five attributes ABCDE You are gi.pdf
PDF
Computer Organization Q2 Multiply the following 841110 .pdf
PDF
Consider a fund with 120 million in assets at the start of .pdf
PDF
Consider a distributed hash table implemented via the Chord .pdf
PDF
Consider a Boolean function fx y z defined by the input.pdf
PDF
Complete the following tasks in order on CorpDC 1 Create a.pdf
PDF
Complete the following table by checking the characteristics.pdf
PDF
Consider 2 countries Ireland and Greece with the same pref.pdf
PDF
COMPUTER SCIENCE Sub Computer Organization and Architectur.pdf
PDF
configure static routes on the routers so that PCO can ping .pdf
Computer connects to the internet through an a Internet Ser.pdf
Complete the Programming of a Simple ObjectOriented Applica.pdf
Complete the following tasks 1 Install and run an Apache s.pdf
Computational question 5 ABC company offered to sell goo.pdf
Complete the following statements 1 The umbilical cord con.pdf
Computer Organization Q1 Consider the following RISCV loop.pdf
Complete the table with types of phagocytes found at various.pdf
Complete the table How do the following organs act as barri.pdf
Computer Organization Q7 Using an 8bit Register perform t.pdf
Computer Science Illuminated book Lab Chapter12 Using the .pdf
Consider a relation R with five attributes ABCDE You are gi.pdf
Computer Organization Q2 Multiply the following 841110 .pdf
Consider a fund with 120 million in assets at the start of .pdf
Consider a distributed hash table implemented via the Chord .pdf
Consider a Boolean function fx y z defined by the input.pdf
Complete the following tasks in order on CorpDC 1 Create a.pdf
Complete the following table by checking the characteristics.pdf
Consider 2 countries Ireland and Greece with the same pref.pdf
COMPUTER SCIENCE Sub Computer Organization and Architectur.pdf
configure static routes on the routers so that PCO can ping .pdf

Recently uploaded (20)

PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
RMMM.pdf make it easy to upload and study
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Presentation on HIE in infants and its manifestations
PPTX
master seminar digital applications in india
PDF
01-Introduction-to-Information-Management.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
RMMM.pdf make it easy to upload and study
Supply Chain Operations Speaking Notes -ICLT Program
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Chinmaya Tiranga quiz Grand Finale.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
FourierSeries-QuestionsWithAnswers(Part-A).pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Complications of Minimal Access Surgery at WLH
Microbial diseases, their pathogenesis and prophylaxis
Presentation on HIE in infants and its manifestations
master seminar digital applications in india
01-Introduction-to-Information-Management.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
VCE English Exam - Section C Student Revision Booklet
102 student loan defaulters named and shamed – Is someone you know on the list?

Complete the class ArraySet1java which implements the SetA.pdf

  • 1. Complete the class ArraySet-1.java which implements the SetADT.java interface. Implement only the methods public Set<E> union(Set<E> set) public Set<E> intersection(Set<E> set) ArraySet-1.java public class ArraySet<E> implements Set<E>, Iterable<E> { private static Random rand = new Random(); private final int DEFAULT_CAPACITY = 100; private final int NOT_FOUND = -1; private int count; // the current number of elements in the set private E[] contents; private int modCount; public ArraySet() { count = 0; modCount = 0; contents = (E[])(new Object[DEFAULT_CAPACITY]); } public ArraySet(int initialCapacity) { count = 0; contents = (E[])(new Object[initialCapacity]); } public boolean add(E element) { boolean result = false; if (!(contains(element))) { if (size() == contents.length) { expandCapacity(); } contents[count] = element; count += 1; result = true; } modCount += 1; return result; } public void addAll(Set<E> set) { Iterator<E> scan = set.iterator(); while (scan.hasNext()) { add (scan.next()); } modCount += 1; } public E removeRandom() throws NoSuchElementException {
  • 2. if (isEmpty()) { throw new NoSuchElementException("ArraySet"); } int choice = rand.nextInt(count); E result = contents[choice]; contents[choice] = contents[count-1]; // fill the gap contents[count-1] = null; count--; modCount += 1; return result; } public E remove(E target) throws NoSuchElementException, NoSuchElementException { int search = NOT_FOUND; if (isEmpty()) { throw new NoSuchElementException("ArraySet"); } for (int index=0; index < count && search == NOT_FOUND; index += 1) { if (contents[index].equals(target)) { search = index; } } if (search == NOT_FOUND) { throw new NoSuchElementException(); } E result = contents[search]; contents[search] = contents[count-1]; contents[count-1] = null; count--; modCount += 1; return result; } public Set<E> union(Set<E> set) { // Implement this method return null; // Replace when implementinging this method } public Set<E> difference(Set<E> set) { ArraySet<E> diff = new ArraySet<E>(); for (int index = 0; index < count; index += 1) { if (!(set.contains(contents[index]))) { diff.add (contents[index]); } }
  • 3. return diff; } public Set<E> intersection(Set<E> set) { // Implement this method return null; // Replace when implmenting this method } public boolean contains (E target) { int search = NOT_FOUND; for (int index=0; index < count && search == NOT_FOUND; index += 1) { if (contents[index].equals(target)) { search = index; } } return (search != NOT_FOUND); } public boolean equals(Set<E> set) { boolean result = false; ArraySet<E> temp1 = new ArraySet<E>(); ArraySet<E> temp2 = new ArraySet<E>(); E element; if (size() == set.size()) { temp1.addAll(this); temp2.addAll(set); Iterator<E> scan = set.iterator(); while (scan.hasNext()) { element = scan.next(); if (temp1.contains(element)) { temp1.remove(element); temp2.remove(element); } } result = (temp1.isEmpty() && temp2.isEmpty()); } return result; } public boolean isEmpty() { return (count == 0); } public int size() { return count; } public Iterator<E> iterator() {
  • 4. return new ArraySetIterator(); } public String toString() { String result = ""; for (int index=0; index < count; index += 1) { result = result + contents[index].toString() + "n"; } return result; } private void expandCapacity() { E[] larger = (E[])(new Object[contents.length*2]); for (int index=0; index < contents.length; index += 1) { larger[index] = contents[index]; } contents = larger; } private class ArraySetIterator implements Iterator<E> { int iteratorModCount; int current; public ArraySetIterator() { iteratorModCount = modCount; current = 0; } public boolean hasNext() throws ConcurrentModificationException { if (iteratorModCount != modCount) { throw new ConcurrentModificationException(); } return (current < count); } public E next() throws ConcurrentModificationException { if (!hasNext()) { throw new NoSuchElementException(); } current += 1; return contents[current - 1]; } public void remove() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } } }
  • 5. SetADT.java public interface SetADT<T> { /** * Adds one element to this set, ignoring duplicates. * * @param element the element to be added to this set * @return true if the element was successfully added */ public boolean add(T element); /** * Removes and returns a random element from this set. * * @return a random element from this set */ public T removeRandom(); /** * Removes and returns the specified element from this set. * * @param element the element to be removed from this list * @return the element just removed from this list */ public T remove(T element); /** * Returns the union of this set and the parameter * * @param set the set to be unioned with this set * @return a set that is the union of this set and the parameter */ public SetADT<T> union(SetADT<T> set); /** * Returns true if this set contains the parameter * * @param target the element being sought in this set * @return true if this set contains the parameter */ public boolean contains(T target); /** * Returns true if this set and the parameter contain exactly * the same elements * * @param set the set to be compared with this set
  • 6. * @return true if this set and the parameter contain exactly * the same elements */ public boolean equals(SetADT<T> set); /** * Returns true if this set contains no elements * * @return true if this set contains no elements */ public boolean isEmpty(); /** * Returns the number of elements in this set * * @return the integer number of elements in this set */ public int size(); /** * Returns an iterator for the elements in this set * * @return an iterator for the elements in this set */ public Iterator<T> iterator(); /** * Returns a string representation of this set * * @return a string representation of this set */ public String toString(); }