/* *Johnny A. Shaieb *Program 1 *cs2003 */ public class Set extends ListArrayBased { //Default Constructor public Set() { super(); } //Object Constructor public Set(Object obj) { super(obj); } //Copy Constructor public Set(Set set) { super(set); } //return the size of the set public int setSize() { return (super.size()); } //print out the contents of the set //A.print(); public void print() { super.display(); } //Set E = A.diff(B); public Set diff(Set aset) { //This will call the copy constructor Set diffset = new Set(this); //initialize the index position int index = 0; for (int i = 1; i<=aset.setSize(); i++) { //assign index a value //index is the position that will be deleted //If position exists then remove it if((index = diffset.contains(aset.get(i))) != -1) { diffset.remove(index); } } return diffset; } //I use this in insert but I could use contains also public boolean in(Object obj_element) { //if the object is not in the list return false if (contains(obj_element) == -1) { return false; } else { //if not equal to -1, then true return true; } } //A.insert(new Integer(10)); //Here you just insert an item at a time public void insert(Object obj) { // If item is contained in the current list, return its index // otherwise return -1 if item is not in the list if(!(in(obj))) { super.append(obj); } else { System.out.println("Object is a duplicate,, cannot add/append to list"); } } //A.arrayInsert(M); //Here you will use insert to M into A public void arrayInsert(Object[] aset) { for (int i = 0; i < aset.length; i++) { insert(aset[i]); } } //Set C = A.union(B); public Set union(Set aset) { //Create a Set object Set unionset = new Set(); //need to populate the unionset for(int i = 1; i <= setSize(); i++) { unionset.insert(super.get(i)); } //Do not have to compare before insert //Because insert checks for us for(int j = 1; j <= aset.size(); j++) { unionset.insert(aset.get(j)); } return unionset; } //Set D = A.intersect(B); public Set intersect(Set aset) { //Set D = A.intersect(B); Set interset = new Set(); for (int i = 1; i <= aset.setSize(); i++) { if (super.contains(aset.get(i)) == -1) { System.out.println("Object is not found, so no intersection"); } else { interset.append(aset.get(i)); } } return interset; } }