• Post Reply Bookmark Topic Watch Topic
  • New Topic

Morse Code to English  RSS feed

 
Saad Salman
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
need to convert morse code into English but without using arrays or maps
need help!!
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your question? Is the code failing to compile, is it not behaving the way you want it to etc?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Always use the code button; I have edited your code with it and doesn't it look better
As you have been told, you need to tell us what is going wrong otherwise we don't know what to suggest.
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saad Salman wrote:... need to convert morse code into English but without using arrays or maps


Why ?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:. . . Why ?
Because some teachers don't explain their requirements very well. The idea is to demonstrate that the students can write if‑elses. So you tell them not to use Maps, which would usually be regarded as poor programming.

I would have used a switch statement myself.
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I would have used a switch statement myself.

I would have used a HashMap
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:
Campbell Ritchie wrote:I would have used a switch statement myself.

I would have used a HashMap

The requirements specifically prohibit the use of Map implementations.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course a Map is the best solution. So would be a MorseCharacter class, but the OP is being tested on writing if elses.I used 131071 for hash because it is a Mersenne prime. The %0x20 bit means you get the same hash code for 'A' and for 'a'.
 
Stephan van Hulst
Saloon Keeper
Posts: 7984
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have used an enum.
 
Stephan van Hulst
Saloon Keeper
Posts: 7984
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I used 131071 for hash because it is a Mersenne prime. The %0x20 bit means you get the same hash code for 'A' and for 'a'.

My personal preference would be to store the letter in canonical form (for instance, in uppercase), and using Objects.hash(code, letter) to generate the hash code.
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I would have used an enum.

I am a bit curious about this in enums .. were you still going to use switch cases ?
I was thinking of its in lines of valueOf() method, but then his values were dots and dashes.

maybe like this:


 
Tim Holloway
Saloon Keeper
Posts: 18793
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if I build a trinary tree whose branches are "dot", "dash" and "terminate", does that count as a map?

This, BTW is why I flunk employment programming aptitude tests. It's never one of the "right" answers.
 
Stephan van Hulst
Saloon Keeper
Posts: 7984
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the assignment is to not use any arrays at all, then yes, I would work with a switch statement. If not, then the values() method can be used. The general outline would be something like this:
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:So if I build a trinary tree whose branches are "dot", "dash" and "terminate", does that count as a map? . . .
Surely that is a Huffman code
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, Stephan, you are right about Objects#hash.
I don't think there is any chance of a null String getting into my class, is there?
 
Tim Holloway
Saloon Keeper
Posts: 18793
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finite State Machine? I've used Antlr since it was still known as the Purdue Compiler Construction Toolset.

I know, I know I don't get the job.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saad.

Please don't ask Java questions in Private Messages. Post the question in the main thread so everyone can see and contribute.

For the benefit of others here, Saad would like to know why the code works for single characters but not for multiple characters.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason the code only works for single characters is because you are comparing the entire input String against the code for each character. If the string does not exactly match the code for a single character only then it won't do anything.

You are going to have to find an approach to take your string and see if it starts with the code for a character. You will then need to discard the part of the input used and repeat for the next character.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and your code for A appears to be the same as your code for J. I think you have made an error somewhere.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!