Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Write a program that can translate Morse code in the format of ...---...
- A space and a slash will be placed between words. ..- / --.-
- For bonus, add the capability of going from a string to Morse code.
- Super-bonus if your program can flash or beep the Morse.
- This is your Morse to translate:
- .... . .-.. .-.. --- / -.. .- .. .-.. -.-- / .--. .-. --- --. .-. .- -- -- . .-. / --. --- --- -.. / .-.. ..- -.-. -.- / --- -. / - .... . / -.-. .... .- .-.. .-.. . -. --. . ... / - --- -.. .- -.--
- */
- import java.util.Scanner;
- import java.util.HashMap;
- import java.util.Map;
- public class easy7{
- public static final java.util.Map<Character, String> codeLibrary;
- static{
- codeLibrary = new HashMap<>();
- codeLibrary.put('a',".-"); codeLibrary.put('b',"-...");
- codeLibrary.put('c',"-.-."); codeLibrary.put('d',"-..");
- codeLibrary.put('e',"."); codeLibrary.put('f',"..-.");
- codeLibrary.put('g',"--."); codeLibrary.put('h',"....");
- codeLibrary.put('i',".."); codeLibrary.put('j',".---");
- codeLibrary.put('k',"-.-"); codeLibrary.put('l',".-..");
- codeLibrary.put('m',"--"); codeLibrary.put('n',"-.");
- codeLibrary.put('o',"---"); codeLibrary.put('p',".--.");
- codeLibrary.put('q',"--.-"); codeLibrary.put('r',".-.");
- codeLibrary.put('s',"..."); codeLibrary.put('t',"-");
- codeLibrary.put('u',"..-"); codeLibrary.put('v',"...-");
- codeLibrary.put('w',".--"); codeLibrary.put('x',"-..-");
- codeLibrary.put('y',"-.--"); codeLibrary.put('z',"--..");
- codeLibrary.put('1',".----"); codeLibrary.put('2',"..---");
- codeLibrary.put('3',"...--"); codeLibrary.put('4',".----");
- codeLibrary.put('5',"....."); codeLibrary.put('6',"-....");
- codeLibrary.put('7',"--..."); codeLibrary.put('8',"---..");
- codeLibrary.put('9',"----."); codeLibrary.put('0',"-----");
- codeLibrary.put(' ',"/");
- }
- public static final java.util.Map<String, Character> letterLibrary;
- static{
- letterLibrary = new HashMap<>();
- letterLibrary.put(".-", 'a'); letterLibrary.put("-...", 'b');
- letterLibrary.put("-.-.", 'c'); letterLibrary.put("-..", 'd');
- letterLibrary.put(".", 'e'); letterLibrary.put("..-.", 'f');
- letterLibrary.put("--.",'g'); letterLibrary.put("....", 'h');
- letterLibrary.put("..", 'i'); letterLibrary.put(".---", 'j');
- letterLibrary.put("-.-", 'k'); letterLibrary.put(".-..", 'l');
- letterLibrary.put("--", 'm'); letterLibrary.put("-.", 'n');
- letterLibrary.put("---", 'o'); letterLibrary.put(".--.", 'p');
- letterLibrary.put("--.-", 'q'); letterLibrary.put(".-.", 'r');
- letterLibrary.put("...", 's'); letterLibrary.put("-", 't');
- letterLibrary.put("..-", 'u'); letterLibrary.put("...-", 'v');
- letterLibrary.put(".--", 'w'); letterLibrary.put("-..-", 'x');
- letterLibrary.put("-.--", 'y'); letterLibrary.put("--..", 'z');
- letterLibrary.put(".----", '1');letterLibrary.put("..---", '2');
- letterLibrary.put("...--", '3');letterLibrary.put(".----", '4');
- letterLibrary.put(".....", '5');letterLibrary.put("-....", '6');
- letterLibrary.put("--...", '7');letterLibrary.put("---..", '8');
- letterLibrary.put("----.", '9');letterLibrary.put("-----", '0');
- letterLibrary.put("/", ' ');
- }
- public static void main(String args[]){
- String choice;
- String userInput;
- String userOutput;
- System.out.println("Welcome to the Morse code translator! \nWould you like to encode or decode Morse code?");
- choice = input();
- System.out.println("Please enter your String: ");
- userInput = input();
- if(choice.equals("encode")){
- userInput.toLowerCase();
- userOutput = encode(userInput);
- System.out.println("The encoded string is: " + userOutput);
- System.exit(0);
- }
- else if(choice.equals("decode")){
- userOutput = decode(userInput);
- System.out.println("The decoded string is" + userOutput);
- System.exit(0);
- }
- else{
- System.out.println("Sorry but your input could not be read, goodbye!");
- System.exit(0);
- }
- }
- public static String encode(final String userInput){
- final StringBuilder morse = new StringBuilder();
- for(int i = 0; i < userInput.length(); i++){
- if(i != 0){
- morse.append(' ');
- }
- morse.append(codeLibrary.get(userInput.charAt(i)));
- }
- return morse.toString();
- }
- public static String decode(String userInput){
- final StringBuilder morse = new StringBuilder();
- for(int i = 0; i < userInput.length(); i++){
- if(i != 0){
- morse.append(' ');
- }
- morse.append(letterLibrary.get(userInputcharAt());
- }
- return morse.toString();
- }
- public static String input(){
- Scanner inputs = new Scanner(System.in);
- String input = inputs.nextLine();
- return input;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement