Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code is below the explanation (Set.java) The idea is that you can represent a set of up to SIZE integers, and they are stored, without

Code is below the explanation (Set.java) The idea is that you can represent a set of up to SIZE integers, and they are stored, without duplicates, in the indices from 0 to next-1. The values from next to S.length-1 do not matterthey do not need to be 0s. When manipulating the array using a for loop, you should use next as the for loop bound, NOT S.length public Set() -- Default constructor; constructs this set as an instance of the empty set. public Set(int[] A) -- 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 clone() -- Return an exact copy of this set (hint: use the previous constructor). public String toString() -- Return a string representation of this set, e.g., [2,3,7,-3] or []; you may not use Array.toString(...) to do this; you may create a char array from the array S and use the String constructor (which will accept a char array as initializer), or you may build up the String one character at a time using concatenation. Note that there is no space after the comma or anywhere in the toString version. Your output must not deviate from the above syntactical representation. public int size() -- Return the number of elements in this set. public boolean isEmpty() -- Return true if this is the empty set (has no members) and false otherwise. public boolean member(int n) -- Return true if n is in the set and false otherwise. public boolean subset(Set T) -- Returns true if this set is a subset of T, that is, every member of this set is a member of T, and false otherwise. (Hint: use member.) public boolean equal(Set T) -- Returns true if this set and T have exactly the same members. (Hint: use subset.) public void insert(int k) -- 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 delete(int k) -- 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 Set union(Set T) -- 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 intersection(Set T) -- 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 setdifference(Set T) -- 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.) Important Note: The last three methods should make new sets, and NOT modify their input sets in any way! Insert and delete will modify the set. Look for opportunities (suggested in the hints) to reuse your own code by implementing methods by using other, simpler methods. For example, equals can be implemented simply (one line of code) using two calls to subset, and setdifference is VERY similar to intersection! 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. * Read the assignment handout for more details. */ public Set(int[] A) { } /* * TODO: * return an exact copy of this set * Read the assignment handout for more details. */ 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 * Read the assignment handout for more details. */ public int size() { } /* * TODO: * Return true if S is the empty set (has no members) * Read the assignment handout for more details. */ public boolean isEmpty() { } /* * TODO: * Return true if n is in the set and false otherwise * Read the assignment handout for more details. */ 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. * Read the assignment handout for more details. */ 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: * Read the assignment handout for more details. */ public void insert(int k) { } /* * TODO: * Read the assignment handout for more details. */ public void delete(int k) { } /* * TODO: * Returns the union of this set and T as a new set * Read the assignment handout for more details. */ public Set union(Set T) { } /* * TODO: * Returns the intersection of S and T as a new set * Read the assignment handout for more details. */ public Set intersection(Set T) { } /* TODO: * Returns the set difference this / T, i.e., all elements of this set which * are not in T, as a new set. * Read the assignment handout for more details. */ 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions

Question

What is management growth? What are its factors

Answered: 1 week ago

Question

What are the organizations task goals on this issue?

Answered: 1 week ago