• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Morse Code and Binary Trees

 
Greenhorn
Posts: 23
Eclipse IDE Chrome 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.

 
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is wrong with the spacing?
 
Laura Peterson
Greenhorn
Posts: 23
Eclipse IDE Chrome 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: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain more: what you do get and what do you want?
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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
Eclipse IDE Chrome 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: 10777
71
Hibernate Eclipse IDE 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: 10777
71
Hibernate Eclipse IDE 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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I have a question regarding this code. For now, it's printing the letters only from the txt file. How to print it along with the morse code as well. Any help will mean alot. Thank you
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please show us your code and explain the problem a bit more. I don't think you will get an answer from OP who hasn't been active here for over six years.
 
Sashneeta Subahar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is my interface:



this is MorseCoder class:


this is my main method:


and this is morse node class:


my txt file:

output:


So basically, I want my output like:
5 .....     H ....   4 ....-     S ...       V ...-
(something like this for the whole output)
where the alphabet is printed along with its morse code. and only five are printed in one row.

Someone please help. Would be so grateful. Please show me your coding so that I understand better. I'm a newbie. Thanks
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where does that code come from? Why does it look so similar to Laura Peterson's, even with the same mistakes repeated? Are you on the same course as her?
I would probably not now design the node like that; I would make the letter final and I would give it a morseCode field. Once you add that field it becomes very easy to print S ... because the Node or some MorseCode object will encapsulate both data.
I would probably also make MorseCode implement Comparable<MorseCode>, or write a Comparator. You should be able to make those consistent with equals(), and you can create a tree around that.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Where does that code come from? Why does it look so similar to Laura Peterson's, even with the same mistakes repeated? Are you on the same course as her?
I would probably not now design the node like that; I would make the letter final and I would give it a morseCode field. Once you add that field it becomes very easy to print S ... because the Node or some MorseCode object will encapsulate both data.
I would probably also make MorseCode implement Comparable<MorseCode>, or write a Comparator. You should be able to make those consistent with equals(), and you can create a tree around that.



I saw this when it came up again. I tried the proposed encode methods, which did not work. I decided to give it a try with some help. I started with your suggestion making morse code a field. This made the solution much easier. I then took on trying to solve it without the field, which I was able to accomplish. This is my solution using morse code field:



The without morse code field is a little more complicated, but both have a common problem. How to stop recursion once letter node is found. "quit" is my solution. There must be something better?
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for delay replying, PR. That code looks very complicated, and I am not sure why you are calling something a recursive return. That method ,ooks half like recursion and half like iteration.
You shouldn't have a ny problems with the value part of your tree node being null because you shouldn't let any nulls in there in the first place. You should also never have any problems finding a Morse Code because your users should only ever enter a valid dot‑dash combination. So if you fail to find the corresponding letter, I think you should throw an exception.You don't want to search in order. You want to search recursively like a binary search. Remember that Strings are implicitly Comparable to one another. So your dots and your dashes will all sort differently. Assuming that you have a correctly populated tree containing only valid Morse Code sorted small=left, large=right, you want a recursive method like this:-I hereby challenge you to use the ?: operator to obviate the duplicated code in lines 13 and 21.
 
Peter Ream
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I Know this is an old thread and without reviewing all the specs can be confusing and even more so when you don't follow the spec. This is actually the encode method with a twist (your suggestion to add a morse code field for each letter). The code above is for encoding each letter (ie a -> .-). I left out the method that receives all the letters and calls the code above to encode each letter which would have probably made this clearer:



I did follow the spec and encode without the twist, but didn't want to show my solution since neither of the posters provided a working method. This code, like the speced encode code, have the common problem of getting out of a recursive method. I chose to ask with this one, as not to give too much away.

I am still reviewing your remarks. What I tagged as "// Recursive return" were returns not actually returning a result, but justing going deeper into the tree.
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Peter Ream wrote:I Know this is an old thread . . .  neither of the posters provided a working method. . . . .

I don't think you need worry about that after six years.

What I tagged as "// Recursive return" were returns not actually returning a result, but justing going deeper into the tree.

I shall have to study your code before I understand that bit, but I haven't got the time to do it now.
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF
htttp://www.e-iceblue.com/free-apis.html
    Bookmark Topic Watch Topic
  • New Topic