SlideShare a Scribd company logo
1 of 12
Download to read offline
In Java Write a program that reads an English language phrase and encodes the phrase into
Morse code or reads in a phrase in Morse code and converts the phrase into English. Use one
space between each Morse-code letter and three spaces between each Morse-code word.
Character
Code
Character
Code
A
.-
T
-
B
-…
U
..-
C
-.-.
V
…-
D
-..
W
.--
E
.
X
-..-
F
..-.
Y
-.--
G
--.
Z
--..
H
….
I
..
DIGITS
J
.---
1
.----
K
-.-
2
..---
L
.-..
3
…--
M
--
4
….-
N
-.
5
…..
O
---
6
-….
P
.--.
7
--…
Q
--.-
8
---..
R
.-.
9
----.
S
…
0
-----
Character
Code
Character
Code
A
.-
T
-
B
-…
U
..-
C
-.-.
V
…-
D
-..
W
.--
E
.
X
-..-
F
..-.
Y
-.--
G
--.
Z
--..
H
….
I
..
DIGITS
J
.---
1
.----
K
-.-
2
..---
L
.-..
3
…--
M
--
4
….-
N
-.
5
…..
O
---
6
-….
P
.--.
7
--…
Q
--.-
8
---..
R
.-.
9
----.
S
…
0
-----
Solution
Java Program:
import java.util.Scanner;
public class MorseCode
{
public static void main ( String [] args )
{
Scanner input = new Scanner( System.in );
System.out.print( "Enter 'Y' if you want convert from english to morse or enter 'N' to convert
from morse to english : " );
String answer = input.nextLine();
if( answer.equals( "Y" ) )
{
System.out.println( "Enter the english text: " );
String english = input.nextLine();
System.out.println( englishToMorse( english ) );
}
if (answer.equalsIgnoreCase( "N" ) )
{
System.out.print( "Morse to English? " );
String answer2 = input.nextLine();
if (answer2.equalsIgnoreCase( "Y" ) )
{
System.out.println( "Enter morse code : " );
String code = input.nextLine();
System.out.println( MorseToEnglish( code ) );
}
}
}
public static String encoder (String toEncode)
{
String morse = toEncode;
if (toEncode.equalsIgnoreCase("a"))
morse = ".-";
if (toEncode.equalsIgnoreCase("b"))
morse = "-...";
if (toEncode.equalsIgnoreCase("c"))
morse = "-.-.";
if (toEncode.equalsIgnoreCase("d"))
morse = "-..";
if (toEncode.equalsIgnoreCase("e"))
morse = ".";
if (toEncode.equalsIgnoreCase("f"))
morse = "..-.";
if (toEncode.equalsIgnoreCase("g"))
morse = "--.";
if (toEncode.equalsIgnoreCase("h"))
morse = "....";
if (toEncode.equalsIgnoreCase("i"))
morse = "..";
if (toEncode.equalsIgnoreCase("j"))
morse = ".---";
if (toEncode.equalsIgnoreCase("k"))
morse = "-.-";
if (toEncode.equalsIgnoreCase("l"))
morse = ".-..";
if (toEncode.equalsIgnoreCase("m"))
morse = "--";
if (toEncode.equalsIgnoreCase("n"))
morse = "-.";
if (toEncode.equalsIgnoreCase("o"))
morse = "---";
if (toEncode.equalsIgnoreCase("p"))
morse = ".--.";
if (toEncode.equalsIgnoreCase("q"))
morse = "--.-";
if (toEncode.equalsIgnoreCase("r"))
morse = ".-.";
if (toEncode.equalsIgnoreCase("s"))
morse = "...";
if (toEncode.equalsIgnoreCase("t"))
morse = "-";
if (toEncode.equalsIgnoreCase("u"))
morse = "..-";
if (toEncode.equalsIgnoreCase("v"))
morse = "...-";
if (toEncode.equalsIgnoreCase("w"))
morse = ".--";
if (toEncode.equalsIgnoreCase("x"))
morse = "-..-";
if (toEncode.equalsIgnoreCase("y"))
morse = "-.--";
if (toEncode.equalsIgnoreCase("z"))
morse = "--..";
if (toEncode.equalsIgnoreCase("0"))
morse = "-----";
if (toEncode.equalsIgnoreCase("1"))
morse = ".----";
if (toEncode.equalsIgnoreCase("2"))
morse = "..---";
if (toEncode.equalsIgnoreCase("3"))
morse = "...--";
if (toEncode.equalsIgnoreCase("4"))
morse = "....-";
if (toEncode.equalsIgnoreCase("5"))
morse = ".....";
if (toEncode.equalsIgnoreCase("6"))
morse = "-....";
if (toEncode.equalsIgnoreCase("7"))
morse = "--...";
if (toEncode.equalsIgnoreCase("8"))
morse = "---..";
if (toEncode.equalsIgnoreCase("9"))
morse = "----.";
if (toEncode.equalsIgnoreCase("."))
morse = ".-.-";
if (toEncode.equalsIgnoreCase(","))
morse = "--..--";
if (toEncode.equalsIgnoreCase("?"))
morse = "..--..";
return morse;
}
public static String decoder (String toEncode) {
String morse = toEncode;
if (toEncode.equalsIgnoreCase(".- "))
morse = "a";
if (toEncode.equalsIgnoreCase("-..."))
morse = "b";
if (toEncode.equalsIgnoreCase("-.-."))
morse = "c";
if (toEncode.equalsIgnoreCase("-.."))
morse = "d";
if (toEncode.equalsIgnoreCase("."))
morse = "e";
if (toEncode.equalsIgnoreCase("..-."))
morse = "f";
if (toEncode.equalsIgnoreCase("--."))
morse = "g";
if (toEncode.equalsIgnoreCase("...."))
morse = "h";
if (toEncode.equalsIgnoreCase(".."))
morse = "i";
if (toEncode.equalsIgnoreCase(".---"))
morse = "j";
if (toEncode.equalsIgnoreCase("-.-"))
morse = "k";
if (toEncode.equalsIgnoreCase(".-.."))
morse = "l";
if (toEncode.equalsIgnoreCase("--"))
morse = "m";
if (toEncode.equalsIgnoreCase("-."))
morse = "n";
if (toEncode.equalsIgnoreCase("---"))
morse = "o";
if (toEncode.equalsIgnoreCase(".--."))
morse = "p";
if (toEncode.equalsIgnoreCase("--.-"))
morse = "q";
if (toEncode.equalsIgnoreCase(".-."))
morse = "r";
if (toEncode.equalsIgnoreCase("..."))
morse = "s";
if (toEncode.equalsIgnoreCase("-"))
morse = "t";
if (toEncode.equalsIgnoreCase("..-"))
morse = "u";
if (toEncode.equalsIgnoreCase("...-"))
morse = "v";
if (toEncode.equalsIgnoreCase(".--"))
morse = "w";
if (toEncode.equalsIgnoreCase("-..-"))
morse = "x";
if (toEncode.equalsIgnoreCase("-.--"))
morse = "y";
if (toEncode.equalsIgnoreCase("--.."))
morse = "z";
if (toEncode.equalsIgnoreCase("-----"))
morse = "0";
if (toEncode.equalsIgnoreCase(".----"))
morse = "1";
if (toEncode.equalsIgnoreCase("..---"))
morse = "2";
if (toEncode.equalsIgnoreCase("...--"))
morse = "3";
if (toEncode.equalsIgnoreCase("....-"))
morse = "4";
if (toEncode.equalsIgnoreCase("....."))
morse = "5";
if (toEncode.equalsIgnoreCase("-...."))
morse = "6";
if (toEncode.equalsIgnoreCase("--..."))
morse = "7";
if (toEncode.equalsIgnoreCase("---.."))
morse = "8";
if (toEncode.equalsIgnoreCase("----."))
morse = "9";
if (toEncode.equalsIgnoreCase(" "))
morse = "";
return morse;
}
public static String englishToMorse( String text )
{
String Text = "";
String SChar;
String CChar;
for (int i = 0; i < text.length(); i++)
{
SChar = text.charAt(i) + "";
CChar = encoder(SChar);
if (CChar.equals(" "))
{
Text = Text + "| ";
}
else
{
Text = Text + CChar;
if (!CChar.equals(" "))
{
Text = Text + " ";
}
}
}
return Text;
}
public static String MorseToEnglish( String text )
{
return null;
}
}
Output:
Enter 'Y' if you want convert from english to morse or enter 'N' to convert from morse to
english : Y
Enter the english text:
MorseCode20
-- --- .-. ... . -.-. --- -.. . ..--- -----

More Related Content

More from mohammedfootwear

5. value 10.00 polnts On May 1, Soriano Co. reported the following ac.pdf
5. value 10.00 polnts On May 1, Soriano Co. reported the following ac.pdf5. value 10.00 polnts On May 1, Soriano Co. reported the following ac.pdf
5. value 10.00 polnts On May 1, Soriano Co. reported the following ac.pdf
mohammedfootwear
 
C programming. Answer question only in C code In the eighth part, yo.pdf
C programming. Answer question only in C code In the eighth part, yo.pdfC programming. Answer question only in C code In the eighth part, yo.pdf
C programming. Answer question only in C code In the eighth part, yo.pdf
mohammedfootwear
 
You will choose a country, other than the USA or Turkey, that you wo.pdf
You will choose a country, other than the USA or Turkey, that you wo.pdfYou will choose a country, other than the USA or Turkey, that you wo.pdf
You will choose a country, other than the USA or Turkey, that you wo.pdf
mohammedfootwear
 
What is the correct answer Chrome File Edit View History Bookmarks.pdf
What is the correct answer Chrome File Edit View History Bookmarks.pdfWhat is the correct answer Chrome File Edit View History Bookmarks.pdf
What is the correct answer Chrome File Edit View History Bookmarks.pdf
mohammedfootwear
 
Using SQL Developer ONLY!Your assignment is to create an auditing .pdf
Using SQL Developer ONLY!Your assignment is to create an auditing .pdfUsing SQL Developer ONLY!Your assignment is to create an auditing .pdf
Using SQL Developer ONLY!Your assignment is to create an auditing .pdf
mohammedfootwear
 
This for English class.I need help for writing 4 to 5 paragraphs a.pdf
This for English class.I need help for writing 4 to 5 paragraphs a.pdfThis for English class.I need help for writing 4 to 5 paragraphs a.pdf
This for English class.I need help for writing 4 to 5 paragraphs a.pdf
mohammedfootwear
 
The atmosphere in a Trident nuclear submarine is generally calm and .pdf
The atmosphere in a Trident nuclear submarine is generally calm and .pdfThe atmosphere in a Trident nuclear submarine is generally calm and .pdf
The atmosphere in a Trident nuclear submarine is generally calm and .pdf
mohammedfootwear
 
Please .pdf
Please .pdfPlease .pdf
Please .pdf
mohammedfootwear
 
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdfPLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
mohammedfootwear
 

More from mohammedfootwear (20)

5. value 10.00 polnts On May 1, Soriano Co. reported the following ac.pdf
5. value 10.00 polnts On May 1, Soriano Co. reported the following ac.pdf5. value 10.00 polnts On May 1, Soriano Co. reported the following ac.pdf
5. value 10.00 polnts On May 1, Soriano Co. reported the following ac.pdf
 
C programming. Answer question only in C code In the eighth part, yo.pdf
C programming. Answer question only in C code In the eighth part, yo.pdfC programming. Answer question only in C code In the eighth part, yo.pdf
C programming. Answer question only in C code In the eighth part, yo.pdf
 
Biology Lab questions1. Why can a very small amount of bacteria b.pdf
Biology Lab questions1. Why can a very small amount of bacteria b.pdfBiology Lab questions1. Why can a very small amount of bacteria b.pdf
Biology Lab questions1. Why can a very small amount of bacteria b.pdf
 
You will choose a country, other than the USA or Turkey, that you wo.pdf
You will choose a country, other than the USA or Turkey, that you wo.pdfYou will choose a country, other than the USA or Turkey, that you wo.pdf
You will choose a country, other than the USA or Turkey, that you wo.pdf
 
Why does the RNA polymerase complex in eukaryotes contain a histone .pdf
Why does the RNA polymerase complex in eukaryotes contain a histone .pdfWhy does the RNA polymerase complex in eukaryotes contain a histone .pdf
Why does the RNA polymerase complex in eukaryotes contain a histone .pdf
 
what Linux command allows you to scan the disk for partition changes.pdf
what Linux command allows you to scan the disk for partition changes.pdfwhat Linux command allows you to scan the disk for partition changes.pdf
what Linux command allows you to scan the disk for partition changes.pdf
 
What is the correct answer Chrome File Edit View History Bookmarks.pdf
What is the correct answer Chrome File Edit View History Bookmarks.pdfWhat is the correct answer Chrome File Edit View History Bookmarks.pdf
What is the correct answer Chrome File Edit View History Bookmarks.pdf
 
What is fault tolerance, and what network aspects must be monitored .pdf
What is fault tolerance, and what network aspects must be monitored .pdfWhat is fault tolerance, and what network aspects must be monitored .pdf
What is fault tolerance, and what network aspects must be monitored .pdf
 
Using SQL Developer ONLY!Your assignment is to create an auditing .pdf
Using SQL Developer ONLY!Your assignment is to create an auditing .pdfUsing SQL Developer ONLY!Your assignment is to create an auditing .pdf
Using SQL Developer ONLY!Your assignment is to create an auditing .pdf
 
Use the definition of big- Theta to prove that 5x^4 + 2x^3 - 1 is The.pdf
Use the definition of big- Theta to prove that 5x^4 + 2x^3 - 1 is The.pdfUse the definition of big- Theta to prove that 5x^4 + 2x^3 - 1 is The.pdf
Use the definition of big- Theta to prove that 5x^4 + 2x^3 - 1 is The.pdf
 
To what degree, if any, has America’s ascendancy on the world stage .pdf
To what degree, if any, has America’s ascendancy on the world stage .pdfTo what degree, if any, has America’s ascendancy on the world stage .pdf
To what degree, if any, has America’s ascendancy on the world stage .pdf
 
This for English class.I need help for writing 4 to 5 paragraphs a.pdf
This for English class.I need help for writing 4 to 5 paragraphs a.pdfThis for English class.I need help for writing 4 to 5 paragraphs a.pdf
This for English class.I need help for writing 4 to 5 paragraphs a.pdf
 
The debt ratio is the relationship between O A. total assets and cur.pdf
The debt ratio is the relationship between O A. total assets and cur.pdfThe debt ratio is the relationship between O A. total assets and cur.pdf
The debt ratio is the relationship between O A. total assets and cur.pdf
 
The atmosphere in a Trident nuclear submarine is generally calm and .pdf
The atmosphere in a Trident nuclear submarine is generally calm and .pdfThe atmosphere in a Trident nuclear submarine is generally calm and .pdf
The atmosphere in a Trident nuclear submarine is generally calm and .pdf
 
Question A woman has a particular disease and all of her children (.pdf
Question A woman has a particular disease and all of her children (.pdfQuestion A woman has a particular disease and all of her children (.pdf
Question A woman has a particular disease and all of her children (.pdf
 
Question 50 (1 point) occurs when savers money makes its way into the.pdf
Question 50 (1 point) occurs when savers money makes its way into the.pdfQuestion 50 (1 point) occurs when savers money makes its way into the.pdf
Question 50 (1 point) occurs when savers money makes its way into the.pdf
 
Name the Three Kings of the Chicago-Memphis Electric Blues in ch.pdf
Name the Three Kings of the Chicago-Memphis Electric Blues in ch.pdfName the Three Kings of the Chicago-Memphis Electric Blues in ch.pdf
Name the Three Kings of the Chicago-Memphis Electric Blues in ch.pdf
 
Please .pdf
Please .pdfPlease .pdf
Please .pdf
 
number 4 1.4 Ordered Pairs The elements of be able to speak about.pdf
number 4 1.4 Ordered Pairs The elements of be able to speak about.pdfnumber 4 1.4 Ordered Pairs The elements of be able to speak about.pdf
number 4 1.4 Ordered Pairs The elements of be able to speak about.pdf
 
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdfPLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 

In Java Write a program that reads an English language phrase and en.pdf

  • 1. In Java Write a program that reads an English language phrase and encodes the phrase into Morse code or reads in a phrase in Morse code and converts the phrase into English. Use one space between each Morse-code letter and three spaces between each Morse-code word. Character Code Character Code A .- T - B -… U ..- C -.-. V …- D -.. W .-- E . X -..- F ..-. Y -.-- G --. Z --..
  • 5. 7 --… Q --.- 8 ---.. R .-. 9 ----. S … 0 ----- Solution Java Program: import java.util.Scanner; public class MorseCode { public static void main ( String [] args ) { Scanner input = new Scanner( System.in ); System.out.print( "Enter 'Y' if you want convert from english to morse or enter 'N' to convert from morse to english : " ); String answer = input.nextLine(); if( answer.equals( "Y" ) ) { System.out.println( "Enter the english text: " ); String english = input.nextLine(); System.out.println( englishToMorse( english ) ); } if (answer.equalsIgnoreCase( "N" ) )
  • 6. { System.out.print( "Morse to English? " ); String answer2 = input.nextLine(); if (answer2.equalsIgnoreCase( "Y" ) ) { System.out.println( "Enter morse code : " ); String code = input.nextLine(); System.out.println( MorseToEnglish( code ) ); } } } public static String encoder (String toEncode) { String morse = toEncode; if (toEncode.equalsIgnoreCase("a")) morse = ".-"; if (toEncode.equalsIgnoreCase("b")) morse = "-..."; if (toEncode.equalsIgnoreCase("c")) morse = "-.-."; if (toEncode.equalsIgnoreCase("d")) morse = "-.."; if (toEncode.equalsIgnoreCase("e")) morse = "."; if (toEncode.equalsIgnoreCase("f")) morse = "..-."; if (toEncode.equalsIgnoreCase("g")) morse = "--."; if (toEncode.equalsIgnoreCase("h")) morse = "...."; if (toEncode.equalsIgnoreCase("i")) morse = ".."; if (toEncode.equalsIgnoreCase("j")) morse = ".---";
  • 7. if (toEncode.equalsIgnoreCase("k")) morse = "-.-"; if (toEncode.equalsIgnoreCase("l")) morse = ".-.."; if (toEncode.equalsIgnoreCase("m")) morse = "--"; if (toEncode.equalsIgnoreCase("n")) morse = "-."; if (toEncode.equalsIgnoreCase("o")) morse = "---"; if (toEncode.equalsIgnoreCase("p")) morse = ".--."; if (toEncode.equalsIgnoreCase("q")) morse = "--.-"; if (toEncode.equalsIgnoreCase("r")) morse = ".-."; if (toEncode.equalsIgnoreCase("s")) morse = "..."; if (toEncode.equalsIgnoreCase("t")) morse = "-"; if (toEncode.equalsIgnoreCase("u")) morse = "..-"; if (toEncode.equalsIgnoreCase("v")) morse = "...-"; if (toEncode.equalsIgnoreCase("w")) morse = ".--"; if (toEncode.equalsIgnoreCase("x")) morse = "-..-"; if (toEncode.equalsIgnoreCase("y")) morse = "-.--"; if (toEncode.equalsIgnoreCase("z")) morse = "--.."; if (toEncode.equalsIgnoreCase("0")) morse = "-----"; if (toEncode.equalsIgnoreCase("1")) morse = ".----";
  • 8. if (toEncode.equalsIgnoreCase("2")) morse = "..---"; if (toEncode.equalsIgnoreCase("3")) morse = "...--"; if (toEncode.equalsIgnoreCase("4")) morse = "....-"; if (toEncode.equalsIgnoreCase("5")) morse = "....."; if (toEncode.equalsIgnoreCase("6")) morse = "-...."; if (toEncode.equalsIgnoreCase("7")) morse = "--..."; if (toEncode.equalsIgnoreCase("8")) morse = "---.."; if (toEncode.equalsIgnoreCase("9")) morse = "----."; if (toEncode.equalsIgnoreCase(".")) morse = ".-.-"; if (toEncode.equalsIgnoreCase(",")) morse = "--..--"; if (toEncode.equalsIgnoreCase("?")) morse = "..--.."; return morse; } public static String decoder (String toEncode) { String morse = toEncode; if (toEncode.equalsIgnoreCase(".- ")) morse = "a"; if (toEncode.equalsIgnoreCase("-...")) morse = "b"; if (toEncode.equalsIgnoreCase("-.-.")) morse = "c"; if (toEncode.equalsIgnoreCase("-.."))
  • 9. morse = "d"; if (toEncode.equalsIgnoreCase(".")) morse = "e"; if (toEncode.equalsIgnoreCase("..-.")) morse = "f"; if (toEncode.equalsIgnoreCase("--.")) morse = "g"; if (toEncode.equalsIgnoreCase("....")) morse = "h"; if (toEncode.equalsIgnoreCase("..")) morse = "i"; if (toEncode.equalsIgnoreCase(".---")) morse = "j"; if (toEncode.equalsIgnoreCase("-.-")) morse = "k"; if (toEncode.equalsIgnoreCase(".-..")) morse = "l"; if (toEncode.equalsIgnoreCase("--")) morse = "m"; if (toEncode.equalsIgnoreCase("-.")) morse = "n"; if (toEncode.equalsIgnoreCase("---")) morse = "o"; if (toEncode.equalsIgnoreCase(".--.")) morse = "p"; if (toEncode.equalsIgnoreCase("--.-")) morse = "q"; if (toEncode.equalsIgnoreCase(".-.")) morse = "r"; if (toEncode.equalsIgnoreCase("...")) morse = "s"; if (toEncode.equalsIgnoreCase("-")) morse = "t"; if (toEncode.equalsIgnoreCase("..-")) morse = "u"; if (toEncode.equalsIgnoreCase("...-"))
  • 10. morse = "v"; if (toEncode.equalsIgnoreCase(".--")) morse = "w"; if (toEncode.equalsIgnoreCase("-..-")) morse = "x"; if (toEncode.equalsIgnoreCase("-.--")) morse = "y"; if (toEncode.equalsIgnoreCase("--..")) morse = "z"; if (toEncode.equalsIgnoreCase("-----")) morse = "0"; if (toEncode.equalsIgnoreCase(".----")) morse = "1"; if (toEncode.equalsIgnoreCase("..---")) morse = "2"; if (toEncode.equalsIgnoreCase("...--")) morse = "3"; if (toEncode.equalsIgnoreCase("....-")) morse = "4"; if (toEncode.equalsIgnoreCase(".....")) morse = "5"; if (toEncode.equalsIgnoreCase("-....")) morse = "6"; if (toEncode.equalsIgnoreCase("--...")) morse = "7"; if (toEncode.equalsIgnoreCase("---..")) morse = "8"; if (toEncode.equalsIgnoreCase("----.")) morse = "9"; if (toEncode.equalsIgnoreCase(" ")) morse = ""; return morse; } public static String englishToMorse( String text )
  • 11. { String Text = ""; String SChar; String CChar; for (int i = 0; i < text.length(); i++) { SChar = text.charAt(i) + ""; CChar = encoder(SChar); if (CChar.equals(" ")) { Text = Text + "| "; } else { Text = Text + CChar; if (!CChar.equals(" ")) { Text = Text + " "; } } } return Text; } public static String MorseToEnglish( String text ) { return null; } }
  • 12. Output: Enter 'Y' if you want convert from english to morse or enter 'N' to convert from morse to english : Y Enter the english text: MorseCode20 -- --- .-. ... . -.-. --- -.. . ..--- -----