SlideShare a Scribd company logo
1 of 6
Download to read offline
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

Similar to Complete the class ArraySet1java which implements the SetA.pdf

Please complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxPlease complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docx
cgraciela1
ย 
import java-util--- public class MyLinkedList{ public static void.pdf
import java-util---  public class MyLinkedList{    public static void.pdfimport java-util---  public class MyLinkedList{    public static void.pdf
import java-util--- public class MyLinkedList{ public static void.pdf
asarudheen07
ย 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdf
maheshkumar12354
ย 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
VictorXUQGloverl
ย 
package lab7 public class SetOperations public static.pdf
package lab7     public class SetOperations  public static.pdfpackage lab7     public class SetOperations  public static.pdf
package lab7 public class SetOperations public static.pdf
syedabdul78662
ย 
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docxJAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
GavinUJtMathist
ย 
Here is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdfHere is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdf
arihantpatna
ย 
we using java code DynamicArrayjava Replace all .pdf
we using java code   DynamicArrayjava   Replace all .pdfwe using java code   DynamicArrayjava   Replace all .pdf
we using java code DynamicArrayjava Replace all .pdf
gudduraza28
ย 
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdfNote- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
Augstore
ย 
public class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdfpublic class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdf
accostinternational
ย 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdf
aksahnan
ย 
Note- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxNote- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docx
VictorzH8Bondx
ย 
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdfClass DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
xlynettalampleyxc
ย 
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
arshin9
ย 
1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf
saradashata
ย 
Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
Stewart29UReesa
ย 

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

Please complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxPlease complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docx
ย 
import java-util--- public class MyLinkedList{ public static void.pdf
import java-util---  public class MyLinkedList{    public static void.pdfimport java-util---  public class MyLinkedList{    public static void.pdf
import java-util--- public class MyLinkedList{ public static void.pdf
ย 
i am looking for help on the method AddSorted and the method Copy only.pdf
i am looking for help on the method AddSorted and the method Copy only.pdfi am looking for help on the method AddSorted and the method Copy only.pdf
i am looking for help on the method AddSorted and the method Copy only.pdf
ย 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdf
ย 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
ย 
package lab7 public class SetOperations public static.pdf
package lab7     public class SetOperations  public static.pdfpackage lab7     public class SetOperations  public static.pdf
package lab7 public class SetOperations public static.pdf
ย 
Write a program to find the number of comparisons using the binary se.docx
 Write a program to find the number of comparisons using the binary se.docx Write a program to find the number of comparisons using the binary se.docx
Write a program to find the number of comparisons using the binary se.docx
ย 
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docxJAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
ย 
Here is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdfHere is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdf
ย 
we using java code DynamicArrayjava Replace all .pdf
we using java code   DynamicArrayjava   Replace all .pdfwe using java code   DynamicArrayjava   Replace all .pdf
we using java code DynamicArrayjava Replace all .pdf
ย 
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdfNote- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
ย 
public class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdfpublic class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdf
ย 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdf
ย 
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdfImplement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
ย 
Note- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxNote- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docx
ย 
Please review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdfPlease review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdf
ย 
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdfClass DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
ย 
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
ย 
1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf
ย 
Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
ย 

More from abbecindia

More from abbecindia (20)

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

Recently uploaded

Personalisation of Education by AI and Big Data - Lourdes Guร rdia
Personalisation of Education by AI and Big Data - Lourdes Guร rdiaPersonalisation of Education by AI and Big Data - Lourdes Guร rdia
Personalisation of Education by AI and Big Data - Lourdes Guร rdia
EADTU
ย 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
ย 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
ย 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
ย 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MysoreMuleSoftMeetup
ย 
ๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝ
ๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝ
ๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝ
ไธญ ๅคฎ็คพ
ย 

Recently uploaded (20)

Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
ย 
Personalisation of Education by AI and Big Data - Lourdes Guร rdia
Personalisation of Education by AI and Big Data - Lourdes Guร rdiaPersonalisation of Education by AI and Big Data - Lourdes Guร rdia
Personalisation of Education by AI and Big Data - Lourdes Guร rdia
ย 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
ย 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
ย 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
ย 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
ย 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
ย 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
ย 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
ย 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
ย 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
ย 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
ย 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
ย 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
ย 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
ย 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
ย 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
ย 
ๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝ
ๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝ
ๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝๆœƒ่€ƒ่‹ฑ่ฝ
ย 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
ย 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
ย 

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(); }