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

PracticeSet Class: Name class PracSet Data Fields: set: Your class shall have a

ID: 3753837 • Letter: P

Question

PracticeSet

Class:


Name class PracSet


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.


Constructors:


Your class shall have a default constructor which creates an empty set.  This just means that your set data field will be an empty array.  (You can do this by creating an array of size 0).


Your class shall have an additional constructor which will accept a comma separated list of integers as input to the constructor.  Also note that this constructor can accept an array of integers as well. The following examples show how this constructor would be invoked:


PracticeSet set1 = new PracticeSet(23, 1, 2, 4, 78, 9);


PracticeSet set2 = new PracticeSet(myArray); //myArray is an existing array of integers.


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.

 


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: use

existsInSet method

for help

PracticeSetUtils Class:


Name class PracticeSetUtils


Only contain static methods.


Data Fields:


No data fields.


Constructors:


This class shall have a no-arg (default) constructor which should have private visibility.  This is to prevent the class from being instantiated.


Methods:


union: This method shall take two PracticeSet class instances as arguments, and returns a PracticeSet object which is the union of the two sets.  This method shall be public and static.


Example {1, 2, 3, 4, 5} and {1, 3, 5, 6, 10} is {1, 2, 3, 4, 5, 6, 10}.


Hint: use addToSet method for help

intersection: This method shall take two PracticeSet class instances as arguments, and returns a PracticeSet object which is the intersection of the two sets. This method shall be public and static.


Example {1, 2, 3, 4, 5} and {1, 3, 5, 6, 10} is {1, 3, 5}.


If there is no intersection, then the result is the empty set.


Hint: use existsInSet method for

help

Main Class:


Test both classes' requirements

In JAVA, please and thank you!

Explanation / Answer

package Array;

// Class PracSet definition

class PracSet

{

// To store numbers

private int set[];

// Points to current index

int currentIndex;

// Default constructor to initialize instance variables

PracSet()

{

set = new int[0];

currentIndex = 0;

}// End of default constructor

// Parameterized constructor to assign parameter value to instance variable

// ... a is variable length argument

PracSet(int ...a)

{

// Dynamically allocates memory of size parameter length

set = new int[a.length];

// Loops till length of the parameter

for(int x = 0; x < a.length; x++)

// Calls the method to add the data

addToSet(a[x]);

}// End of parameterized constructor

// Method to return the the value at given parameter index position

int getIndexValue(int index)

{

return set[index];

}// End of method

// Method to set the parameter number at current index position

void setIndexValue(int no)

{

// Assigns the number at current index position

// and increase the counter by one

set[currentIndex++] = no;

}// End of method

// Method to return current length

int getLength()

{

return currentIndex;

}// End of method

// Method to set the length

void setLength(int no)

{

set = new int[no];

}// End of method

// Method to return true id number is available in set

// Otherwise return false

boolean existsInSet(int no)

{

// Loops till current length of the set

for(int x = 0; x < currentIndex; x++)

// Checks if current index position of the set is equals to the parameter number

// then return true

if(set[x] == no)

return true;

// Otherwise after the finish of the loop return false

return false;

}// End of method

// Method to add a number at current index position if it is not available in the set

void addToSet(int no)

{

// Calls the method to check if the number is available in the set then display error message  

if(existsInSet(no))

System.out.println("The number - " + no + " already exits.");

// Otherwise add the parameter number at current index position of set

else

set[currentIndex++] = no;

}// End of method

// Overrides toString() method to return string format of the set

public String toString()

{

String result = "";

for(int x = 0; x < currentIndex; x++)

result += set[x] + ", ";

return result;

}// End of method

}// End of class

// Class PracticeSetUtils definition

public class PracticeSetUtils

{

// Private constructor

private PracticeSetUtils(){}

// Static method to return the union of two PracSet objects

public static PracSet union(PracSet p1, PracSet p2)

{

// Creates a temporary object of class PracSet

PracSet temp = new PracSet();

// Set the length of the temporary object by adding the length

// of both the parameter object

temp.setLength(p1.getLength() + p2.getLength());

// Loops till end of the first set length

for(int x = 0; x < p1.currentIndex; x++)

// Calls the method to add the first set value to the temporary object set

// Without duplicate

temp.addToSet(p1.getIndexValue(x));

// Loops till end of the second set length

for(int x = 0; x < p2.currentIndex; x++)

// Calls the method to add the second set value to the temporary object set

// Without duplicate

temp.addToSet(p2.getIndexValue(x));

// Returns the union object

return temp;

}// End of method

// Static method to return the intersection of two PracSet objects

public static PracSet intersection(PracSet p1, PracSet p2)

{

// Creates a temporary object of class PracSet

PracSet temp = new PracSet();

// Set the length of the temporary object by adding the length

// of both the parameter object

temp.setLength(p1.getLength() + p2.getLength());

// Loops till end of the first set length

for(int x = 0; x < p1.currentIndex; x++)

// Calls the method to check if the first set value at x position

// is available in second set

if(p2.existsInSet(p1.getIndexValue(x)))

// If true then add the data

temp.setIndexValue(p1.getIndexValue(x));

// Returns the intersection object

return temp;

}// End of method

// main method definition

public static void main(String[] args)

{

// Creates an integer array

int arr[] = {1, 3, 5, 6, 10};

// Creates an object of the class PracSet with variable length argument parameterized constructor  

PracSet ps1 = new PracSet(1, 2, 3, 4, 5);

// Creates an object of the class PracSet with variable length argument parameterized constructor

PracSet ps2 = new PracSet(arr);

// Displays both the sets

System.out.println("Set - 1: " + ps1);

System.out.println("Set - 2: " + ps2);

// Calls the method to make union of ps1 and ps2 and displays he union result

System.out.println(" Union of Set - 1 and Set - 2 " + union(ps1, ps2));

// Calls the method to make Intersection of ps1 and ps2 and displays he union result

System.out.println(" Intersection of Set - 1 and Set - 2 " + intersection(ps1, ps2));

}// End of main method

}// End of class

Sample Output:

Set - 1: 1, 2, 3, 4, 5,

Set - 2: 1, 3, 5, 6, 10,

The number - 1 already exits.

The number - 3 already exits.

The number - 5 already exits.

Union of Set - 1 and Set - 2

1, 2, 3, 4, 5, 6, 10,

Intersection of Set - 1 and Set - 2

1, 3, 5,

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote