Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The insertSymbol and lookupSymbol I need help with: * This is used as a binary tree representing the decoding of morse * dot / dash

The insertSymbol and lookupSymbol I need help with: * This is used as a binary tree representing the decoding of morse * "dot / dash" sequences (e.g., "." or "-"). (Morse *encoding* is * not done in this class.) * * The idea is that a sequence of dots and dashes can be use to * determine a path from the tree root to some node -- internal * or leaf -- at which the symbol corresponding to that dot/dash * sequence is stored. Rather than naming the subtrees "left" * and "right", there are instead named within a MorseTreeNode as * "dot" and "dash". * * Note: Not every internal node will correspond to the end of some * dot/dash path of a legitimate symbol; therefore if an internal * node stores *null* as the symbol, then this means there is no * morse code sequence from the root to this node that corresponds * to a legitimate symbol. * */ package morse; public class MorseTree { MorseTreeNode root; public MorseTree() { } /* * PURPOSE: * * Given a one-character symbol and its corresponding Morse code * equivalent, update a binary tree to enable decoding of that * equivalent. * * Unlike operations on a binary search tree where an insert() * operation normally results in at most one new node, this * insert might result in more than one. In essence, after * the insertion is complete, there will be a path from the * root to some node (not necessarily a leaf node!) where * the combination of dot and dash edges matches the code * given as the parameter. The node at the end of the path will * be set to contain the symbol. * * * */ public void insertSymbol(char symbol, String code) { return; } /* * PURPOSE: * * Given some sequence of dots (".") and dashes ("-"), * traverse the binary tree at root from the start to the end * of the sequence. "Traverse" here means follow the "dot" * link if the char in the sequence is the "." character, and * follow the "dash" link if the char in the sequence is the * "-" character. Examine the node at the end of that sequence. * There are three possibilities: * * (1) The node's symbol is not null. In this case, the * morse-code sequence is meaningful, and we return * the symbol at that node. * * (2) The node's symbol *is* null. In this case, the * morse-code sequence is *not* meaningful. We * return null (and the caller of lookupSymbol() must * explicitly check for this possibility. * * (3) The sequence is longer than any path in the * tree. As with (2), this means the morse-code * sequence is not meaningful (i.e., garbage?) and * so we return null. Again as with (2) the caller * of lookupSymbol must */ public Character lookupSymbol(String code) { return ' '; } } 

The Node class:

/* * MorseTreeNode.java * * Used to implement the binary tree for morse-signal decoding. * Rather than having a "left" and "right" references, we * instead have the names "dot" and "dash". */ package morse; public class MorseTreeNode { Character symbol; MorseTreeNode dot; MorseTreeNode dash; MorseTreeNode(Character symbol) { this.symbol = symbol; dot = dash = null; } } 

the Tester class for this:

import java.util.List; import java.util.LinkedList; import morse.*; public class A4test { private static int testCount = 1; private static boolean testArraySolution = false; private static int stressTestSize = 200; /* itu_symbol[] and itu_morse[] are based on the International * Telecommunication Union's standard for Morse code. * * * This information has been broken into two arrays -- one * of characters, and one of strings. The arrays are exactly * the same size. For example, if index = 2, then the * symbol is 'c', and the morse code for 'c' is at itu_morse[2] * (i.e., "-.-." or "dash dot dash dot"). */ private static char[] itu_symbol = { '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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '?', '!', '(', ')', ':', ';', ',', '\'', '/', '&', '-' }; private static String[] itu_morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", ".-.-.-", "..--..", "-.-.--", "-.--.", "-.--.-", "---...", "-.-.-.", "--..--", ".----.", "-..-.", ".-...", "-....-" }; public static void displayResults (boolean passed) { /* Using some ideas from http://bit.ly/2jCNQ1L that helps * identify the line number corresponding to some run-time * code point. Exceptions are needed here (i.e., we need * to throw an exception at the code point of interest). */ if (passed) { System.out.println ("Passed test: " + testCount); } else { System.out.println ("Failed test: " + testCount + " at line " + Thread.currentThread().getStackTrace()[2].getLineNumber() ); System.exit(1); } testCount++; } public static void testEmptyMorseTree() { System.out.println("testEmptyMorseTree"); MorseTree mt = new MorseTree(); displayResults(mt.lookupSymbol("--") == null); } public static void testSimpleMorseTree() { System.out.println("testSimpleMorseTree"); MorseTree mt = new MorseTree(); mt.insertSymbol('e', "."); mt.insertSymbol('t', "-"); displayResults(mt.lookupSymbol(".").equals('e')); displayResults(mt.lookupSymbol("-").equals('t')); displayResults(mt.lookupSymbol(".-") == null); displayResults(mt.lookupSymbol("-.") == null); } public static void testBasicMorseTree() { System.out.println("testBasicMorseTree"); boolean passTest; MorseTree mt = new MorseTree(); for (int i = 0; i < itu_symbol.length; i++) { mt.insertSymbol(itu_symbol[i], itu_morse[i]); } passTest = true; for (int i = 0; i < itu_symbol.length; i++) { if (!mt.lookupSymbol(itu_morse[i]).equals(itu_symbol[i])) { passTest = false; } } displayResults(passTest); passTest = true; for (int i = itu_symbol.length - 1; i <= 0; i--) { if (!mt.lookupSymbol(itu_morse[i]).equals(itu_symbol[i])) { passTest = false; } } displayResults(passTest); } public static void testBasicOperator() { System.out.println("testBasicOperator"); String before, after; MorseTree mt = new MorseTree(); for (int i = 0; i < itu_symbol.length; i++) { mt.insertSymbol(itu_symbol[i], itu_morse[i]); } TelegraphOperator operator = new TelegraphOperator(mt, itu_symbol, itu_morse); before = ".- -... -.-. -..- -.-- --.."; after = operator.receiveMessage(before); displayResults(after.equals("abcxyz")); before = "...-- .-.-.- .---- ....- .---- ..... ----."; after = operator.receiveMessage(before); displayResults(after.equals("3.14159")); before = "..-. --- --- ... -... .- .-.. .-.."; after = operator.receiveMessage(before); displayResults(after.equals("foos ball")); before = "canada"; after = operator.sendMessage(before); displayResults(after.equals("-.-. .- -. .- -.. .-")); before = "mr. dressup"; after = operator.sendMessage(before); displayResults(after.equals( "-- .-. .-.-.- -.. .-. . ... ... ..- .--." )); before = "'call me at 1:00'"; after = operator.sendMessage(before); displayResults(after.equals( ".----. -.-. .- .-.. .-.. -- . .- - .---- ---... -----" + " ----- .----." )); before = ".-.-.-.-.-.-.-.-"; after = operator.receiveMessage(before); displayResults(after.equals("")); before = "... --- ... ..........."; after = operator.receiveMessage(before); displayResults(after.equals("")); } public static void testPoemOperator() { System.out.println("testPoemOperator"); String before, after; MorseTree mt = new MorseTree(); for (int i = 0; i < itu_symbol.length; i++) { mt.insertSymbol(itu_symbol[i], itu_morse[i]); } TelegraphOperator operator = new TelegraphOperator(mt, itu_symbol, itu_morse); before = Sonnet18.morse; after = operator.receiveMessage(before); displayResults(after.equals(Sonnet18.text)); before = Sonnet18.text; after = operator.sendMessage(before); displayResults(after.equals(Sonnet18.morse)); } public static void main (String[] args) { try { testEmptyMorseTree(); testSimpleMorseTree(); testBasicMorseTree(); testBasicOperator(); testPoemOperator(); } catch (Exception e) { System.out.println("Your code threw an Exception."); System.out.println("Perhaps a stack trace will help:"); e.printStackTrace(System.out); } } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Understanding Oracle APEX 5 Application Development

Authors: Edward Sciore

2nd Edition

1484209893, 9781484209899

More Books

Students also viewed these Databases questions

Question

What challenges do you face in your current role?

Answered: 1 week ago

Question

How well is the company IT system able to support this initiative?

Answered: 1 week ago

Question

What training do they require?

Answered: 1 week ago