Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. /*
  2. * @author:
  3. * period:
  4. *
  5. * directions: complete the methods below. the lab should be case insensitive
  6. *
  7. * You should use the TreeNode class provided in this project
  8. * I have been told the java version of TreeNode is more complicated
  9. * to use than this simpler TreNode class provided by The College Board
  10. * during the years of the AP Computer Science AB test
  11. */
  12.  
  13. import java.util.TreeMap;
  14.  
  15. public class MorseCode
  16. {
  17. private static final char DOT = '.';
  18. private static final char DASH = '-';
  19.  
  20. public static TreeMap<Character, String> codeMap;
  21. public static TreeNode decodeTree;
  22.  
  23. // this method is complete
  24. public static void start()
  25. {
  26. codeMap = new TreeMap<Character, String>();
  27. decodeTree = new TreeNode(new Character(' '), null, null);
  28. // put a space in the root of the decoding tree
  29.  
  30. addSymbol('A', ".-");
  31. addSymbol('B', "-...");
  32. addSymbol('C', "-.-.");
  33. addSymbol('D', "-..");
  34. addSymbol('E', ".");
  35. addSymbol('F', "..-.");
  36. addSymbol('G', "--.");
  37. addSymbol('H', "....");
  38. addSymbol('I', "..");
  39. addSymbol('J', ".---");
  40. addSymbol('K', "-.-");
  41. addSymbol('L', ".-..");
  42. addSymbol('M', "--");
  43. addSymbol('N', "-.");
  44. addSymbol('O', "---");
  45. addSymbol('P', ".--.");
  46. addSymbol('Q', "--.-");
  47. addSymbol('R', ".-.");
  48. addSymbol('S', "...");
  49. addSymbol('T', "-");
  50. addSymbol('U', "..-");
  51. addSymbol('V', "...-");
  52. addSymbol('W', ".--");
  53. addSymbol('X', "-..-");
  54. addSymbol('Y', "-.--");
  55. addSymbol('Z', "--..");
  56. addSymbol('0', "-----");
  57. addSymbol('1', ".----");
  58. addSymbol('2', "..---");
  59. addSymbol('3', "...--");
  60. addSymbol('4', "....-");
  61. addSymbol('5', ".....");
  62. addSymbol('6', "-....");
  63. addSymbol('7', "--...");
  64. addSymbol('8', "---..");
  65. addSymbol('9', "----.");
  66. addSymbol('.', ".-.-.-");
  67. addSymbol(',', "--..--");
  68. addSymbol('?', "..--..");
  69. }
  70.  
  71. /**
  72. * Inserts a letter and its Morse code string into the encoding map (codeMap)
  73. * and calls treeInsert to insert them into the decoding tree.
  74. */
  75. private static void addSymbol(char letter, String code)
  76. {
  77. codeMap.put(letter, code);
  78. treeInsert(letter, code);
  79. }
  80.  
  81. /**
  82. * Inserts a letter according to its Morse code string into the
  83. * decoding tree. Each dot-dash string corresponds to a path
  84. * in the tree from the root to a node: at a "dot" go left, at a "dash" go
  85. * right. The node at the end of the path holds the symbol
  86. * for that code string. See the word documents for more help.
  87. */
  88. private static void treeInsert(char letter, String code)
  89. {
  90. TreeNode temp = decodeTree;
  91. for (int i = 0; i<code.length(); i++)
  92. {
  93. char c = code.charAt(i);
  94. if (c == DOT)
  95. {
  96. if(temp.getLeft() == null)
  97. {
  98. TreeNode node = new TreeNode(c);
  99. temp.setLeft(node);
  100.  
  101. }
  102.  
  103. temp = temp.getLeft();
  104.  
  105. }
  106. else if (c == DASH)
  107. {
  108. if (temp.getRight() == null)
  109. {
  110. TreeNode node = new TreeNode(c);
  111. temp.setRight(node);
  112.  
  113. }
  114.  
  115. temp = temp.getRight();
  116.  
  117. }
  118.  
  119. }
  120. temp.setValue(Character.valueOf(letter));
  121. }
  122.  
  123. /**
  124. * Converts text into a Morse code message. Adds a space after a dot-dash
  125. * sequence for each letter. Other spaces in the text are transferred directly
  126. * into the encoded message.
  127. * Returns the encoded message.
  128. */
  129. public static String encode(String text)
  130. {
  131. String morse = "";
  132. for(int i = 0;i<text.length();i++)
  133. {
  134. Character x = Character.valueOf(text.charAt(i));
  135. if(x == ' ')
  136. {
  137. morse += " ";
  138. }
  139. else
  140. {
  141. morse += codeMap.get(x) + " ";
  142. }
  143. }
  144. return morse;
  145. }
  146.  
  147. /**
  148. * Converts a Morse code message into a text string. Assumes that dot-dash
  149. * sequences for each letter are separated by one space. Additional spaces are
  150. * transferred directly into text.
  151. * Returns the plain text message.
  152. */
  153. public static String decode(String morse)
  154. {
  155. StringBuffer text = new StringBuffer(100);
  156. TreeNode temp = decodeTree;
  157. for (int i = 0; i < morse.length(); i++)
  158. {
  159. char x = morse.charAt(i);
  160. if (x == DOT)
  161. {
  162. temp = temp.getLeft();
  163. }
  164. else if (x == DASH)
  165. {
  166. temp = temp.getRight();
  167. }
  168. else if (x == ' ')
  169. {
  170. text.append(temp.getValue());
  171. if(morse.charAt(i-1) == ' ')
  172. {
  173.  
  174. }
  175. temp = decodeTree;
  176. }
  177. }
  178. return text.toString();
  179. }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement