• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Morse Code converter

 
Ranch Hand
Posts: 116
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 116
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you think a List is better than an array? I am not convinced that follows.
 
Carlos Reves
Ranch Hand
Posts: 116
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No need to comment
 
Carlos Reves
Ranch Hand
Posts: 116
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No need for comments
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Carlos Reves
Ranch Hand
Posts: 116
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys for the inputs.

back to the drawing board then.

Best regards
Carlos
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Carlos Reves
Ranch Hand
Posts: 116
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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>.
 
Carlos Reves
Ranch Hand
Posts: 116
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Carlos Reves
Ranch Hand
Posts: 116
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just realized that i don't need static methods in MorseSymbols class...
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not bad but what happens when you search for a char that's not in the englishCharacters array?
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Carlos Reves
Ranch Hand
Posts: 116
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I said in my first post this version assumes that input strings are correct. The verification for correctness will be the next stage...
 
Carlos Reves
Ranch Hand
Posts: 116
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

moreSymbols? Didn't get what you mean...
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic