// // A random set of integers of size n // import java.util.Random; public class RandomSet { private int[] v; private int size; private int capacity; private Random random; public RandomSet(int n){ size = 0; capacity = n; v = new int[n]; random = new Random(); } private int uniform(int N) { return random.nextInt(N); } private void swap(int[] v,int i,int j){int temp = v[i];v[i] = v[j]; v[j] = temp;} public void add(int e) throws RandomSetException { if (size == capacity) throw new RandomSetException("overflow"); v[size] = e; size++; swap(v,uniform(size),size-1); } // // add an element e, assumed to be unique // public int delete() throws RandomSetException { if (size == 0) throw new RandomSetException("underflow"); int i = uniform(size); int e = v[i]; size--; swap(v,i,size); return e; } // // select randomly an element e, delete e from the set, then return e // public int select() throws RandomSetException { if (size == 0) throw new RandomSetException("uderflow"); int i = uniform(size); int e = v[i]; return e; } // // select randomly an element e, then return e // public int size(){return size;} public String toString(){ String s = "{"; if (size > 0) s = s + v[0]; for (int i=1;i