Question: Complete the following code: public class Set { private int SIZE = 10; // initial length of the array private int[] S; // array holding

Complete the following code:  public class Set { private int SIZE = 10; // initial length of the array private int[] S; // array holding the set private int next; // pointer to next available slot in array /* * DO NOT MODIFY THE Set() * constructor. We have completed this for you. */ public Set() { S = new int[SIZE]; next = 0; } /* * TODO: * return an exact copy of this set. * Construct this set consisting of exactly the elements of A (which, you may assume, does not have duplicates); A can *be of arbitrary length (it may not be smaller than SIZE). (Hint: create an empty set and use insert(...) to add the *elements, which may trigger a resize of the array.) public Set(int[] A) { } /* * TODO: * return an exact copy of this set * Read the assignment handout for more details. * use previous instructor */ public Set clone() { } // /* * DO NOT MODIFY THE resize method. * replace array A with array twice as big, and copy everything over */ private void resize() { int[] T = new int[SIZE * 2]; for (int i = 0; i < S.length; ++i) { T[i] = S[i]; } SIZE = SIZE * 2; S = T; } /* * DO NOT MODIFY THE toString method. * We have completed this method for you. */ public String toString() { if (next == 0) { return "[]"; } String s = "["; for (int i = 0; i < next-1; ++i) { s += S[i] + ","; } s += S[next-1] + "]"; return s; } /* * TODO: * Return the number of elements in the set */ public int size() { } /* * TODO: * Return true if S is the empty set (has no members) * and false otherwise */ public boolean isEmpty() { } /* * TODO: * Return true if n is in the set and false otherwise */ public boolean member(int k) { } /* * TODO: * Returns true if S is a subset of T, that is, every member of S is a member of T. * hint (use member method) */ public boolean subset(Set T) { } /* * TODO: * The equal method must call the subset method i.e., * two sets are equal if both are subset of each other. */ public boolean equal(Set T) { } /* * TODO: * If the integer k is a member of the set already, do nothing, otherwise, add to the set; if this would cause an *ArrayOutOfBoundsException, call resize() (provided in template code) to increase size of array before *doing insertion. */ public void insert(int k) { } /* * TODO: * If the integer k is not a member, do nothing; else remove it from the set; you must shift all elements * which occur after k in the array to the left by one slot. */ public void delete(int k) { } /* * TODO: *Return a new set consisting of all of the elements of this set and T combined (without duplicates). * (Hint: make a clone of this set, and insert all elements of T into the clone.) */ public Set union(Set T) { } /* * TODO: * Return the intersection of this set and T. (Hint: make a new empty set, and for every element of * this set, if it also occurs in T, insert it into the new set.) */ public Set intersection(Set T) { } /* TODO: *Return the setdifference of this set and T. (Hint: make a new empty set, and for every * element of this set, if it does NOT occur in T, insert it into the new set.) */ public Set setdifference(Set T) { } /* * Do not write any code inside the main method and expect it to get marked. * Any code that you write inside the main method will be ignored. However, * you are free to edit the main function in any way you like for * your own testing purposes. */ public static void main(String [] args) { System.out.println(" Unit Test for Set: note that your answers, when they are"); System.out.println(" sets, could be in a different order (since order does"); System.out.println(" not matter), this is the meaning of \"same set as...\" "); Set A = new Set(); Set B = new Set( new int[] { 5 } ); Set C = new Set( new int[] { 5, 3, 7, 4, 1 } ); Set D = new Set( new int[] { 4, 3, -3, 10, 8 } ); Set E = new Set( new int[] { 8, 4, 10 } ); Set F = new Set( new int[] { 10, 8, 4 } ); System.out.println("Test 01: Should be []"); System.out.println(A); System.out.println(); System.out.println("Test 02: Should be [5]"); System.out.println(B); System.out.println(); System.out.println("Test 03: Should be same set as [5,3,7,4,1]"); System.out.println(C); System.out.println(); System.out.println("Test 04: Should be []"); System.out.println(A.clone()); System.out.println(); System.out.println("Test 05: Should be same set as [5,3,7,4,1]"); System.out.println(C.clone()); System.out.println(); System.out.println("Test 06: Should be 0"); System.out.println(A.size()); System.out.println(); System.out.println("Test 07: Should be 5"); System.out.println(D.size()); System.out.println(); System.out.println("Test 08: Should be true"); System.out.println(A.isEmpty()); System.out.println(); System.out.println("Test 09: Should be false"); System.out.println(F.isEmpty()); System.out.println(); System.out.println("Test 10: Should be false"); System.out.println(A.member(4)); System.out.println(); System.out.println("Test 11: Should be true"); System.out.println(C.member(1)); System.out.println(); System.out.println("Test 12: Should be false"); System.out.println(D.member(1)); System.out.println(); System.out.println("Test 13: Should be true"); System.out.println(A.subset(D)); System.out.println(); System.out.println("Test 14: Should be false"); System.out.println(D.subset(C)); System.out.println(); System.out.println("Test 15: Should be true"); System.out.println(E.subset(D)); System.out.println(); System.out.println("Test 16: Should be false"); System.out.println(D.subset(E)); System.out.println(); System.out.println("Test 17: Should be false"); System.out.println(D.equal(E)); System.out.println(); System.out.println("Test 18: Should be true"); System.out.println(E.equal(F)); System.out.println(); System.out.println("Test 19: Should be true"); System.out.println(F.equal(E)); System.out.println(); System.out.println("Test 20: Should be true"); System.out.println(A.equal(A)); System.out.println(); System.out.println("Test 21: Should be same set as [4,6]"); Set A1 = A.clone(); A1.insert(4); A1.insert(6); A1.insert(4); System.out.println(A1); System.out.println(); System.out.println("Test 22: Should be same set as [10,8,4,5]"); Set F1 = F.clone(); F1.insert(5); F1.insert(4); System.out.println(F1); System.out.println(); System.out.println("Test 23: Should be same set as [8,4,10]"); Set E1 = E.clone(); E1.insert(10); System.out.println(E1); System.out.println(); System.out.println("Test 24: Should be []"); A1 = A.clone(); A1.delete(5); System.out.println(A1); System.out.println(); System.out.println("Test 25: Should be []"); Set B1 = B.clone(); B1.delete(5); System.out.println(B1); System.out.println(); System.out.println("Test 26: Should be same set as [8,4,10]"); E1 = E.clone(); E1.delete(5); System.out.println(E1); System.out.println(); System.out.println("Test 27: Should be same set as [4,10]"); E1 = E.clone(); E1.delete(8); System.out.println(E1); System.out.println(); System.out.println("Test 28: Should be same set as [3,4]"); System.out.println(C.intersection(D)); System.out.println(); System.out.println("Test 29: Should be [8,4,10]"); System.out.println(E.intersection(F)); System.out.println(); System.out.println("Test 30: Should be same set as []"); System.out.println(A.intersection(F)); System.out.println(); System.out.println("Test 31: Should be same set as []"); System.out.println(B.intersection(F)); System.out.println(); System.out.println("Test 32: Should be same set as [4,3,-3,10,8,5,7,1]"); System.out.println(C.union(D)); System.out.println(); System.out.println("Test 33: Should be same set as [10,8,4]"); System.out.println(E.union(F)); System.out.println(); System.out.println("Test 34: Should be same set as [10,8,4]"); System.out.println(A.union(F)); System.out.println(); System.out.println("Test 35: Should be same set as [5,3,7,4,1]"); System.out.println(C.union(B)); System.out.println(); System.out.println("Test 36: Should be same set as [5,7,1]"); System.out.println(C.setdifference(D)); System.out.println(); System.out.println("Test 37: Should be same set as []"); System.out.println(E.setdifference(F)); System.out.println(); System.out.println("Test 38: Should be same set as [5,3,7,4,1]"); System.out.println(C.setdifference(A)); System.out.println(); System.out.println("Test 39: Should be same set as []"); System.out.println(C.setdifference(C)); System.out.println(); System.out.println("Test 40: Should be same set as [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]"); Set G = new Set(); for(int i = 0; i < 32; ++i) { G.insert(i); } System.out.println(G); System.out.println(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!