Method union creates a third set that is the set-theoretic


Create the program in C#

Create class IntegerSet. Each IntegerSet object can hold integers in the range 0-100. The set is represented by an array of bools. Array element a[i] is true if integer i is in the set. Array element a[j] is false if integer j is not in the set. The parameterless constructor initializes the array to the "empty set" (i.e., a set whose array representation contains all false values).

Provide the following methods:

a) Method Union creates a third set that is the set-theoretic union of two existing sets (i.e., an element of the third set's array is set to true if that element is true in either or both of the existing sets-otherwise, the element of the third set is set to false).

b) Method Intersection creates a third set which is the set-theoretic intersection of two existing sets (i.e., an element of the third set's array is set to false if that element is false in either or both of the existing sets-otherwise, the element of the third set is set to true).

c) Method InsertElement inserts a new integer k into a set (by setting a[k] to true).

d) Method DeleteElement deletes integer m (by setting a[m] to false).

e) Method ToString returns a string containing a set as a list of numbers separated by spaces. Include only those elements that are present in the set. Use --- to represent an empty set.

f) Method IsEqualTo determines whether two sets are equal.

Test your class IntegerSet by using the main method below:
static void Main(string[] args)
{
// initialize two sets
Console.WriteLine("Input Set A");
IntegerSet set1 = InputSet();
Console.WriteLine("nInput Set B");
IntegerSet set2 = InputSet();
IntegerSet union = set1.Union(set2);
IntegerSet intersection = set1.Intersection(set2);
// prepare output
Console.WriteLine("nSet A contains elements:");
Console.WriteLine(set1.ToString());
Console.WriteLine("nSet B contains elements:");
Console.WriteLine(set2.ToString());
Console.WriteLine(
"nUnion of Set A and Set B contains elements:");
Console.WriteLine(union.ToString());
Console.WriteLine(
"nIntersection of Set A and Set B contains elements:");
Console.WriteLine(intersection.ToString());
// test whether two sets are equal
if (set1.IsEqualTo(set2))
Console.WriteLine("nSet A is equal to set B");
else
Console.WriteLine("nSet A is not equal to set B");
// test insert and delete
Console.WriteLine("nInserting 77 into set A...");
set1.InsertElement(77);
Console.WriteLine("nSet A now contains elements:");
Console.WriteLine(set1.ToString());
Console.WriteLine("nDeleting 77 from set A...");
set1.DeleteElement(77);
Console.WriteLine("nSet A now contains elements:");
Console.WriteLine(set1.ToString());
// test constructor
int[] intArray = { 25, 67, 2, 9, 99, 105, 45, -5, 100, 1 };
IntegerSet set3 = new IntegerSet(intArray);
Console.WriteLine("nNew Set contains elements:");
Console.WriteLine(set3.ToString());
} // end Main
// creates a new set by reading numbers from the user
private static IntegerSet InputSet()
{
IntegerSet temp = new IntegerSet();
Console.Write("Enter number (-1 to end): ");
int number = Convert.ToInt32(Console.ReadLine());
while (number != -1)
{
temp.InsertElement(number);
Console.Write("Enter number (-1 to end): ");
number = Convert.ToInt32(Console.ReadLine());
} // end while
return temp;
}
}
Output
Input Set A
Enter number (-1 to end): 2
Enter number (-1 to end): 7
Enter number (-1 to end): 4
Enter number (-1 to end): 3
Enter number (-1 to end): 1
Enter number (-1 to end): -1
Input Set B
Enter number (-1 to end): 4
Enter number (-1 to end): 1
Enter number (-1 to end): 3
Enter number (-1 to end): 8
Enter number (-1 to end): 9
Enter number (-1 to end): 7
Enter number (-1 to end): -1
Set A contains elements:
{ 1 2 3 4 7 }
Set B contains elements:
{ 1 3 4 7 8 9 }
Union of Set A and Set B contains elements:
{ 1 2 3 4 7 8 9 }
Intersection of Set A and Set B contains elements:
{ 1 3 4 7 }
Set A is not equal to set B
Inserting 77 into set A...
Set A now contains elements:
{ 1 2 3 4 7 77 }
Deleting 77 from set A...
Set A now contains elements:
{ 1 2 3 4 7 }
New Set contains elements:
{ 1 2 9 25 45 67 99 100 }
Press any key to continue . . .

Solution Preview :

Prepared by a verified Expert
DOT NET Programming: Method union creates a third set that is the set-theoretic
Reference No:- TGS01564117

Now Priced at $40 (50% Discount)

Recommended (95%)

Rated (4.7/5)