Login Register






Programming Challenge - Morse Code Cipher filter_list
Author
Message
RE: Programming Challenge - Morse Code Cipher #10
I can help you out further, there's nothing truly wrong with creating your own class to encapsulate the data, but why exclude the functionality from the implementation? The beauty of a class is defined by both of these; functionality, AND data. For instance, add the implementation:

Code:
class MorseCodeConverter { Dictionary<char, string> morseMap = new Dictionary<char, string> { {'A', ".-"}, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."}, {'E', "."}, {'F', "..-."}, {'G', "--."}, {'H', "...."}, {'I', ".."}, {'J', ".---"}, {'K', "-.-"}, {'L', ".-.."}, {'M', "--"}, {'N', "-."}, {'O', "---"}, {'P', ".--."}, {'Q', "--.-"}, {'R', ".-."}, {'S', "..."}, {'T', "-"}, {'U', "..-"}, {'V', "...-"}, {'W', ".--"}, {'X', "-..-"}, {'Y', "-.--"}, {'Z', "--.."}, {'0', "-----"}, {'1', ".----"}, {'2', "..---"}, {'3', "...--"}, {'4', "....-"}, {'5', "....."}, {'6', "-...."}, {'7', "--..."}, {'8', "---.."}, {'9', "----."} }; public string this[char key] { get { char c = char.ToUpper(key); if (morseMap.ContainsKey(c)) return morseMap[c]; return "????"; } } public char this[string val] { get { if (morseMap.ContainsValue(val)) return morseMap.First(kv => kv.Value.Equals(val, StringComparison.OrdinalIgnoreCase)).Key; return '?'; } } }

Very easy usage:
Code:
MorseCodeConverter morseCodeConverter = new MorseCodeConverter(); Console.WriteLine(morseCodeConverter[".-"]); // Writes out "A" Console.WriteLine(morseCodeConverter['A']); // Writes out ".-"

You could include TryParseMorse() methods for instance to allow the user to parse and see if the result is usable or not even... You can do many things with this, the whole point of a class is not limited to what you demonstrate it as being capable of or useful for, unless we're talking about a very limited idea of a class here.

Reply





Messages In This Thread