Question: Write a class called Set that models a mathematical set. Note that a set does not contain duplicate values and is not sorted. For example,

Write a class called Set that models a mathematical set. Note that a set does not contain duplicate values and is not sorted. For example, we cannot have the following set {8,9,8}. Furthermore, the set {7,8,1} is equivalent to the set {8, 1, 7}. The Set class will contain the following features

Private Instance Variables

int[] set - stores the elements of the set

int size contains the number of elements in the set.

Note that 0 <= size <= set.length Public Constructor Set(int capacity) allocates memory for the set array. The array length = capacity. Private Method void increaseCapacity this methods doubles the length of the set array without losing any of the values stored in the array. Example: If set = {8, 2, 1} before calling increaseCapacity then set = {8, 2, 1, 0, 0, 0} after calling increaseCapacity. Public Methods void add(int value) if value is not contained in the set then perform assign value to set[size] and then increment size by 1. If size = set.length then call the increaseCapacity method to increase the size of the set array before adding any values to the set. boolean contains(int value) returns true if the value is contained in the set; otherwise return false. Use a while loop to determine if a value is in array. Stop iteration if you determine the value is in the array. Do not use a break statement. int size() return the value of size void clear() allocates a new array of length 10 to set; sets size to 0

void clear() allocates a new array of length 10 to set; sets size to 0 boolean remove(int value) if value is not in the set then return false; if value is contained in the set then remove this value from the set with the following algorithm: if the value is stored at index k then shift the values stored in indices k + 1 to size 1 to the left and decrease size by 1. Make sure to use System.arraycopy to shift array elements to the left. String toString() returns a String representation of the set. Example: suppose the array contains the value 4, 8, and 9. The toString method returns the String {4, 8, 9}. If the set is empty return the String {}.

Below is the test class that will use to run the code;

public class Test {

public static void main(String[] args) {

boolean flag;

Set s = new Set(8);

s.add(4);

System.out.println("Size of the set: " + s.size());

s.add(100);

System.out.println("Size of the set: " + s.size());

s.add(23);

System.out.println("Size of the set: " + s.size());

System.out.println("Contents of the set: " + s);

flag = s.remove(2);

if (flag) {

System.out.println("An element was removed from the set");

} else {

System.out.println("An element was not removed from the set");

}

System.out.println();

s.clear();

System.out.println("Contents of the set: " + s);

System.out.println();

s.add(50);

s.add(24);

s.add(101);

s.add(45);

System.out.println("Size of the set: " + s.size());

System.out.println("Contents of the set: " + s);

System.out.println();

flag = s.remove(50);

if (flag) {

System.out.println("An element was removed from the set");

} else {

System.out.println("An element was not removed from the set");

}

System.out.println("Size of the set: " + s.size());

System.out.println("Contents of the set: " + s);

System.out.println();

s.clear();

for (int i = 0; i < 20; i++) {

s.add(i);

}

System.out.println("Size of the set: " + s.size());

System.out.println("Contents of the set: " + s);

System.out.println();

s.add(10);

System.out.println("Size of the set: " + s.size());

System.out.println("Contents of the set: " + s);

}

}

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!