Forums Register Login

Morse Code converter

+Pie Number of slices to send: Send
Good morning all!

I did an exercise on a morse code converter with the following requirements:

Perhaps the most famous of all coding schemes is the Morse code, developed by Samuel Morse in 1832 for use with the telegraph system. The Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few special characters (e.g., period, comma, colon, semicolon). In sound-oriented systems, the dot represents a short sound and the dash a long sound. Other representations of dots and dashes are used with light-oriented systems and signal-flag systems. Separation between words is indicated by a space or, simply, the absence of a dot or dash. In a sound-oriented system, a space is indicated by a short time during which no sound is transmitted. Is to be used the international version of the Morse code (only characters and digits).
Write an application that reads an English-language phrase and encodes it into Morse code. Also write an application that reads a phrase in Morse code and converts it into the English-language equivalent. Use one blank between each Morse-coded letter and three blanks between each Morse-coded word.


The classes i coded, MorseConverter and TestMorseConverter, assume so far that the input strings are correct (only letters, digits and spaces as the requirements say), and are as follows:



What do you think about them?

Thanks

Best regards
Carlos
+Pie Number of slices to send: Send
 

What do you think about them?


There aren't any comments describing what the code is doing and how it is doing it.

What are the hardcoded magic numbers?  48 and 55?
+Pie Number of slices to send: Send
Sorry... Just added the comments.

I know that there are better ways to do this (with ArrayLists for example), but wanted to start with plain arrays first...
+Pie Number of slices to send: Send
Why do you think a List is better than an array? I am not convinced that follows.
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:Why do you think a List is better than an array? I am not convinced that follows.


Well i thought that the indexOf() method of the ArrayList would provide a simpler way of finding the index of the morse simbol int the morseToEnglish() method for example.
+Pie Number of slices to send: Send
No need to comment
+Pie Number of slices to send: Send
 

Norm Radder wrote:An explanation for the magic numbers: 48 and 55 would be useful.
Could the 48 be '0'?
Is the 55  'A' - 10?
char values are numeric and can be used directly without using their int values.

The explanation is in the comments!
But basically it's the difference between the ASCII code of the characters and their corresponding index in the array, so:

ASCII of '0' = 48
Index of 1st digit in array = 0
Diff = 48 - 0 = 48

ASCII of 'A' = 65
Index of 1st letter in array = 10
Diff = 65 - 10 = 55

+Pie Number of slices to send: Send
No need for comments
+Pie Number of slices to send: Send
 

Norm Radder wrote:

What do you think about them?


There aren't any comments describing what the code is doing and how it is doing it.
What are the hardcoded magic numbers?  48 and 55?


Carlos Reves wrote:Sorry... Just added the comments.


Don't use comments as a substitute for readable and expressive code. That's a smell.  If you have to add comments to explain what the code is doing, then your code needs to be refactored for clarity.

Part of the problem is that you're using a single array to hold different types of codes.  My solution (somebody posted the same problem a while ago) used one array for digits and another one for letters.  This allows you to use integer arithmetic to figure out the index of the corresponding code since char is an integer type.  That is, '9' - '0' will give you 9, which is the index of '9' in {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}.  Similarly, 'Z' - 'A' is 26 25, which is the index of 'Z' in an array of letters {'A', 'B', ..., 'Z'}

If you use this scheme, you can eliminate the unintuitive 48 and 55 magic numbers.

Also, there's just too much code in either of those methods.  It's not readable at all.
+Pie Number of slices to send: Send
Note: don't let my example array lead you to think that's what I had in my solution.  You have to figure out what the appropriate content of the array should be if you're going to translate a char to its equivalent Morse code.
+Pie Number of slices to send: Send
Thanks guys for the inputs.

back to the drawing board then.

Best regards
Carlos
+Pie Number of slices to send: Send
 

Carlos Reves wrote:. . . ASCII of '0' = 48
Index of 1st digit in array = 0
Diff = 48 - 0 = 48

ASCII of 'A' = 65
Index of 1st letter in array = 10
Diff = 65 - 10 = 55

But '0' is not 0. It is 48. A char is a number and you can do arithmetic with it. You can write n − '0' and there will be no need for the literal 48. The same applies to 'A'. By using 'A' and 'a' you can enhance your application to support uppercase and lowercase writing. Or even use n % 0x20. You may find it is easier to use hex numbers rather than decimal.

Junilu has already told you much of that.
+Pie Number of slices to send: Send
My advice: don't use arrays that need index juggling. Simply use: String[] morseCodes = new String[256], (or small enough), and get the morse code by: morseCode = morseCodes(char).
If you think that is wasting too much memory, then use a HashMap (or two).
+Pie Number of slices to send: Send
I've done some changes. Created a new class for Morse Symbols. Here are my changes (the Test class didn't change so won't put it here).

No magic numbers allready...

Best regards
Carlos
+Pie Number of slices to send: Send
 

Piet Souris wrote:My advice: don't use arrays that need index juggling. Simply use: String[] morseCodes = new String[256], (or small enough), and get the morse code by: morseCode = morseCodes(char).
If you think that is wasting too much memory, then use a HashMap (or two).


That's a reasonable alternative although you'd still need to juggle index values to initialize the elements to their corresponding codes. The lookup will be straightforward though.  You could also use a proper Map<Character, String>.
+Pie Number of slices to send: Send
 

Junilu Lacar wrote:You could also use a proper Map<Character, String>.


i didn't get to HashMaps in the book yet... Just Arrays and ArrayLists. So i didn't want to use them.
+Pie Number of slices to send: Send
Just realized that i don't need static methods in MorseSymbols class...
1
+Pie Number of slices to send: Send
Not bad but what happens when you search for a char that's not in the englishCharacters array?
+Pie Number of slices to send: Send
 

Junilu Lacar wrote:Not bad but what happens when you search for a char that's not in the englishCharacters array?


@OP similar problem appears with moreSymbols, but there you'd suffer from exception.
+Pie Number of slices to send: Send
As I said in my first post this version assumes that input strings are correct. The verification for correctness will be the next stage...
+Pie Number of slices to send: Send
 

Liutauras Vilda wrote:@OP similar problem appears with moreSymbols, but there you'd suffer from exception.

moreSymbols? Didn't get what you mean...
+Pie Number of slices to send: Send
 

Carlos Reves wrote:

Liutauras Vilda wrote:@OP similar problem appears with moreSymbols, but there you'd suffer from exception.

moreSymbols? Didn't get what you mean...

I meant morseSymbols, sorry for typo.

I meant that after the binary search you could get any value less than 0 (in case of not found character), so you'd end up accessing morseSymbols[negativeIndex], which would cause you an exception to be thrown.

But you explained already why you didn't handle those cases.
+Pie Number of slices to send: Send
Now you may wonder why is there could be not just -1, but rather any negative number in case of lack of searchable number. That is because of binary search, it actually defines position of your searchable number where it actually suppose to be in this sequence, then makes this position number (0-indexed) a negative and adds -1.

So, in case of:

you get i = -5, because -2 supposed to be after -3, at position 4, then this number becomes -4 and then adding -1 which makes -5.

A bit off topic actually.
Screaming fools! It's nothing more than a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 5974 times.
Similar Threads
morse code converters
converting morse code to english
Why doesn't this program translate morse code correctly?
MorseCode Translation: decoding incorrectly
What does this say?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:20:52.