/** * This is a MorseCode class which calls upon MorseCode to translate MorseCode to text and back. * A wiki i used to locate information http://en.wikipedia.org/wiki/Morse-code * * @author Freeman Lo * @version 0.0.2 Build 2 2015.10.10. * Limitations - hardcoded morse code * I catered morse code to fit for this website, pretty much * anything entered here can be translated on this website * http://morsecode.scphillips.com/translator.html */ import java.io.*; import java.util.*; import java.lang.*; public class MorseCode{ private String name; private int length; private Map map = new HashMap(); // Cap letters have the same morse code as lower case letters public MorseCode() { map.put("A", ".-" ); map.put("B", "-..."); map.put("C", "-.-."); map.put("D", "-.." ); map.put("E", "." ); map.put("F", "..-."); map.put("G", "--." ); map.put("H", "...."); map.put("I", ".." ); map.put("J", ".---"); map.put("K", "-.-" ); map.put("L", ".-.."); map.put("M", "--" ); map.put("N", "-." ); map.put("O", "---" ); map.put("P", ".--."); map.put("Q", "--.-"); map.put("R", ".-." ); map.put("S", "..." ); map.put("T", "-" ); map.put("U", "..-" ); map.put("V", "...-"); map.put("W", ".--" ); map.put("X", "-..-"); map.put("Y", "-.--"); map.put("Z", "--.."); map.put("1",".----"); map.put("2","..---"); map.put("3","...--"); map.put("4","...--"); map.put("5","....."); map.put("6","-...."); map.put("7","--..."); map.put("8","---.."); map.put("9","----."); map.put("0","-----"); map.put(":", "---..."); map.put(",", "--..--"); map.put("?", "..--.."); map.put(".-", "A"); map.put("-...", "B"); map.put("-.-.", "C"); map.put("-..", "D"); map.put(".", "E"); map.put("..-.", "F"); map.put("--.", "G"); map.put("....", "H"); map.put("..", "I"); map.put(".---", "J"); map.put("-.-", "K"); map.put(".-..", "L"); map.put("--", "M"); map.put("-.", "N"); map.put("---", "O"); map.put(".--.", "P"); map.put("--.-", "Q"); map.put(".-.", "R"); map.put("...", "S"); map.put("-", "T"); map.put("..-", "U"); map.put("...-", "V"); map.put(".--", "W"); map.put("-..-", "X"); map.put("-.--", "Y"); map.put("--..", "Z"); map.put(".----","1"); map.put("..---","2"); map.put("...--","3"); map.put("...--","4"); map.put(".....","5"); map.put("-....","6"); map.put("--...","7"); map.put("---..","8"); map.put("----.","9"); map.put("-----","0"); map.put("---...",":"); map.put("--..--",","); map.put("..--..","?"); // Special set of Predefined Symbols // i couldnt find the following symbols on its morse code counterpart // therefore, if you use them it will not be translatable. map.put("!","!"); map.put(".","."); map.put(";",";"); } public void setName(String name) { this.name = name; } public void setLength(int length) { this.length = length; } public void convertToMC() { String key; String mess = ""; int x = 0; int y = 1; while ( y <= this.length ) { key = this.name.substring(x,y); String value = map.get(key); if (value == null) { value = "|"; mess = mess + value + " "; } else { mess = mess + value + " "; } x++; y++; } System.out.print("Morse Code is: "); System.out.println(mess); } /* This part will be painful for a user to enter in a Morse Code. * Also, the chance of having an user to misstype something is highly possible. * It would be difficult for a programmer in a short amount of time * to find ways to catch all of these cases. * It is easy to prompt the user for a value and allow this algorithm to process * any morse code back to a human readable string. `* therefore for simplicity sake, i hardcoded the morse code and allow * a method to translate it back to a readable string. */ // Convert MC to Text public void convertToText() { String word = "-- .- -.-- -.-- --- ..- ..-. .. -. -.. .--. . .- -.-. . .. -. -.-- --- ..- .-. -.. --- -- .- .. -."; String [] arr = word.split(" "); String key = ""; int i = 0; String mess = ""; while ( i < arr.length ){ key = arr[i]; String value = map.get(key); if ( value == null ) { value = ""; mess = mess + value; } else { mess = mess + value; } i++; } System.out.println(); System.out.println("Morse Code to translate is: " + word); System.out.print("Translated Morse code is: "); System.out.print(mess); System.out.println(); } public void printMap() { Iterator it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry)it.next(); System.out.println(pairs.getKey() + " = " + pairs.getValue()); } } public int getMapSize() { return map.size(); } }