The purpose of the project is to write code for a generic MySet class, as described below. Based off of my teachers template, I have already written the code that is seen below, as well as written up some test sets for use when the program is done. The problem I am having is understanding how to implement the generics in the add and remove methods mostly, as well as union and intersect. I believe the easiest way to do union and intersect is to have the methods go through their parameters and then return a third arrayList with the results. Can anyone give me some pointers or tips on a good starting point to do the rest of the methods.
import java.util.*; public class MySet<T> implements Set<T> { private ArrayList<T> set; public MySet() { set = new ArrayList<T>(); } public void clear() { } public boolean add(T e) { } public boolean contains(Object o) { } public boolean isEmpty() { } public boolean remove(Object o) { } public int size() { return set.size(); } public Object[] toArray() { ArrayList<T> temp = (ArrayList<T>) set.clone(); T[] array = (T[]) new Object[set.size()]; int i; for (i=0; i<set.size(); i++) array[i] = (T) temp.get(i); return array; } public static <T> Set<T> union(Set<T> first, Set<T> second ) { } public static <T> Set<T> intersect(Set<T> first, Set<T> second ) { Set<T> set1 = first; Set<T> set2 = second; for (int i = 0; i < set1.size(); i++){ for (int j = 0; j < set2.size(); j++){ if (set1.get(i) == set2.get(i)) } } } public void printAll() { } }public class MySetTests { public static void main(String[] args) { test1(); test2(); test3(); test4(); //..... } //test cosntructor public static void test1() { MySet<Integer> testSet = new MySet<Integer>(); assert(testSet.size() == 0); } //test add on String public static void test2() { MySet<String> testSet = new MySet<String>(); assert(testSet.add("A") == true); assert(testSet.add("A") == false); } //test add and contains public static void test3() { Set<String> testSet = new MySet<String>(); assert(testSet.add("A") == true); assert(testSet.contains("A") == true)); } //test add on Rectangle class public static void test4() { Set<Rectangle> testSet = new MySet<Rectangle>(); assert(testSet.add(new Rectangle(3, 5)) == true); assert(testSet.add(new Rectangle(3, 5)) == false); } }public class Rectangle { private int width; private int length; public Rectangle(int w, int l) { width = w; length = l; } public void setW(int w) { width = w; } public boolean equals(Object o) { if ((o == null) || (! (o instanceof Rectangle))) return false; Rectangle rec = (Rectangle) o; return rec.width == width && rec.length == length; } public String toString() { return "\nwidth: " + width + ", len: "+length; } }