Advertisement
Guest User

[Java] Morse code program

a guest
Aug 21st, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.60 KB | None | 0 0
  1. /*Write a program that can translate Morse code in the format of ...---...
  2. A space and a slash will be placed between words. ..- / --.-
  3. For bonus, add the capability of going from a string to Morse code.
  4. Super-bonus if your program can flash or beep the Morse.
  5. This is your Morse to translate:
  6. .... . .-.. .-.. --- / -.. .- .. .-.. -.-- / .--. .-. --- --. .-. .- -- -- . .-. / --. --- --- -.. / .-.. ..- -.-. -.- / --- -. / - .... . / -.-. .... .- .-.. .-.. . -. --. . ... / - --- -.. .- -.--
  7. */
  8.  
  9. import java.util.Scanner;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12.  
  13. public class easy7{
  14.      public static final java.util.Map<Character, String> codeLibrary;
  15.      static{
  16.         codeLibrary = new HashMap<>();
  17.         codeLibrary.put('a',".-");  codeLibrary.put('b',"-...");
  18.         codeLibrary.put('c',"-.-.");    codeLibrary.put('d',"-..");
  19.         codeLibrary.put('e',".");   codeLibrary.put('f',"..-.");
  20.         codeLibrary.put('g',"--."); codeLibrary.put('h',"....");
  21.         codeLibrary.put('i',"..");  codeLibrary.put('j',".---");
  22.         codeLibrary.put('k',"-.-"); codeLibrary.put('l',".-..");
  23.         codeLibrary.put('m',"--");  codeLibrary.put('n',"-."); 
  24.         codeLibrary.put('o',"---"); codeLibrary.put('p',".--.");
  25.         codeLibrary.put('q',"--.-");    codeLibrary.put('r',".-.");
  26.         codeLibrary.put('s',"..."); codeLibrary.put('t',"-");
  27.         codeLibrary.put('u',"..-"); codeLibrary.put('v',"...-");
  28.         codeLibrary.put('w',".--"); codeLibrary.put('x',"-..-");
  29.         codeLibrary.put('y',"-.--");    codeLibrary.put('z',"--..");
  30.         codeLibrary.put('1',".----");   codeLibrary.put('2',"..---");
  31.         codeLibrary.put('3',"...--");   codeLibrary.put('4',".----");
  32.         codeLibrary.put('5',".....");   codeLibrary.put('6',"-....");
  33.         codeLibrary.put('7',"--...");   codeLibrary.put('8',"---..");
  34.         codeLibrary.put('9',"----.");   codeLibrary.put('0',"-----");
  35.         codeLibrary.put(' ',"/");
  36.        }
  37.        public static final java.util.Map<String, Character> letterLibrary;
  38.      static{
  39.         letterLibrary = new HashMap<>();
  40.         letterLibrary.put(".-", 'a');   letterLibrary.put("-...", 'b');
  41.         letterLibrary.put("-.-.", 'c'); letterLibrary.put("-..", 'd');
  42.         letterLibrary.put(".", 'e');    letterLibrary.put("..-.", 'f');
  43.         letterLibrary.put("--.",'g');   letterLibrary.put("....", 'h');
  44.         letterLibrary.put("..", 'i');   letterLibrary.put(".---", 'j');
  45.         letterLibrary.put("-.-", 'k');  letterLibrary.put(".-..", 'l');
  46.         letterLibrary.put("--", 'm');   letterLibrary.put("-.", 'n');  
  47.         letterLibrary.put("---", 'o');  letterLibrary.put(".--.", 'p');
  48.         letterLibrary.put("--.-", 'q'); letterLibrary.put(".-.", 'r');
  49.         letterLibrary.put("...", 's');  letterLibrary.put("-", 't');
  50.         letterLibrary.put("..-", 'u');  letterLibrary.put("...-", 'v');
  51.         letterLibrary.put(".--", 'w');  letterLibrary.put("-..-", 'x');
  52.         letterLibrary.put("-.--", 'y'); letterLibrary.put("--..", 'z');
  53.         letterLibrary.put(".----", '1');letterLibrary.put("..---", '2');
  54.         letterLibrary.put("...--", '3');letterLibrary.put(".----", '4');
  55.         letterLibrary.put(".....", '5');letterLibrary.put("-....", '6');
  56.         letterLibrary.put("--...", '7');letterLibrary.put("---..", '8');
  57.         letterLibrary.put("----.", '9');letterLibrary.put("-----", '0');
  58.         letterLibrary.put("/", ' ');
  59.        }
  60.     public static void main(String args[]){
  61.         String choice;
  62.         String userInput;
  63.         String userOutput;
  64.        
  65.         System.out.println("Welcome to the Morse code translator! \nWould you like to encode or decode Morse code?");
  66.         choice = input();
  67.         System.out.println("Please enter your String: ");
  68.         userInput = input();
  69.        
  70.         if(choice.equals("encode")){
  71.             userInput.toLowerCase();
  72.             userOutput = encode(userInput);
  73.             System.out.println("The encoded string is: " + userOutput);
  74.             System.exit(0);
  75.         }
  76.         else if(choice.equals("decode")){
  77.             userOutput = decode(userInput);
  78.             System.out.println("The decoded string is" + userOutput);
  79.             System.exit(0);
  80.         }
  81.         else{
  82.             System.out.println("Sorry but your input could not be read, goodbye!");
  83.             System.exit(0);
  84.         }
  85.     }
  86.     public static String encode(final String userInput){
  87.         final StringBuilder morse = new StringBuilder();
  88.        
  89.         for(int i = 0; i < userInput.length(); i++){
  90.             if(i != 0){
  91.                 morse.append(' ');
  92.             }
  93.             morse.append(codeLibrary.get(userInput.charAt(i)));
  94.         }
  95.         return morse.toString();
  96.     }
  97.     public static String decode(String userInput){
  98.         final StringBuilder morse = new StringBuilder();
  99.        
  100.         for(int i = 0; i < userInput.length(); i++){
  101.             if(i != 0){
  102.                 morse.append(' ');
  103.             }
  104.             morse.append(letterLibrary.get(userInputcharAt());
  105.         }
  106.         return morse.toString();
  107.     }
  108.     public static String input(){
  109.         Scanner inputs = new Scanner(System.in);
  110.         String input = inputs.nextLine();
  111.         return input;
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement