Hey, so i have this SetADT, and i need to add two methods, Union and Intersection.
- The Union method will return a union of the two sets as an array to the calling program (no duplicates allowed).
- The Intersection method will return the intersection of the two sets as an array to the calling program (no duplicates allowed).
Can anyone guide me in the right direction to adding these two methods?
SetADT.java
package DSA2; public class SetADT { //define fields private int pointer; private int[] setArray; //define methods public SetADT() { pointer = 0; setArray = new int[5]; } public void add(int value) { if (!contains(value)) { //check available space //if not - increase the array size if (pointer == setArray.length) { setArray = increaseSize(); } setArray[pointer] = value; pointer++; } } @Override public String toString() { StringBuilder sb = new StringBuilder(); for(int index=0; index<pointer; index++) { sb.append(setArray[index]); sb.append(", "); } return sb.toString(); }//toString public boolean contains(int value) { for(int index=0; index < setArray.length; index++) { if(setArray[index]== value) { return true; } } return false; }//contains private int[] increaseSize() { int size = setArray.length; int [] newArr = new int[size + 5]; System.arraycopy(setArray, 0, newArr, 0, setArray.length); return newArr; } public int[] toArray() { int [] tmpArray = new int[pointer]; System.arraycopy(setArray, 0, tmpArray,0, pointer); return tmpArray; }//toArray }
Thanks in advance!
Jason.