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.
What do you think about them?
Campbell Ritchie wrote:Why do you think a List is better than an array? I am not convinced that follows.
The explanation is in the comments!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.
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.
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.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
There are three kinds of actuaries: those who can count, and those who can't.
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).
Junilu Lacar wrote:You could also use a proper Map<Character, String>.
Junilu Lacar wrote:Not bad but what happens when you search for a char that's not in the englishCharacters array?
moreSymbols? Didn't get what you mean...Liutauras Vilda wrote:@OP similar problem appears with moreSymbols, but there you'd suffer from exception.
I meant morseSymbols, sorry for typo.Carlos Reves wrote:
moreSymbols? Didn't get what you mean...Liutauras Vilda wrote:@OP similar problem appears with moreSymbols, but there you'd suffer from exception.
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
|