Question
Generics Exercise: Create an application that can apply set operations to a set of values. Generics will be used to specify the type of values
Generics Exercise:
Create an application that can apply set operations to a set of values. Generics will be used to specify the type of values on which the application should apply the set operations.
The applicaton will follow the S.O.L.I.D. design principles.
Interfaces
The ISet interface represents a set of non-duplicating values. Specify a generic type for the interface (
Methods of the ISet interface include Add(T) to add an element of type T to the set, AddAll(IEnumerable
The ISetFactory represents a ISet instantiator that creates ISet objects. There are two methods supported in this interface: CreateSet with no parameters that creates and returns an ISet object with no elements and CreateSet that takes an IEnumerable parameter and returns an ISet object initialized with elements from that parameter. The CreateSet methods must include a type parameter (
Classes
The Set class implements (supports) the ISet interface. A generic type (
The Set class must implement the Add, AddAll, SetUnion, SetIntersect, and SetDifference methods as described in the ISet interface. Each of the five methods should be overridable to allow for future extensions. The Set must also support the IEnumerable
The Set class should not use the IEnumerable
Finally, the Set class must override the ToString method to return a string that contains a comma-separated list for the elements of the set in square brackets (e.g. [element1, element2, element3, ..., elementn]).
The SetFactory class implements (supports) the ISetFactory interface. It implements the two CreateSet methods by calling the appropriate Set class constructors and returning the newly instantiated object.
The Application
Create an application class named ConSet. The application contains a Run method that includes a type parameter so that the application can be run working with a particular type of set (e.g. int elements or double elements, etc). The Run method takes an ISetFactory parameter (dependency injection) which it uses to create the set objects as described below.
The application asks the user to enter two sets of values and then displays the UNION, INTERSECT, and DIFFERENCE between each of the two sets. To get the set of values from the user, create and call the method GetValues. The method must use a generic parameter to indicate the type of value to get from the keyboard. The Convert.ChangeType method can be used to convert the string input (the first parameter) into a type specified by the second parameter (e.g. typeof(T)). You will have to cast the result to be type T. The input should perform an infinite loop and exit when the type conversion is unsuccessful (i.e., throws FormatException) by calling yield break. Otherwise the converted value should be returned ( yield return).
The application can ask the user to enter a sequence of values (for each of the two sequences), create ISet objects based on those sequences, and then display the UNION, INTERSECT, and DIFFERENCE of the two sets. A sample of an execution with an int and a double type is shown below.
Program.cs
The Program.cs file must create the ConSet object, the ISetFactory object and then call the ConSet.Run method specifying the type of data for the set. E.g.
ConSet conset = new ConSet();
ISetFactory setfactory = new SetFactory();
conset.Run
conset.Run
Sample Execution of the Application
Please enter the first set of Int32 values:
1
2
3
Please enter the second set of Int32 values:
3
5
7
[1, 2, 3] UNION [3, 5, 7] = [1, 2, 3, 5, 7]
[1, 2, 3] INTERSECT [3, 5, 7] = [3]
[1, 2, 3] DIFFERENCE [3, 5, 7] = [1, 2]
Please enter the first set of Double values:
1.1
2.2
3.3
Please enter the second set of Double values:
3.3
5.5
7.7
[1.1, 2.2, 3.3] UNION [3.3, 5.5, 7.7] = [1.1, 2.2, 3.3, 5.5, 7.7]
[1.1, 2.2, 3.3] INTERSECT [3.3, 5.5, 7.7] = [3.3]
[1.1, 2.2, 3.3] DIFFERENCE [3.3, 5.5, 7.7] = [1.1, 2.2]
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