Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Untoasted_Craven;
- import java.util.ArrayList;
- public class Feedback_2 {
- private static String morseAlphabet = " .- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..";
- private static String asciiAlphabet = " a b c d e f g h i j k l m n o p q r s t u v w x y z";
- public static void main(String[] args) {
- // initialization
- ArrayList<String> morseTable = createMorseLookupTable(morseAlphabet);
- ArrayList<String> asciiTable = createAsciiLookupTable(asciiAlphabet);
- // note: we implicitly require the lookup tables to have the same size! (1 to 1 mapping)
- assert (morseTable.size() == asciiTable.size());
- // "application part": our test code
- String testWord = "brandon";
- String smorsed = smorse(morseTable, asciiTable, splitIntoLetters(testWord));
- System.out.println(smorsed);
- }
- public static ArrayList<String> createMorseLookupTable(String alphabet) {
- String[] letters = alphabet.split(" "); // note: it's sufficient to split only once!
- ArrayList<String> result = new ArrayList<String>();
- for (int i = 0; i < letters.length; i++) {
- result.add(letters[i]);
- }
- return result;
- }
- public static ArrayList<String> createAsciiLookupTable(String alphabet) {
- String[] letters = alphabet.split(" "); // note: it's sufficient to split only once!
- ArrayList<String> result = new ArrayList<String>();
- for (int i = 0; i < letters.length; i++) {
- result.add(letters[i]);
- }
- return result;
- }
- public static ArrayList<String> splitIntoLetters(String word) {
- String[] letters = word.split("a{0}"); // note: it's sufficient to split only once!
- ArrayList<String> result = new ArrayList<String>();
- for (int i = 0; i < letters.length; i++) {
- result.add(letters[i]);
- }
- return result;
- }
- public static String smorse(ArrayList<String> morseTable, ArrayList<String> asciiTable, ArrayList<String> word) {
- String result = "";
- int wordIndex = 0;
- for (int asciiIndex = 0; asciiIndex < asciiTable.size(); asciiIndex++) {
- boolean lettersMatch = word.get(wordIndex).equals(asciiTable.get(asciiIndex));
- if (lettersMatch) {
- result += morseTable.get(asciiIndex);
- wordIndex++;
- asciiIndex = 0;
- }
- if (wordIndex == word.size()) {
- break;
- }
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement