**** USING C# **** **** USING C# **** **** USING C# **** **** USING C# **** ****
ID: 3743245 • Letter: #
Question
**** USING C# **** **** USING C# **** **** USING C# ****
**** USING C# **** **** USING C# **** **** USING C# ****
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”
a) Different constructors to initialize the set (one is to set the elements from a one dimension array) b) union: find the union of the set with a given set. OUTPUT: a Set object. c) intersection: find the intersection of the set with a given set. OUTPUT: a Set object. d) difference: find the difference of the set and the given set (Set – given Set). OUTPUT: a Set object. e) Cartesian product: find the Cartesian product (Set X given set). OUTPUT: prints out the product. 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. h) isEmpty method that returns true if the set is empty (no elements). i) isElement method that takes an element and check if it is an element of the set. j) isEqual method that check if the set is equal to a given set. k) getCardinality method that returns the cardinality of the set. 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. m) removeElement method to remove an element from the set. n) Clear method to remove all elements of the set. o) toArray that converts the object of the Set class to a one dimensional array. 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;
using System.Collections.Generic;
using System.Text;
namespace RegularExpression
{
public class Set : CollectionBase
{
public object this[int index]
{
get
{
return ((object)List[index]);
}
set
{
List[index] = value;
}
}
public int AddElement(object element)
{
if (Contains(element) == false)
{
return List.Add(element);
}
return -1;
}
public void AddElementRange(object[] arrValue)
{
foreach (object obj in arrValue)
{
if (Contains(obj) == false)
{
List.Add(obj);
}
}
}
public void Union(Set setValue)
{
foreach (object obj in setValue)
{
if (Contains(obj) == false)
{
List.Add(obj);
}
}
}
public bool IsSubset(Set setValue)
{
foreach (object obj in setValue)
{
if (Contains(obj) == false)
{
return false;
}
}
return true;
}
public bool IsProperSubset(Set setValue)
{
if (GetCardinality() > setValue.GetCardinality() && IsSubset(setValue) )
{
return true;
}
return false;
}
public void Subtract(Set setValue)
{
foreach (object obj in setValue)
{
if (Contains(obj) == true)
{
RemoveElement(obj);
}
}
}
public bool IsEqual(Set setValue)
{
if (GetCardinality() == setValue.GetCardinality() && IsSubset(setValue))
{
return true;
}
return false;
}
public bool IsEmpty()
{
return List.Count == 0 ? true : false;
}
public int GetCardinality()
{
return List.Count;
}
public bool ElementExist(object value)
{
return Contains(value);
}
public int IndexOf(object value)
{
return (List.IndexOf(value));
}
public bool RemoveElement(object value)
{
if (Contains(value) == true)
{
List.Remove(value);
return true;
}
return false;
}
private bool Contains(object value)
{
// If value is not of type Int16, this will return false.
return List.Contains(value);
}
protected override void OnInsert(int index, object value)
{
if (value == null)
{
throw new ArgumentException("Element cannot not be null.");
}
if (Contains(value) == true )
{
throw new ArgumentException("Element already exists in the set.", "value: " + value.ToString());
}
}
public Array ToArray(Type type)
{
return base.InnerList.ToArray(type);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.