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...
hi! I have this code. when I run it, it works but in the print(the number...
hi! I have this code. when I run it, it works but in the print(the number we are searching for is ) it prints the number twice. ex. for 5 it prints 55 etc. apart from this, it works #include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ int n,i; printf("Give me the size of the table : \n"); scanf("%d", &n); int A[n],x,y; for (i=0;i<n;i++) A[i]=rand() % n; for (i=0;i<n;i++) printf("%d\n", A[i]); srand(time(NULL)); y=rand()% n ; printf("The number we are searching...
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 + " "...
program runs but does not compute the average. Here is what I have. Thank you #include...
program runs but does not compute the average. Here is what I have. Thank you #include <iostream> #include<iomanip> #include"Average.h" using namespace std; int main() {    cout<<"Enter 3 integers, seperated by spaces, on a single line: ";    int number1, number2, number3;    cin>>number1>>number2>>number3;    int average;    double avg; //   average = computeAverage(number1, number2, number3);    cout<<"Average of "<<number1<<" and "<<number2 <<" and "<<number3 <<" is "    <<average<<endl;          cout<<"Enter 2 integers, seperated by a space,...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT