Overview
MorseIt uses a HashMap-based translation algorithm to convert plain text into Morse code. The translation is handled by thetranslateToMorse() method in the MainActivity class.
Translation Method
The core translation logic is implemented inMainActivity.java:62-144:
Algorithm Breakdown
1. HashMap Initialization
The method creates a newHashMap<Character, String> that maps individual characters to their Morse code equivalents:
The HashMap is populated with 88 character mappings, including uppercase letters (A-Z), lowercase letters (a-z), digits (0-9), and select punctuation marks.
2. Character Mapping Process
Each character is mapped to its Morse code representation using the.put() method:
3. Character Lookup Algorithm
The translation loop processes each character in the input string:- Iterate through each character in the input string
- Lookup the character in the HashMap
- Append the Morse code if found, or the original character if not found
- Add space after each character for readability
4. Fallback Behavior
If a character is not found in the HashMap (like special symbols or unsupported characters), the algorithm preserves the original character:Unsupported characters are passed through as-is rather than being ignored or causing errors.
StringBuilder Usage
The method usesStringBuilder for efficient string concatenation:
+ operator, especially when processing longer input strings.
Case Sensitivity
The HashMap includes separate entries for both uppercase and lowercase letters, but they map to identical Morse code sequences:Space Handling
Each translated character is followed by a space character to separate Morse code sequences in the output:Performance Characteristics
- Time Complexity: O(n) where n is the length of the input string
- Space Complexity: O(n) for the output StringBuilder
- HashMap Lookup: O(1) average case for character lookup
Source Code Location
The complete implementation can be found in:- File:
app/src/main/java/com/subin/morseit/MainActivity.java - Method:
translateToMorse(String input_field_text) - Lines: 62-144