In: Computer Science
I have a code and it works and runs as it supposed too. What is the UML for it? Any help will be awesome. Thanks.
import java.util.Scanner;
public class StringToMorseCode {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
char[] letters = { ' ', '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', '1', '2', '3', '4', '5', '6', '7', '8',
'9', '0' };
String[] morseLetters = { " ", " .- ", " -... ", " -.-. ", " -.. ",
" . ", " ..-. ", " --. ", " .... ", " .. ", " .--- ", " -.- ", "
.-.. ", " -- ", " -. ", " --- ", " .--. ", " --.- ", " .-. ", " ...
", " - ", " ..- ", " ...- ", " .-- ", " -..- ", " -.-- ", " --.. ",
" .---- ", " ..--- ", " ...-- ", " ....- ", " ..... ", " -.... ", "
--... ", " ---.. ", " ----. ", " ----- "};
String textToChange = "";
String newText = "";
System.out.println("Enter the text you want to change to Morse
code:");
textToChange = input.nextLine();
textToChange = textToChange.toLowerCase();
for (int i = 0; i < textToChange.length(); i++) {
for (short j = 0; j < 37; j++) {
if (textToChange.charAt(i) == letters[j]) {
newText += morseLetters[j];
newText += " ";
break;
}
}
}
System.out.println("Text in Morse Code:");
System.out.println(newText);
}
}
Since you have all the code inside main method, the resultant UML diagram will only have class name and main method and nothing else. So this code needs a bit of rearrangement before creating a sensible UML class diagram. Please find the below code which does the same thing as your previous code, but with a more object oriented approach.
StringToMorseCode.java (updated)
import java.util.Scanner;
public class StringToMorseCode {
// array containing valid lower case letters
char[] letters = { ' ', '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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
// String array containing morse code equivalents of above characters
String[] morseLetters = { " ", " .- ", " -... ", " -.-. ", " -.. ", " . ",
" ..-. ", " --. ", " .... ", " .. ", " .--- ", " -.- ", " .-.. ",
" -- ", " -. ", " --- ", " .--. ", " --.- ", " .-. ", " ... ",
" - ", " ..- ", " ...- ", " .-- ", " -..- ", " -.-- ", " --.. ",
" .---- ", " ..--- ", " ...-- ", " ....- ", " ..... ", " -.... ",
" --... ", " ---.. ", " ----. ", " ----- " };
// method to convert a text to morse code and return the resultant text
String convertTextToMorseCode(String textToChange) {
textToChange = textToChange.toLowerCase();
String newText = "";
for (int i = 0; i < textToChange.length(); i++) {
for (short j = 0; j < 37; j++) {
if (textToChange.charAt(i) == letters[j]) {
newText += morseLetters[j];
newText += " ";
break;
}
}
}
return newText;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String textToChange = "";
String newText = "";
System.out.println("Enter the text you want to change to Morse code:");
textToChange = input.nextLine();
// creating an object of StringToMorseCode
StringToMorseCode converter = new StringToMorseCode();
// converting textToChange to morse code, assigning to newText
newText = converter.convertTextToMorseCode(textToChange);
// displaying results
System.out.println("Text in Morse Code:");
System.out.println(newText);
}
}
UML DIAGRAM