Question
Java Assignment: Define a Java class whose purpose it is to represent a Set of integers. A set may not contain duplicate numbers. The class
Java Assignment: Define a Java class whose purpose it is to represent a Set of integers. A set may not contain duplicate numbers. The class should be designed around the following characteristics:
Do not use ArrayList, only use Array!!!
1. The name of the class should be Set.
2. The class should be composed of at least the following data members:
setArray, an array of integers. This array will contain the elements that comprise the set.
numElements, data member to represent the number of elements in the set.
DEFAULT_SIZE, (a constant) data member to represent the default value used to allocate memory. Set the default size to five. Note that you may need to add additional data members to accomplish the objective of the driver program.
3. The class should include at least the following methods:
Set() Default Constructor to create the set array with a DEFAULT_SIZE.
Set( int [] arr ) Constructor to initialize the set from the array of integers passed to the constructor. The arguments to this constructor are the array of integers.
Set( Set another) Copy constructor to initialize the object from an existing object.
getLength(). return numElements;
isElement( int n ) This method determines if the specified integer is an element of the set and returns true or false accordingly.
addElement( int n ) A method to add one element to the set. Remember, a set may not contain duplicate numbers. A method to populate the set of integers from user input. If the number of elements exceeds the physical size of the array, the array needs to be extended by an amount equal to DEFAULT_SIZE . You can create a new array, then copy the old array elements to the new array then re-point the old array reference to the new one.
A method to display the set. Note that the set should be displayed using proper set notation ( i.e. { 1, 5, 7 } ).
A method remove( int n ) to remove an element from an existing set if it exists. 4. Methods to perform each of the following set operations:
union( Set another) This method creates a new set that represents the union of the two sets (i.e. the set of the object this methods was invoked on with the set of the object passed to the method). This method returns the new set.
intersection( Set another) This method creates a new set that represents the intersection of the two sets and returns the new set.
difference( Set another) This method creates a new set that represents the difference of the two sets and returns the new set.
equals( Set another) This method determines whether the two sets have the same elements and return true or false accordingly.
Do not use ArrayList, only use Array!!!
A main method is provided to test your code.
public class Set_Demo { public static void main() { // Create a set using an array int [] arr1 ={1,2,3,4,2,4}; Set firstSet = new Set(arr1); firstSet.displaySet(); System.out.println("setArray length = "+firstSet.getLength()); // Add numbers entered by the user eg: 1 5 3 6 8 2 9 0 -1 firstSet.populate(); System.out.println("firstSet size = "+ firstSet.getLength()); firstSet.displaySet(); // Use the copy constructor System.out.println(" Demonstrating copy constructor"); Set myCopySet = new Set(firstSet); myCopySet.displaySet(); System.out.println(" Demonstrating union of two sets"); int [] arr2 ={5,2,6,4,9,4,33}; Set secondSet = new Set(arr2); firstSet.displaySet(); secondSet.displaySet(); Set unionSet = firstSet.union(secondSet); unionSet.displaySet(); System.out.println(" Demonstrating intersection of two sets"); Set intersectSet = firstSet.intersection(secondSet); intersectSet.displaySet(); System.out.println(" Demonstrating firstSet.difference(secondSet)"); Set diffSet = firstSet.difference(secondSet); diffSet.displaySet(); System.out.println(" Demonstrating secondSet.difference(firstSet)"); diffSet = secondSet.difference(firstSet); diffSet.displaySet(); System.out.println(" Demonstrating firstSet.equals(secondSet)"); if(firstSet.equals(secondSet)) System.out.println("They are equal"); else System.out.println("They are NOT equal"); System.out.println(" Demonstrating clone of firstSet.equals(firstSet)"); Set cloneSet = new Set(firstSet); if(cloneSet.equals(firstSet)) System.out.println("They are equal"); else System.out.println("They are NOT equal"); System.out.println(" Checking if 9 and 3 are elements of firstSet"); int n=9; if (firstSet.isElement(n)) System.out.println(n+" is an element"); else System.out.println(n+" is NOT an element"); n=3; if (firstSet.isElement(n)) System.out.println(n+" is an element"); else System.out.println(n+" is NOT an element"); } }
Sample output:
{1,2,3,4}
setArray length = 5
Enter numbers to add separated by spaces. Enter a -ve number to indicate no more :
1 5 3 6 8 2 9 0 -1
Set size extended to 10
firstSet size = 10
{1,2,3,4,5,6,8,9,0}
Demonstrating copy constructor
{1,2,3,4,5,6,8,9,0}
Demonstrating union of two sets Set size extended to 10
{1,2,3,4,5,6,8,9,0}
{5,2,6,4,9,33}
{1,2,3,4,5,6,8,9,0,33}
Demonstrating intersection of two sets
{2,4,5,6,9}
Demonstrating firstSet.difference(secondSet)
{1,3,8,0}
Demonstrating secondSet.difference(firstSet)
{33}
Demonstrating firstSet.equals(secondSet)
They are NOT equal Demonstrating clone of firstSet.equals(firstSet)
They are equal Checking if 9 and 3 are elements of firstSet
9 is an element
3 is an element
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started