Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 ( ). In order to make sure values are non-duplicating, implementors must be able to test for equality among the type elements. This can be accomplished by ensuring that the type specified supports the IEquatable interface (a type parameter constraint). The IEquatable requires that the implementing type supports an Equals method to test for equality. The set represents a sequence by inheriting the IEnumerable interface.

Methods of the ISet interface include Add(T) to add an element of type T to the set, AddAll(IEnumerable) to add a sequence of values of type T to the set. Both Add and AddAll prevent duplicate values from being added to the set. The methods Add and AddAll do not return anything. The supported set methods are SetUnion, SetIntersect, and SetDifference. Each of these methods takes an ISet parameter and returns a new ISet object. The SetUnion method returns a new ISet object that contains all elements of the current set object and the parameter set object, eliminating any duplications. The SetIntersect method returns a new ISet object that contains only elements that are in both the current set and the parameter set objects. The SetDifference method returns a new ISet object that contains only elements that are in the current set object that are not in the parameter set object.

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 ( ) to indicate the type of ISet to be created. The method type parameter will have the same type constraint as the ISet interface.

Classes

The Set class implements (supports) the ISet interface. A generic type ( ) is required and must be constrained as it is in the ISet interface. You can implement the storage for the set data using a List as a private class field. The class must support 2 constructors: The default constructor creates a Set with no elements. A second constructor creates a Set that is initialized with a sequence of values from an IEnumerable parameter. The constructor must ensure that duplicate values are not stored in the set.

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 interface (defined in ISet) by implementing the IEnumerable.GetEnumerator and IEnumerable.GetEnumerator methods (that is, the generic and non-generic versions of the method). To implement these methods, simply call the backing List object's GetEnumerator method and return the enumerator returned by those methods. Note, to call the list's IEnumerable.GetEnumerator method, you must cast the list object to an IEnumerable (non-generic) type before calling GetEnumerator.

The Set class should not use the IEnumerable extension methods Union, Intersect, or Except.

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(setfactory);

conset.Run(setfactory);

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Explain the distinction(s) between agency funds and trust funds.

Answered: 1 week ago

Question

Describe the three types of start-up firms.

Answered: 1 week ago