Saturday, January 24, 2015

Morse Code was developed by Samuel Morse in 1832 to be used in telegraph systems. Morse code assigns a series of dots and hyphens to each of the letter of the alphabet, each digit, and some few special character such as period, comma, colon and semi-colon. Separation of words is indicated by a forward slash(/). Want to know about Morse Code?
so, i decided to write a simple Java program that can generate a morse code within a given array of letters from the user. Just take a look and feel free to comment and suggest.

package teamUndefined;

import java.io.*;

public class morseCodeEncoder {
   
    public static void main(String[] args)throws IOException{
       
        BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));

/*since letters , numbers and its equivalent morse code are static or fix, i decided to put it on 2 dimensional array, for me to not having a hard time to iterate and compare those value into user inputs*/      

        String[][] _array = {
                {"A",".-"}, {"B","-..."}, {"C","-.-."},{"D","-.."},
                {"E","."}, {"F","..-."}, {"G","--."}, {"H","...."},
                {"I",".."}, {"J",".---"}, {"L",".-.."}, {"M","--"},
                {"N","-."}, {"O","---"}, {"P",".--."}, {"Q","--.-"},
                {"R",".-."},{"S","..."}, {"T","-"}, {"U","..-"},
                {"V","...-"}, {"W",".--"}, {"X","-..-"}, {"Y","-.--"},
                {"Z","--.."},{"1",".----"},{"2","..---"},{"3","...--"},
                {"4","....-"},{"5","....."},{"6","-...."},{"7","--..."},
                {"8","---.."},{"9","----."},{"0","-----"},{" ","/"}
        };
     
/*now we need to declare 2 arrays, for us to have a container for user input and of course for the result as well */


        String[] _inputArray, _resArray;
        String _input;       
        _input = buff.readLine();       
        _inputArray = new String[_input.length()];       
   
/*this loop can fill-in the input array variable, using substring to slice it one by one*/
  
        for (int i = 0; i < _input.length(); i++) {
            _inputArray[i] = _input.substring(i, i + 1).toUpperCase();
        } 

        _resArray = new String[_input.length()];   


/*and now we can iterate and compare user input into morse code which is stored on 2 dimensional array, and store it on result array*/
  
        for (int i = 0; i < _inputArray.length; i++) {
            for (int j = 0; j < _array.length; j++) {
                for (int j2 = 0; j2 < 2; j2++) {                   
                    if (_inputArray[i].equals(_array[j][0])) {
                        _resArray[i] = _array[j][1] + " ";
                    }
                }
            }
        }

/*and we can now simply iterate on result array and display the value */

        for (int i = 0; i < _resArray.length; i++) {
            System.out.print(_resArray[i]);
        }
    }
   
}

Hope you like it, and hoping to developed your thinking skills on how to formulate an simple and basic algorithm.

0 comments:

Post a Comment