Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please help JAVA public class CodeConverter { Map codeMap; // map from alphanume

ID: 3712633 • Letter: P

Question

please help JAVA

public class CodeConverter {

Map codeMap; // map from alphanumeric to Morse code
Map invCodeMap; // map from Morse code to alphanumeric

/** DO NOT MODIFY THE DECLARATION LINES ABOVE */

/**
* Default constructor. Initialize the instance variable.
*/
public CodeConverter() {
throw new UnsupportedOperationException("replace with your implementation");
}

/**
* Read codeFile and beef up the code list. Each line in the codeFile has two
* fields (letter, codeString) separated by a comma.
*
* @param codeFile
* @throws IOException
*/
public void buildCodeTable(String codeFile) throws IOException {
Scanner input = new Scanner(new File(codeFile));

// ADD YOU CODE HERE
  
input.close();
}

/**
* Convert a text to the corresponding Morse code sequence, and return the Morse
* code sequence. Before conversion, convert all characters in the input text to
* upper-cases and also remove all leading and trailing whitespace chars of the
* input text.
*
* For each letter (A-Z) or a number in the text, the corresponding Morse code
* will be added to the output code sequence. Between the Morse codes for
* alphanumeric char, insert a space. The space in the text will be replaced
* with three spaces. Ignore all other characters.
*
* For example, Both "A b" and "A% b" will be encoded to ".- -...".
*
* @param text
* a sequence of characters.
* @return the Morse code corresponding to the input text
*/
public String toMorseCode(String text) {
throw new UnsupportedOperationException("replace with your implementation");
}

/**
* Convert a Morse code sequence to the corresponding alphanumeric text, and
* return the text. Before conversion, remove all leading and trailing
* whitespace chars of the input text.
*
* Note that symbols are separated with a space. The three consecutive spaces
* will be converted to a single space character in the output text. You may
* want to maintain two maps for this assignment -- one for Text to Morse, and
* the other for Morse to Text).
*
* For example, "... --- ..." will be converted to "SOS".
*
* @param morseCodeSeq
* a sequence of Morse code symbols.
* @return the text corresponding to the Morse code
*/
public String toAlphaNumeric(String morseCodeSeq) {
throw new UnsupportedOperationException("replace with your implementation");
}

/**
* This methods count how many Morse codes has symbols equal to the len input
* parameter. For example, if the input parameter is 1, this method should
* return 2 (The Morse code for 'E' is '.' and for 'T' is '-'.)
*
* @param len
* @return the number of keys of which Morse code is encoded with 'len' symbols.
*/
public int countByCodeLength(int len) {
throw new UnsupportedOperationException("replace with your implementation");
}

/**
* The main method is not needed for this assignment but you can use it as a
* driver to run a few test cases. It is recommended to write your tests in the
* StudentTests class and run it as JUnit tests as you run PublicTests.
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
CodeConverter codeConv = new CodeConverter();
codeConv.buildCodeTable("MorseCode.txt");

String morseCode = codeConv.toMorseCode("CMSC 131");
System.out.println(morseCode);
String text = codeConv.toAlphaNumeric(morseCode);
System.out.println(text);
}
}

the file MorseCode.txt contains the following

A,.-
B,-...
C,-.-.
D,-..
E,.
F,..-.
G,--.
H,....
I,..
J,.---
K,-.-
L,.-..
M,--
N,-.
O,---
P,.--.
Q,--.-
R,.-.
S,...
T,-
U,..-
V,...-
W,.--
X,-..-
Y,-.--
Z,--..
0,-----
1,.----
2,..---
3,...--
4,....-
5,.....
6,-....
7,--...
8,---..
9,----.

Explanation / Answer

Code:

import java.util.*;
import java.io.*;
class CodeConverter {

Map codeMap; // map from alphanumeric to Morse code
Map invCodeMap; // map from Morse code to alphanumeric

/** DO NOT MODIFY THE DECLARATION LINES ABOVE */

/**
* Default constructor. Initialize the instance variable.
*/
public CodeConverter() {
codeMap = new HashMap<String,String>();
invCodeMap = new HashMap<String,String>();

}

/**
* Read codeFile and beef up the code list. Each line in the codeFile has two
* fields (letter, codeString) separated by a comma.
*
* @param codeFile
* @throws IOException
*/
public void buildCodeTable(String codeFile) throws IOException {
Scanner input = new Scanner(new File(codeFile));
input.useDelimiter(" ,");
while(input.hasNextLine()){
String inp=input.nextLine();
String data []= inp.split(",");
codeMap.put(data[0],data[1]);
invCodeMap.put(data[1],data[0]);
}
input.close();
}

/**
* Convert a text to the corresponding Morse code sequence, and return the Morse
* code sequence. Before conversion, convert all characters in the input text to
* upper-cases and also remove all leading and trailing whitespace chars of the
* input text.
*
* For each letter (A-Z) or a number in the text, the corresponding Morse code
* will be added to the output code sequence. Between the Morse codes for
* alphanumeric char, insert a space. The space in the text will be replaced
* with three spaces. Ignore all other characters.
*
* For example, Both "A b" and "A% b" will be encoded to ".- -...".
*
* @param text
* a sequence of characters.
* @return the Morse code corresponding to the input text
*/
public String toMorseCode(String text) {
text = text.trim();
text = text.toUpperCase();
String res="";
for(int i=0;i<text.length();i++){
char x=text.charAt(i);
if((x>='A' && x<='Z')||(x>='0' && x<='9')){
res+=codeMap.get(x+"")+" ";
}
if(x==' '){
res+=" ";
}

}
return res;
}

/**
* Convert a Morse code sequence to the corresponding alphanumeric text, and
* return the text. Before conversion, remove all leading and trailing
* whitespace chars of the input text.
*
* Note that symbols are separated with a space. The three consecutive spaces
* will be converted to a single space character in the output text. You may
* want to maintain two maps for this assignment -- one for Text to Morse, and
* the other for Morse to Text).
*
* For example, "... --- ..." will be converted to "SOS".
*
* @param morseCodeSeq
* a sequence of Morse code symbols.
* @return the text corresponding to the Morse code
*/
public String toAlphaNumeric(String morseCodeSeq) {
morseCodeSeq = morseCodeSeq.trim();
String letter="";
String res="";
for(int i=0;i<morseCodeSeq.length();i++){
char x = morseCodeSeq.charAt(i);
if(x==' '){
res+=invCodeMap.get(letter+"");
letter="";
if(morseCodeSeq.charAt(i+1)==' ' && morseCodeSeq.charAt(i+2)==' '){ //if there are 3 consecutive spaces
res+=" ";
i+=3;
}
}
else{
letter+=x;
}
}

res+=invCodeMap.get(letter+"");//for getting the last letter
return res;
}

/**
* This methods count how many Morse codes has symbols equal to the len input
* parameter. For example, if the input parameter is 1, this method should
* return 2 (The Morse code for 'E' is '.' and for 'T' is '-'.)
*
* @param len
* @return the number of keys of which Morse code is encoded with 'len' symbols.
*/
public int countByCodeLength(int len) {
int count=0;
for (char x='A';x<='Z';x++)
{
if((codeMap.get(x+"")+"").length() == len)
count++;
}
for (char x='0';x<='9';x++)
{
if((codeMap.get(x+"")+"").length() == len)
count++;
}

return count;
}

/**
* The main method is not needed for this assignment but you can use it as a
* driver to run a few test cases. It is recommended to write your tests in the
* StudentTests class and run it as JUnit tests as you run PublicTests.
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
CodeConverter codeConv = new CodeConverter();
codeConv.buildCodeTable("MorseCode.txt");

String morseCode = codeConv.toMorseCode("CMSC 131");
System.out.println(morseCode);
String text = codeConv.toAlphaNumeric(morseCode);
System.out.println(text);
System.out.println("Number of morse codes of length 2: "+codeConv.countByCodeLength(2));
}
}

Sample Output:

radas-macOS:Desktop radas$ java CodeConverter
-.-. -- ... -.-. .---- ...-- .----
CMSC 131
Number of morse codes of length 2: 4