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

C sharp please Create a Map class that stores a collection of key/value pairs. T

ID: 3702731 • Letter: C

Question

C sharp please

Create a Map class that stores a collection of key/value pairs. The key/value pairs should be implemented using a Pair class that has the fields and properties First and Second. The Pair class and the Map class must be generic so that the keys can be of any type and the values can be of any type. The Map class should include methods for adding a new key/value pair, searching for a value by supplying the key, returning all the keys in a map, clearing the map in order to start over, and a size method that returns the number of key/value pairs in the map.

Use the Map class in an application of your choosing. You can implement a word dictionary or a phone book or something else that functions like a dictionary

Explanation / Answer

Program:

[SerializableAttribute]

[DefaultMemberAttribute("Item")]

public class Map<D,R> : CompoundValue, IDictionary<D,R>, ICollection<KeyValuePair<D,R>>,

        IEnumerable<KeyValuePair<D,R>>, IEnumerable

config ParameterCombination: Main

{

    action abstract static bool Implementation.AddKey(string key)

        where key in { "green", "yellow", "red" };

    action abstract static bool Implementation.RemoveKey(string key)

        where key in { "green", "yellow", "cyan" };

    action abstract static void

        Implementation.SetValues(Map<string, int> valuesMap)

        where valuesMap in {

            Map<string, int>{ "green"->1, "blue"->3, "cyan"->5 },

            Map<string, int>{ "green"->5, "red"->4, "yellow"->7 },

            Map<string, int>{} };

    action abstract static Map<string, int>

        Implementation.GetValues(Set<string> keySet)

        where keySet in {

            Set<string>{ "green", "blue" },

            Set<string>{ "red", "orange" },

            Set<string>{ "cyan" },

            Set<string>{} };   

    action abstract static Sequence<int>

        Implementation.RemoveDuplicateElements(Sequence<int> queue)

        where queue in {

            Sequence<int>{ 1, 3, 2 },

            Sequence<int>{ 2, 2, 2 },

            Sequence<int>{ 1 },

            Sequence<int>{} };

}

namespace CollectionInitializers

{

    static class PropertiesModel

    {

        private static MapContainer<string, int> properties =

            new MapContainer<string, int>();

        [Rule]

        static bool AddKey(string key)

        {

            if (properties.ContainsKey(key) || key == null)

            {

                return false;

            }

            else

            {

                properties[key] = 0;

                return true;

            }

        }

        [Rule]

        static bool RemoveKey(string key)

        {

            return properties.Remove(key);

        }

        [Rule]

        static Set<string> GetKeys()

        {

            return new Set<string>(properties.Keys);

        }

        [Rule]

        static void SetValues(Map<string, int> valuesMap)

        {

            if (valuesMap == null) return;

            foreach (string key in valuesMap.Keys)

            {

                if (properties.ContainsKey(key))

                {

                    properties[key] = valuesMap[key];

                }

            }

        }

        [Rule]

        static Map<string, int> GetValues(Set<string> keySet)

        {

            if (keySet == null) return new Map<string, int>();

            MapContainer<string, int> tempMap = new MapContainer<string, int>();

            foreach (string key in keySet)

            {

                if (properties.ContainsKey(key))

                {

                    tempMap.Add(key, properties[key]);

                }

            }

            return new Map<string, int>(tempMap);

        }

        [Rule]

        static void Clear()

        {

            properties.Clear();

        }

        [Rule]

        static Sequence<int> RemoveDuplicateElements(Sequence<int> queue)

        {

            return (queue != null) ?

                new Sequence<int>(queue.Distinct()) : new Sequence<int>();

        }

    }

}