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

use C# please (visual studio 2017) Here is sample code using System; using Syste

ID: 3743014 • Letter: U

Question

use C# please (visual studio 2017)

Here is sample code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp4
{
class SetOperations
{
//p)   print method to print the elements of the set.
public static void printElementsofSet(Set inputSet)
{
for(int i =0;i {
Console.WriteLine(inputSet.data[i]);
}
}

//l) addElement method that adds element to the set. Make sure that you donít add an existing element. You will use isElement check before you try to add it.
public static Set addElement(Set inputSet, int number)
{
// Checking if the element is in the Set
bool result = false;
for (int i = 0; i < inputSet.size; i++)
{
if (inputSet.data[i] == number)
result = true;
}

if(result==false) // means we need to creat another Set which is size+1
{
Set resultSet = new Set(inputSet.size + 1);
for (int i = 0; i < inputSet.size; i++)
{
resultSet.data[i] = inputSet.data[i];
}
resultSet.data[inputSet.size] = number;
return resultSet;
}
else // means we have already had this element in the Set
return inputSet;
}

// b) union: find the union of the set with a given set. OUTPUT: a Set object.
public static Set Union(Set inputSet1, Set inputSet2)
{

Set result = new Set(0);

for (int i = 0; i < inputSet1.size; i++)
{
if (!isElement(result, inputSet1.data[i]))
{
result = addElement(result, inputSet1.data[i]);
}
}
for (int i = 0; i < inputSet2.size; i++)
{
if (!isElement(result, inputSet2.data[i]))
{
result = addElement(result, inputSet2.data[i]);
}
}
return result;
}

// i) isElement method that takes an element and check if it is an element of the set.
public static bool isElement(Set inputSet, int number)
{
bool result = false;
for (int i = 0; i < inputSet.size; i++)
{
if (inputSet.data[i] == number)
result = true;
}
return result;
}
}
class Set
{
public int[] data;
public int size = 0;

//a) Basic constructor to initialize the set with size only
public Set(int numofelements)
{
data = new int[numofelements];
size = numofelements;
}

//a) A constructors to initialize the set (one is to set the elements from a one dimension array)
public Set(int[] elements)
{
data = new int[elements.Length];
for (int i = 0; i < elements.Length; i++)
data[i] = elements[i];
size = elements.Length;
}

}
class Program
{
static void Main(string[] args)
{
Set aSet = new Set(new int[]{ 11,3,7,9,12});
Set bSet = new Set(new int[] { 1, 8, 12,4,4 });

Set result = SetOperations.Union(aSet, bSet);
SetOperations.printElementsofSet(result);
}
}
}

Write a computer program that defines the class "Set" (assume that the elements are integers) The class should contain methods to perform the operations listed below. Write a test program that tests your "Set" class. The test program will be a menu-like to test all the operations in class "Set" Use a linked list, double linked list, Arraylist of hashset or anything like these 1. a) Different constructors to initialize the set (one is to set the elements from a one dimension array) 2. b) union: find the union of the set with a given set. OUTPUT: a Set object. 3. c) intersection: find the intersection of the set with a given set. OUTPUT: a Set object. 4. d) difference: find the difference of the set and the given set (Set - given Set). OUTPUT: a Set object. 5. e) Cartesian product: find the Cartesian product (Set X given set). OUTPUT: prints out the product. 6. f) Check whether the set is a subset of another set. OUTPUT: True or False. g) Find the powerset of the set. OUTPUT: prints out the powerset. 8. h) isEmpty method that returns true if the set is empty (no elements). 9. i) isElement method that takes an element and check if it is an element of the set. 10. j) isEqual method that check if the set is equal to a given set. 11. k) getCardinality method that returns the cardinality of the set. 12. 1) addElement method that adds element to the set. Make sure that you don't add an existing element. You will use isElement check before you try to add it 13. m) removeElementmethodtoremoveanelementfromtheset 14. n) Clear method to remove all elements of the set. 15. o) toArray that converts the object of the Set class to a one dimensional array. 6. p) print method to print the elements of the set. In your test program, declare two sets objects and use the constructor to initialize them by sending a one dimension array contains the elements of the set.

Explanation / Answer

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ConsoleApp4

{

class SetOperations

{

//p) print method to print the elements of the set.

public static void printElementsofSet(Set inputSet)

{

for(int i =0;i < inputSet.size; i++)

{

Console.Write(inputSet.data[i] + " ");

}

}

//l) addElement method that adds element to the set. Make sure that you donít add an existing element. You will use isElement check before you try to add it.

public static Set addElement(Set inputSet, int number)

{

// Checking if the element is in the Set

bool result = false;

for (int i = 0; i < inputSet.size; i++)

{

if (inputSet.data[i] == number)

result = true;

}

if(result==false) // means we need to creat another Set which is size+1

{

Set resultSet = new Set(inputSet.size + 1);

for (int i = 0; i < inputSet.size; i++)

{

resultSet.data[i] = inputSet.data[i];

}

resultSet.data[inputSet.size] = number;

return resultSet;

}

else // means we have already had this element in the Set

return inputSet;

}

//m) removeElement method that removes element from the set.

public static Set removeElement(Set inputSet, int number)

{

// Checking if the element is in the Set

bool result = false;

for (int i = 0; i < inputSet.size; i++)

{

if (inputSet.data[i] == number)

result = true;

}

if(result==true)

{

Set resultSet = new Set(0);

for (int i = 0; i < inputSet.size; i++)

{

if(inputSet.data[i] != number)

{

resultSet = addElement(resultSet, inputSet.data[i]);

}

}

return resultSet;

}

else // means we don't have this element in the Set

return inputSet;

}

//n) removeAllElements method that removes all element of the set.

public static Set removeAllElements(Set inputSet)

{

inputSet.data = new int[] {};

inputSet.size = 0;

return inputSet;

}

// b) union: find the union of the set with a given set. OUTPUT: a Set object.

public static Set Union(Set inputSet1, Set inputSet2)

{

Set result = new Set(0);

for (int i = 0; i < inputSet1.size; i++)

{

if (!isElement(result, inputSet1.data[i]))

{

result = addElement(result, inputSet1.data[i]);

}

}

for (int i = 0; i < inputSet2.size; i++)

{

if (!isElement(result, inputSet2.data[i]))

{

result = addElement(result, inputSet2.data[i]);

}

}

return result;

}

// c) union: find the intersection of the set with a given set. OUTPUT: a Set object.

public static Set Intersection(Set inputSet1, Set inputSet2)

{

Set result = new Set(0);

for (int i = 0; i < inputSet1.size; i++)

{

if (!isElement(result, inputSet1.data[i]) && isElement(inputSet2, inputSet1.data[i]))

{

result = addElement(result, inputSet1.data[i]);

}

}

return result;

}

// d) Difference: find the Difference of the set with a given set. OUTPUT: a Set object.

public static Set Difference(Set inputSet1, Set inputSet2)

{

Set result = new Set(0);

for (int i = 0; i < inputSet1.size; i++)

{

if (!isElement(result, inputSet1.data[i]) && !isElement(inputSet2, inputSet1.data[i]))

{

result = addElement(result, inputSet1.data[i]);

}

}

return result;

}

// e) Product: find the cartesian Product of the set with a given set. OUTPUT: Prints the cartesian product.

public static void Product(Set inputSet1, Set inputSet2)

{

for (int i = 0; i < inputSet1.size; i++)

{

for (int j = 0; j < inputSet2.size; j++)

{

Console.Write("{ " + inputSet1.data[i] + ", " + inputSet2.data[j] + " }");

}

Console.WriteLine();

}

}

// g) PowerSet: find the PowerSet of the set. OUTPUT: Prints the PowerSet.

public static void PowerSet(Set inputSet)

{

int set_size = (int)Math.Pow(2, inputSet.size);

for(int i = 0; i < set_size; i++)

{

Console.Write('{');

for(int j = 0; j < inputSet.size; j++)

{

  

if((i & (1 << j)) > 0)

{

Console.Write(inputSet.data[j] + " ");

}

}

Console.WriteLine('}');

}

}

// f) isSubset: find whether a set is subset of a given set. OUTPUT: Boolean

public static bool isSubset(Set inputSet1, Set inputSet2)

{

bool is_subset = true;

for (int i = 0; i < inputSet1.size; i++)

{

if (!isElement(inputSet2, inputSet1.data[i]))

{

is_subset = false;

}

}

return is_subset;

}

// h) isEmpty: find whether a set is empty or not. OUTPUT: Boolean

public static bool isEmpty(Set inputSet1)

{

if(inputSet1.size == 0){

return true;

}

return false;

}

// j) isEqual: find whether a set is equal to another set or not. OUTPUT: Boolean

public static bool isEqual(Set inputSet1, Set inputSet2)

{

if(inputSet1.size != inputSet2.size)

{

return false;

}

for (int i = 0; i < inputSet1.size; i++)

{

if (!isElement(inputSet2, inputSet1.data[i]))

return false;

}

return true;

}

// k) getCardinality: find cardinality of a set. OUTPUT: Int

public static int getCardinality(Set inputSet1)

{

return inputSet1.size;

}

// i) isElement method that takes an element and check if it is an element of the set.

public static bool isElement(Set inputSet, int number)

{

bool result = false;

for (int i = 0; i < inputSet.size; i++)

{

if (inputSet.data[i] == number)

result = true;

}

return result;

}

// o) toArray method that takes an element and check if it is an element of the set.

public static int[] toArray(Set inputSet)

{

int[] result = new int[inputSet.size];

for (int i = 0; i < inputSet.size; i++)

{

result[i] = inputSet.data[i];

}

return result;

}

}

class Set

{

public int[] data;

public int size = 0;

//a) Basic constructor to initialize the set with size only

public Set(int numofelements)

{

data = new int[numofelements];

size = numofelements;

}

//a) A constructors to initialize the set (one is to set the elements from a one dimension array)

public Set(int[] elements)

{

data = new int[elements.Length];

for (int i = 0; i < elements.Length; i++)

data[i] = elements[i];

size = elements.Length;

}

}

class Program

{

static void Main(string[] args)

{

Set aSet = new Set(new int[]{ 11,3,7,9,12});

Set bSet = new Set(new int[] { 1, 8,4,4, 12});

Set cSet = new Set(new int[] {});

Console.WriteLine("Union:");

Set result = SetOperations.Union(aSet, bSet);

SetOperations.printElementsofSet(result);

Console.WriteLine(" Intersection:");

Set result1 = SetOperations.Intersection(aSet, bSet);

SetOperations.printElementsofSet(result1);

Console.WriteLine(" Difference:");

Set result2 = SetOperations.Difference(aSet, bSet);

SetOperations.printElementsofSet(result2);

Console.WriteLine(" Product:");

SetOperations.Product(aSet, bSet);

Console.WriteLine(" is Subset:");

bool result3 = SetOperations.isSubset(aSet, bSet);

Console.WriteLine(result3);

Console.WriteLine(" PowerSet:");

SetOperations.PowerSet(aSet);

Console.WriteLine(" is Empty:");

bool result4 = SetOperations.isEmpty(cSet);

Console.WriteLine(result4);

Console.WriteLine(" is Equal:");

bool result5 = SetOperations.isEqual(cSet, bSet);

Console.WriteLine(result5);

Console.WriteLine(" Cardinality:");

int result6 = SetOperations.getCardinality(aSet);

Console.WriteLine(result6);

Console.WriteLine(" RemoveElement:");

Set result7 = SetOperations.removeElement(aSet,11);

SetOperations.printElementsofSet(result7);

Console.WriteLine(" RemoveAllElement:");

Set result8 = SetOperations.removeAllElements(aSet);

SetOperations.printElementsofSet(result8);

Console.WriteLine(" ToArray:");

int[] result9 = SetOperations.toArray(bSet);

for(int i=0; i<result9.Length; i++)

{

Console.Write(result9[i] + " ");

}

}

}

}

OUTPUT

Union:
11 3 7 9 12 1 8 4
Intersection:
12
Difference:
11 3 7 9
Product:
{ 11, 1 }{ 11, 8 }{ 11, 4 }{ 11, 4 }{ 11, 12 }
{ 3, 1 }{ 3, 8 }{ 3, 4 }{ 3, 4 }{ 3, 12 }
{ 7, 1 }{ 7, 8 }{ 7, 4 }{ 7, 4 }{ 7, 12 }
{ 9, 1 }{ 9, 8 }{ 9, 4 }{ 9, 4 }{ 9, 12 }
{ 12, 1 }{ 12, 8 }{ 12, 4 }{ 12, 4 }{ 12, 12 }

is Subset:
False

PowerSet:
{}
{11 }
{3 }
{11 3 }
{7 }
{11 7 }
{3 7 }
{11 3 7 }
{9 }
{11 9 }
{3 9 }
{11 3 9 }
{7 9 }
{11 7 9 }
{3 7 9 }
{11 3 7 9 }
{12 }
{11 12 }
{3 12 }
{11 3 12 }
{7 12 }
{11 7 12 }
{3 7 12 }
{11 3 7 12 }
{9 12 }
{11 9 12 }
{3 9 12 }
{11 3 9 12 }
{7 9 12 }
{11 7 9 12 }
{3 7 9 12 }
{11 3 7 9 12 }

is Empty:
True

is Equal:
False

Cardinality:
5

RemoveElement:
3 7 9 12
RemoveAllElement:

ToArray:
1 8 4 4 12