Question: Please help me complete this Java code: Than you! public class Set { private int SIZE = 20; // length of the array private int
Please help me complete this Java code: Than you!
public class Set {
private int SIZE = 20; // length of the array
private int[] S; // array holding the set
private int next; // pointer to next available slot in array
//--------------------------------------------------------------------------------
//public Set() -- Default constructor; Constructs this set as an instance of the empty set
public Set() {
// your code here
}
//--------------------------------------------------------------------------------
/* 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(int[] A) {
// your code here
}
//--------------------------------------------------------------------------------
//public Set clone() -- Return an exact copy of this set (hint: use the previous constructor).
public Set clone() {
// your code here
return this; // just to get it to compile; replace this with something appropriate
}
//--------------------------------------------------------------------------------
/* Given:
This method reallocates the array S to twice as big and copies all the elements over.
It is called only by insert.
*/
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;
}
//--------------------------------------------------------------------------------
/*
* 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 String toString() {
// your code here
return null; // just to get it to compile; replace null with something appropriate
}
//--------------------------------------------------------------------------------
// public int size() -- Return the number of elements in this set.
public int size() {
// your code here
return 0; // just to get it to compile; replace 0 with something appropriate
}
//--------------------------------------------------------------------------------
/*
* public boolean isEmpty() -- Return true if this is the empty set (has no members) and false
otherwise.
*/
public boolean isEmpty() {
// your code here
return false; // just to get it to compile; replace false with something appropriate
}
//--------------------------------------------------------------------------------
//public boolean member(int n) -- Return true if n is in the set and false otherwise.
public boolean member(int k) {
// your code here
return false; // just to get it to compile; replace false with something appropriate
}
//--------------------------------------------------------------------------------
/*
* 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 subset(Set T) {
// your code here
return false; // just to get it to compile; replace false with something appropriate
}
//--------------------------------------------------------------------------------
//public boolean equal(Set T) -- Returns true if this set and T have exactly the same members.
// (Hint: use subset.)
public boolean equal(Set T) {
// your code here
return false; // just to get it to compile; replace false with something appropriate
}
//--------------------------------------------------------------------------------
/*
* 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 insert(int k) {
// your code here
}
//--------------------------------------------------------------------------------
/*
* 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 void delete(int k) {
// your code here
}
//--------------------------------------------------------------------------------
/*
* 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 union(Set T) {
// your code here
return null; // just to get it to compile; replace null with something appropriate
}
//--------------------------------------------------------------------------------
/*
* 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 intersection(Set T) {
// your code here
return null; // just to get it to compile; replace null with something appropriate
}
//--------------------------------------------------------------------------------
/*
* 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.)
*/
public Set setdifference(Set T) {
// your code here
return null; // just to get it to compile; replace null with something appropriate
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
