SlideShare a Scribd company logo
Need help coding MorseCode in Java:
Create Class MorseCodeClient. This represents a Client that allows a user communicate with a
server across a network. The server saves the message and then relays the message to another
client This is a JFrame application and should extend JFrame and Implement Runnable. The
client application should allow the user to type English-language phrases in a JTextArea. When
the user sends the message, the client application encodes the text into Morse code and sends the
coded message through the server to the other client. Create class MorseCodeClientTest, this is a
test class for morse code client and uses the main method to created new instance of
MorseCodeClient.
Solution
package MorseCode;
import java.util.ArrayList;
public class MorseCodeConverter extends javax.swing.JFrame {
/**
* Creates new form MorseCodeConverter
*/
public MorseCodeConverter() {
initComponents();
}
@SuppressWarnings("unchecked")
// //GEN-BEGIN:initComponents
private void initComponents() {
mainWindowContainer = new javax.swing.JPanel();
userSentence = new javax.swing.JTextField();
inputLabel = new javax.swing.JLabel();
outputLabel = new javax.swing.JLabel();
inputConfirmButton = new javax.swing.JButton();
outputField = new javax.swing.JTextField();
title = new javax.swing.JLabel();
javax.swing.GroupLayout mainWindowContainerLayout = new
javax.swing.GroupLayout(mainWindowContainer);
mainWindowContainer.setLayout(mainWindowContainerLayout);
mainWindowContainerLayout.setHorizontalGroup(
mainWindowContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEAD
ING)
.addGap(0, 100, Short.MAX_VALUE)
);
mainWindowContainerLayout.setVerticalGroup(
mainWindowContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEAD
ING)
.addGap(0, 100, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(204, 204, 255));
userSentence.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
userSentenceActionPerformed(evt);
}
});
inputLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
inputLabel.setText("Enter your sentence here");
outputLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
outputLabel.setText("This is your sentence in Morse Code");
outputLabel.setToolTipText("");
inputConfirmButton.setText("OK");
inputConfirmButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
inputConfirmButtonActionPerformed(evt);
}
});
outputField.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
title.setFont(new java.awt.Font("Simplified Arabic", 0, 18)); // NOI18N
title.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
title.setText("Morse Code Converter");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(171, 171, 171)
.addComponent(inputConfirmButton))
.addGroup(layout.createSequentialGroup()
.addGap(88, 88, 88)
.addComponent(inputLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 148,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(86, 86, 86)
.addComponent(outputLabel, javax.swing.GroupLayout.PREFERRED_SIZE,
212, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(userSentence, javax.swing.GroupLayout.DEFAULT_SIZE,
349, Short.MAX_VALUE)
.addComponent(title, javax.swing.GroupLayout.PREFERRED_SIZE, 346,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(outputField))))
.addContainerGap(30, Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[]
{inputLabel, outputLabel});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(title, javax.swing.GroupLayout.PREFERRED_SIZE, 26,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(21, 21, 21)
.addComponent(inputLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(userSentence, javax.swing.GroupLayout.PREFERRED_SIZE, 27,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(inputConfirmButton)
.addGap(31, 31, 31)
.addComponent(outputLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(outputField, javax.swing.GroupLayout.PREFERRED_SIZE, 30,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(44, 44, 44))
);
layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[]
{outputField, userSentence});
pack();
}// //GEN-END:initComponents
/**
* This method is run when the User enters a sentence; it runs
* when the user hits 'enter'.
*/
private void userSentenceActionPerformed(java.awt.event.ActionEvent evt) {
//GEN- FIRST:event_userSentenceActionPerformed
String[] morseCode = {" ", "--..--", ".-.-.-", "..--..", "-----",
".----", "..---", "...---", "....-", ".....", "-....", "--...",
"---..", "----.", ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
"....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
"--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--",
"--.."};
String english = " ,.?0123456789abcdefghijklmnopqrstuvwxyz";
char[] englishArray = english.toCharArray();
ArrayList holdList = new ArrayList<>();
StringBuilder holdBuilder = new StringBuilder();
String userString = userSentence.getText().toLowerCase();
char[] userArray = userString.toCharArray();
for(int i = 0; i < userArray.length; i++) {
for(int n = 0; n < englishArray.length; n++) {
if(userArray[i] == englishArray[n]) {
holdList.add(n);
}
}
}
for(int i = 0; i < holdList.size(); i++) {
holdBuilder.append(morseCode[holdList.get(i)]);
}
String moCode = holdBuilder.toString();
outputField.setText(moCode);
}//GEN-LAST:event_userSentenceActionPerformed
/**
* This method is run when the user clicks the 'OK' button
* in the GUI. It has the same code as the userSentence method
*/
private void inputConfirmButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-
FIRST:event_inputConfirmButtonActionPerformed
// a string array to hold all of the Morse Code values
String[] morseCode = {" ", "--..--", ".-.-.-", "..--..", "-----",
".----", "..---", "...---", "....-", ".....", "-....", "--...",
"---..", "----.", ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
"....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
"--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--",
"--.."};
// a string of english letter, numbers and punctuation that
// corresponds to the Morse Code list
String english = " ,.?0123456789abcdefghijklmnopqrstuvwxyz";
// turn that english string into an array of characters
// this will allow us to iterate over it
char[] englishArray = english.toCharArray();
// make an ArrayList to hold our index values as we find them.
// also initialize a StringBuilder object that will help us in
// printing out a clean string of morse code values
ArrayList holdList = new ArrayList<>();
StringBuilder holdBuilder = new StringBuilder();
// user input, convert to lowercase for simplicity;
// convert the input to a character array. This will
// allow iteration
String userString = userSentence.getText().toLowerCase();
char[] userArray = userString.toCharArray();
// loop through each character in the userArray, and match
// them up with values in the array of english characters.
// save the index value of the english array
for(int i = 0; i < userArray.length; i++) {
for(int n = 0; n < englishArray.length; n++) {
if(userArray[i] == englishArray[n]) {
holdList.add(n);
}
}
}
// append all morse codes associated
// with the english array indexes to the StringBuilder object.
for(int i = 0; i < holdList.size(); i++) {
holdBuilder.append(morseCode[holdList.get(i)]);
}
// convert that object to a string and print it out.
String moCode = holdBuilder.toString();
outputField.setText(moCode);
}//GEN-LAST:event_inputConfirmButtonActionPerformed
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MorseCodeConverter().setVisible(true);
}
});
}
private javax.swing.JButton inputConfirmButton;
private javax.swing.JLabel inputLabel;
private javax.swing.JPanel mainWindowContainer;
private javax.swing.JTextField outputField;
private javax.swing.JLabel outputLabel;
private javax.swing.JLabel title;
private javax.swing.JTextField userSentence;
}

More Related Content

More from fastechsrv (20)

PDF
This figure shows the stress-strain curve for a polymer. The followi.pdf
fastechsrv
 
PDF
There are 6 holes in a wall. A mouse is in one hole I dont know wh.pdf
fastechsrv
 
PDF
The identity below was verified incorrectly. In which line was an err.pdf
fastechsrv
 
PDF
The FED is both centralized and decentralized in its structure. .pdf
fastechsrv
 
PDF
The diameter of the Milky Way disc is approximately 9x10^20 meters. .pdf
fastechsrv
 
PDF
The Ad Hoc network shown below (including node Q) is currently operat.pdf
fastechsrv
 
PDF
take the following code and give details of what each line of code i.pdf
fastechsrv
 
PDF
RNA is important for all of the following reasons EXCEPT most RNA .pdf
fastechsrv
 
PDF
Patient A got IV (Intravenous) fluid at the hospital that turned out .pdf
fastechsrv
 
PDF
Novotny et al. The authors of the paper Why are there so many speci.pdf
fastechsrv
 
PDF
Match the description with the appropriate term. Where more than one .pdf
fastechsrv
 
PDF
How should globalization be viewed as a four dimensional concept.pdf
fastechsrv
 
PDF
I want the show the works step by step for Aand B Autism is a seriou.pdf
fastechsrv
 
PDF
If only one strand of the DNA molecule is transcribed for a particul.pdf
fastechsrv
 
PDF
How do each of the factors listed in (1) affect the diffusion of sol.pdf
fastechsrv
 
PDF
find the domain of the function f (x)= 2x3 x8SolutionNumerat.pdf
fastechsrv
 
PDF
Explain a) advantage of dispersing seeds, andexplain b) two (2) neat.pdf
fastechsrv
 
PDF
Discuss the meaning of a tortWhat are the four elements of neglig.pdf
fastechsrv
 
PDF
Describe the example of Darwin’s finches and how adaptive radiati.pdf
fastechsrv
 
PDF
Consider the vector space V=ropf^2 over the set of scalars ropf. Defi.pdf
fastechsrv
 
This figure shows the stress-strain curve for a polymer. The followi.pdf
fastechsrv
 
There are 6 holes in a wall. A mouse is in one hole I dont know wh.pdf
fastechsrv
 
The identity below was verified incorrectly. In which line was an err.pdf
fastechsrv
 
The FED is both centralized and decentralized in its structure. .pdf
fastechsrv
 
The diameter of the Milky Way disc is approximately 9x10^20 meters. .pdf
fastechsrv
 
The Ad Hoc network shown below (including node Q) is currently operat.pdf
fastechsrv
 
take the following code and give details of what each line of code i.pdf
fastechsrv
 
RNA is important for all of the following reasons EXCEPT most RNA .pdf
fastechsrv
 
Patient A got IV (Intravenous) fluid at the hospital that turned out .pdf
fastechsrv
 
Novotny et al. The authors of the paper Why are there so many speci.pdf
fastechsrv
 
Match the description with the appropriate term. Where more than one .pdf
fastechsrv
 
How should globalization be viewed as a four dimensional concept.pdf
fastechsrv
 
I want the show the works step by step for Aand B Autism is a seriou.pdf
fastechsrv
 
If only one strand of the DNA molecule is transcribed for a particul.pdf
fastechsrv
 
How do each of the factors listed in (1) affect the diffusion of sol.pdf
fastechsrv
 
find the domain of the function f (x)= 2x3 x8SolutionNumerat.pdf
fastechsrv
 
Explain a) advantage of dispersing seeds, andexplain b) two (2) neat.pdf
fastechsrv
 
Discuss the meaning of a tortWhat are the four elements of neglig.pdf
fastechsrv
 
Describe the example of Darwin’s finches and how adaptive radiati.pdf
fastechsrv
 
Consider the vector space V=ropf^2 over the set of scalars ropf. Defi.pdf
fastechsrv
 

Recently uploaded (20)

PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
community health nursing question paper 2.pdf
Prince kumar
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 

Need help coding MorseCode in JavaCreate Class MorseCodeClient. T.pdf

  • 1. Need help coding MorseCode in Java: Create Class MorseCodeClient. This represents a Client that allows a user communicate with a server across a network. The server saves the message and then relays the message to another client This is a JFrame application and should extend JFrame and Implement Runnable. The client application should allow the user to type English-language phrases in a JTextArea. When the user sends the message, the client application encodes the text into Morse code and sends the coded message through the server to the other client. Create class MorseCodeClientTest, this is a test class for morse code client and uses the main method to created new instance of MorseCodeClient. Solution package MorseCode; import java.util.ArrayList; public class MorseCodeConverter extends javax.swing.JFrame { /** * Creates new form MorseCodeConverter */ public MorseCodeConverter() { initComponents(); } @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { mainWindowContainer = new javax.swing.JPanel(); userSentence = new javax.swing.JTextField(); inputLabel = new javax.swing.JLabel(); outputLabel = new javax.swing.JLabel(); inputConfirmButton = new javax.swing.JButton(); outputField = new javax.swing.JTextField(); title = new javax.swing.JLabel(); javax.swing.GroupLayout mainWindowContainerLayout = new javax.swing.GroupLayout(mainWindowContainer); mainWindowContainer.setLayout(mainWindowContainerLayout); mainWindowContainerLayout.setHorizontalGroup(
  • 2. mainWindowContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEAD ING) .addGap(0, 100, Short.MAX_VALUE) ); mainWindowContainerLayout.setVerticalGroup( mainWindowContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEAD ING) .addGap(0, 100, Short.MAX_VALUE) ); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setBackground(new java.awt.Color(204, 204, 255)); userSentence.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { userSentenceActionPerformed(evt); } }); inputLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); inputLabel.setText("Enter your sentence here"); outputLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); outputLabel.setText("This is your sentence in Morse Code"); outputLabel.setToolTipText(""); inputConfirmButton.setText("OK"); inputConfirmButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { inputConfirmButtonActionPerformed(evt); } }); outputField.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N title.setFont(new java.awt.Font("Simplified Arabic", 0, 18)); // NOI18N title.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); title.setText("Morse Code Converter"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(
  • 3. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(171, 171, 171) .addComponent(inputConfirmButton)) .addGroup(layout.createSequentialGroup() .addGap(88, 88, 88) .addComponent(inputLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGap(86, 86, 86) .addComponent(outputLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGap(20, 20, 20) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(userSentence, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE) .addComponent(title, javax.swing.GroupLayout.PREFERRED_SIZE, 346, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(outputField)))) .addContainerGap(30, Short.MAX_VALUE)) ); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {inputLabel, outputLabel}); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(title, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(21, 21, 21)
  • 4. .addComponent(inputLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(userSentence, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(inputConfirmButton) .addGap(31, 31, 31) .addComponent(outputLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(outputField, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(44, 44, 44)) ); layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {outputField, userSentence}); pack(); }// //GEN-END:initComponents /** * This method is run when the User enters a sentence; it runs * when the user hits 'enter'. */ private void userSentenceActionPerformed(java.awt.event.ActionEvent evt) { //GEN- FIRST:event_userSentenceActionPerformed String[] morseCode = {" ", "--..--", ".-.-.-", "..--..", "-----", ".----", "..---", "...---", "....-", ".....", "-....", "--...", "---..", "----.", ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}; String english = " ,.?0123456789abcdefghijklmnopqrstuvwxyz"; char[] englishArray = english.toCharArray(); ArrayList holdList = new ArrayList<>(); StringBuilder holdBuilder = new StringBuilder();
  • 5. String userString = userSentence.getText().toLowerCase(); char[] userArray = userString.toCharArray(); for(int i = 0; i < userArray.length; i++) { for(int n = 0; n < englishArray.length; n++) { if(userArray[i] == englishArray[n]) { holdList.add(n); } } } for(int i = 0; i < holdList.size(); i++) { holdBuilder.append(morseCode[holdList.get(i)]); } String moCode = holdBuilder.toString(); outputField.setText(moCode); }//GEN-LAST:event_userSentenceActionPerformed /** * This method is run when the user clicks the 'OK' button * in the GUI. It has the same code as the userSentence method */ private void inputConfirmButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN- FIRST:event_inputConfirmButtonActionPerformed // a string array to hold all of the Morse Code values String[] morseCode = {" ", "--..--", ".-.-.-", "..--..", "-----", ".----", "..---", "...---", "....-", ".....", "-....", "--...", "---..", "----.", ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}; // a string of english letter, numbers and punctuation that // corresponds to the Morse Code list String english = " ,.?0123456789abcdefghijklmnopqrstuvwxyz"; // turn that english string into an array of characters // this will allow us to iterate over it char[] englishArray = english.toCharArray();
  • 6. // make an ArrayList to hold our index values as we find them. // also initialize a StringBuilder object that will help us in // printing out a clean string of morse code values ArrayList holdList = new ArrayList<>(); StringBuilder holdBuilder = new StringBuilder(); // user input, convert to lowercase for simplicity; // convert the input to a character array. This will // allow iteration String userString = userSentence.getText().toLowerCase(); char[] userArray = userString.toCharArray(); // loop through each character in the userArray, and match // them up with values in the array of english characters. // save the index value of the english array for(int i = 0; i < userArray.length; i++) { for(int n = 0; n < englishArray.length; n++) { if(userArray[i] == englishArray[n]) { holdList.add(n); } } } // append all morse codes associated // with the english array indexes to the StringBuilder object. for(int i = 0; i < holdList.size(); i++) { holdBuilder.append(morseCode[holdList.get(i)]); } // convert that object to a string and print it out. String moCode = holdBuilder.toString(); outputField.setText(moCode); }//GEN-LAST:event_inputConfirmButtonActionPerformed public static void main(String args[]) { /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() {
  • 7. new MorseCodeConverter().setVisible(true); } }); } private javax.swing.JButton inputConfirmButton; private javax.swing.JLabel inputLabel; private javax.swing.JPanel mainWindowContainer; private javax.swing.JTextField outputField; private javax.swing.JLabel outputLabel; private javax.swing.JLabel title; private javax.swing.JTextField userSentence; }