Question

In: Computer Science

I have a code and it works and runs as it supposed too. What is the...

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);
}
}

Solutions

Expert Solution

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



Related Solutions

I have the following assignment for probability class: I am supposed to write a routine (code)...
I have the following assignment for probability class: I am supposed to write a routine (code) in MATLAB that does solve the following problem for me: a) Generate N=10000 samples of a Uniform random variable (X) using the rand () command. This Uniform RV should have a range of -1 to +3. b) Generate (Estimate) and plot the PDF of X from the samples. You are not allowed to use the Histogram () command, any commands from the Matlab Statistics...
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
i have attached my code here and we are supposed to create two classes. one is...
i have attached my code here and we are supposed to create two classes. one is date the other switches accounts for bank and then displays the bank account,type,name,date(pulled from first class and then prints out. i am having issues getting my code to pull from the first class and dont know how to make it read the data from date class if someone can look at my code and tell me how to fix this i would greatly appreciate...
I was not sure how to utilize this line because I made code that works but...
I was not sure how to utilize this line because I made code that works but not with this line specifically. C++ Function 2: bool exists_trio_within_distance(int*,int,int); //Input:    //an integer array (param 1), its size (param 2), and    //a distance (param 3) //Output:    //True or false //Behavior:    //Returns true is there exists    //a sequence of 3 values in the array    //such that sum of the first two elements    //is equal to the third element...
I have this mystery code. I dont know what the code does. Can somone please explain...
I have this mystery code. I dont know what the code does. Can somone please explain with examples. (python 3.xx) from typing import Dict, TextIO, Tuple, List def exam(d1: Dict[str, List[int]], d2: Dict[int, int]) -> None: """ *Mystery code* """ for key in d1: value = d1[key] for i in range(len(value)): value[i] = d2[value[i]]   
USE R CODING! Pleaseee I need the code With R coding Obs: it supposed to use...
USE R CODING! Pleaseee I need the code With R coding Obs: it supposed to use probability density function like X ~ Binomial( n ,p ) dbinom(X=?, n, prob) pbinom(X=?, n, prob) rbinmo(幾個符合二項分配的X, n, prob) X~Poisson (lamda) dpois(X=?, lamda) ppois (X=?, lamda) rpois (X, lamda) **Suppose the random variable X obeys the binomial allocation B (n=100, p=0.1) (a) Use X as the binomial allocation to calculate P(12≤X≤14) (b) In practice, when np ≥ 5 and n(1-p) ≥ 5, X will...
Here is what I have so far. I have created a code where a user can...
Here is what I have so far. I have created a code where a user can enter in their information and when they click submit all of the information is shown. How can I add a required field for the phone number without using an alert? <!Doctype html> <html> <head> <meta charset="UTF-8"> <title>Login and Registeration Form Design</title> <link rel="stylesheet" type="text/css" href="signin.css"> <script> function myFunction(){ document.getElementById('demo').innerHTML = document.getElementById('fname').value + " " + document.getElementById('lname').value + " " + document.getElementById('street').value + " "...
I need a Verilog code that makes the LEDs on the FPGA board works like this....
I need a Verilog code that makes the LEDs on the FPGA board works like this. https://image.ibb.co/mu5tnS/6.gif There are 16 LEDs in the FPGA board
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1478) at Main.main(Main.java:34) Your output Welcome to the food festival! Would you like to place an order? Expected output This test case should produce no output in java import java.util.Scanner; public class Main {    public static void display(String menu[])    {        for(int i=0; i<menu.length; i++)        {            System.out.println (i + " - " + menu[i]);...
Here is my java code. It works and has the correct output, but I need to...
Here is my java code. It works and has the correct output, but I need to add a file and I am not sure how. I cannot use the FileNotFoundException. Please help! import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,0,0}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: "...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT