Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Data Fields: set: Your class shall have a data field called set. This data field

ID: 3745434 • Letter: D

Question

Data Fields:
set: Your class shall have a data field called set. This data field shall be an array of integer values. This data field shall also be private with no getters or setters.
Methods:
existsInSet: This method shall be a public method which accepts one integer as an argument. This method shall return true or false depending on whether or not the given value already exists within your set. You will want to implement this method first since it will help you to implement some of the methods to follow.
HINT: You will need to account for when the set may be empty.
addToSet: This method shall be a public method which accepts one integer as an argument. The integer shall be added to the set, provided that the value does not already exist within the set. If the value already exists, display an error message.
HINT 1: Use the existsInSet method that you wrote previously to help implement this method.
HINT 2: Since we cannot change the size of an existing array, you will need to create a new array with the current size + 1, and then copy the existing values to the new array and add the new value to the end. Be sure to update your set data field to refer to the new array and not the old one.
removeFromSet: This method shall be a public method which accepts one integer as an argument. This integer shall be a value that will be removed from the set. (Note: This is not the index to remove, but the actual value you want to remove.)
HINT 1: Make sure the value to be removed actually exists. If it does not, display an error message to the user and do not remove anything. Use the existsInSet method that you wrote previously to help implement this method.
HINT 2: Since we cannot change the size of an existing array, you will need to create a new array with the current size - 1, and then copy the existing values to the new array, but exclude the value to be removed. Be sure to update your set data field to refer to the new array and not the old one.

Explanation / Answer


You have not mentioned programming language. So given the code in java
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i

public class Set {
private int[] set;
public Set(){
set = null;
}
public boolean existsInSet(int num){
if(set != null){
for(int i = 0; i < set.length; i++)
if(set[i] == num)
return true;
}
return false;
}
public void addToSet(int num){
if(!existsInSet(num)){
int[] temp;
if(set == null)
temp = new int[1];
else
temp = new int[set.length + 1];
//copy existing data
int i;
for(i = 0; i < temp.length - 1; i++)
temp[i] = set[i];
temp[i] = num;
set = temp; //update field to refer to new data array
}
else
System.out.println("Duplicate number " + num +", did not add to set");
}
public void removeFromSet(int num){
if(existsInSet(num)){
int[] temp = new int[set.length - 1];
//copy existing data
for(int i = 0, j = 0; j < set.length;j++){
if(set[j] != num) //skip the data to be removed
temp[i++] = set[j];
}
set = temp; //update field to refer to new data array
}
else
System.out.println("Number " + num +", does not exist in set");
}
}