• Post Reply Bookmark Topic Watch Topic
  • New Topic

Morse Code and Binary Trees  RSS feed

 
Laura Peterson
Greenhorn
Posts: 23
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings, fellow Java programmers! I have an assignment (yes, a homework one, but I actually DID it, my class TA is awol and I have no one to ask questions) that I have two errors in that I can't seem to correct. I am also including the input file, the sample output file, and the assignment outline (sorry for such a long post- can anyone tell me what file formats are accepted for attachments? It seems like everything I try doesn't work). My problems are with the inOrderPrint/printInorder (lines 69 & 78, somehow the output isn't spacing correctly) and the searchTree (line 147, when I go through the debugger everything looks fine, but it doesn't terminate properly) methods. Any help and advice as far as fixing those bugs for the correct output would be welcomed. Thanks so much for your time!

Here is my code:


Here is the interface code:


The Node class code:


And the test class code:






And the description:

This assignment will give you practice with trees. You are to write a class MorseCoder that allows you to encode and decode Morse code.
The Morse code (see Table 6.10) is a common code that is used to encode messages consisting of letters and digits. Each letter consists of a series of dots and dashes; for example, the code for the letter a is •– and the code for the letter b is –•••. Store each letter of the alphabet in a node of a binary tree of level 5. The root node is at level 1 and stores no letter. The left node at level 2 stores the letter e (code is •), and the right node stores the letter t (code is –). The 4 nodes at level 3 store the letters with codes (••, •–, –•, ––). To build the tree (see Figure below), read a file in which each line consists of a letter followed by its code. The letters should be ordered by tree level. To find the position for a letter in the tree, scan the code and branch left for a dot and branch right for a dash. Encode a message by replacing each letter by its code symbol. Then decode the message using the Morse code tree.
Your class must implement the MorseCodeInterface provided and must contain the methods specified. You may need to provide a constructor to build the tree from the input file, encodings.txt. You may create private methods to implement the recursion for the different methods.
You are allowed to declare whatever data fields you want for your class, but you should try to keep these to a minimum. You can get by with just one data field (possibly root) and you shouldn’t need much more. If you have unnecessary data fields (data fields that could be turned into local variables), you will lose style points.
In terms of correctness, your class must provide all of the functionality described above. In terms of style, we will be grading on your use of comments, good variable names, consistent indentation and good coding style to implement these operations.
You should name your file MorseCoder.java and you should turn it in electronically from the “assignments” link on the class web page. You will need to have MorseCodeInterface.java, MorseNode.java, MorseMain.java and encodings.txt (In the same level as src) all in the same directory as your MorseCoder.java in order to run MorseMain. A sample output file, output.txt file is also attached.
You may work on this assignment in pairs.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is wrong with the spacing?
 
Laura Peterson
Greenhorn
Posts: 23
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My output does not have the same spacing that the sample output does.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain more: what you do get and what do you want?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laura Peterson wrote:Any help and advice as far as fixing those bugs for the correct output would be welcomed.

I don't know about helping you to fix the bugs, but I have a few tips about rationalisation:

Basically, you have an incredible amount of code, which suggests to me that you're tackling this procedurally rather than analytically. You're also using substrings when you really don't need to. Your MorseNode class is actually very good; it's the MorseCoder class where you seem to have lapsed into "let's force out a solution by coding" mode.

So, start by breaking down each process and working our WHAT you need to do, not HOW you're going to code it (see the WhatNotHow page for more details).

Take adding a letter to your tree, for example:
1. You will be given an ASCII letter and its associated Morse code string.
2. Start at the root node
3. For each character in the String:
4. If it's a dot, go left. If no left Node exists, add it so that you can.
5. If it's a dash, go right. If no right Node exists, add it so that you can.
6. If it isn't either, throw an Exception ("bad Morse string"?). This is a very important thing to remember: anyone can write code that works when things are OK; good programmers write code that works when they aren't.
7. If there are more characters, go back to Step 3.
8. Set the character for the final Node you arrived at to the ASCII character you were supplied.
Notice that in describing the process I haven't used ANY Java.

Now, your code is pretty much doing that, but it's rather verbose. You could also break it down a bit more. How about this:Do you see how it's quite a bit simpler? We've used our analysis to write the code; not code to get the solution.
Furthermore, we now have a method (nextNode()) that we can use when searching as well as adding.

My suggestion: Try going through that sort of exercise for all the major methods in your MorseCoder class. I suspect you'll discover that you have other redundancies, and just going through the process may well show you where your current problem is.

HIH

Winston
 
Laura Peterson
Greenhorn
Posts: 23
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the new thought process. I think I'm understanding a little better now. Thank you!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laura Peterson wrote:Thanks for the new thought process. I think I'm understanding a little better now. Thank you!

You're most welcome.

I should add that the above is only one way to do it, but the main thing is to think before you code.
Best of luck with your project.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laura Peterson wrote:Any help and advice ... would be welcomed.

One other thing occurred to me; and it's a bit more subtle.

MorseNode.setLetter() simply sets the letter to whatever it was given. IMO, it needs to do a bit more than just that.

If you're setting it for a second time, it should probably throw an error, because the likelihood is that you're attempting to change the key of an existing Node, which probably indicates duplicate or invalid information. So, how about this:Again, it's not the only way to do it, but it might be worth thinking about.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!