Question

Implement the method Encoding.morseCodes(int m, int n) that yields a set of all Morse code strings...

  1. Implement the method Encoding.morseCodes(int m, int n) that yields a set of all Morse code strings with m dots and n dashes. For example, morseCodes(2, 1) yields a set with ..- .-. ..- -.. in some order. Hint: Recursion. Start with a dot, and then what? Start with a dash, and then what? Check your answer with CodeCheck http://codecheck.it/codecheck/files?type=jsonp&problem=16081624106d9jhwd3l5v52tat8n571k7wo
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.*;

public class Encoding {

    public static Set<String> morseCodes(int m, int n) {
        Set<String> result = new TreeSet<>();

        // base cases
        if (m == 0 && n == 0){
            result.add("");
        }
        else if (n == 0){
            String s = "";
            for(int i=0; i<m; i++) {
                s += ".";
            }
            result.add(s);
        }
        else if (m == 0){
            String s = "";
            for(int i=0; i<n; i++) {
                s += "-";
            }
            result.add(s);
        }
        // recursive case
        else{
            Set<String> subresult1 = morseCodes(m-1, n);
            Set<String> subresult2 = morseCodes(m, n-1);

            for(String str : subresult1){
                // append "." to str and add it to "result"
                result.add(str + ".");
            }
            for(String str : subresult2){
                // append "-" to str and add it to "result"
                result.add(str + "-");
            }

        }
        return result;
    }
}


Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
Implement the method Encoding.morseCodes(int m, int n) that yields a set of all Morse code strings...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Help with writing morse code

    Screen Shot 2021-03-17 at 9.57.42 AM.pngScreen Shot 2021-03-17 at 9.57.49 AM.png3 Decoding a Morse Code messageIn this exercise you will decipher a Morse code message sent to Agent 008 by Agent \(007 .\) The last words of Agent 007 were "The future of technology lies in \(\ldots\) " at which point she produced a memory stick containing a MATLAB file ctftmod.mat. The file ctftmod \(.\) mat contains the following:af, bf the denominator and numerator coefficients of a lowpass filter, whose...

  • Program Description: A Java program is to be created to produce Morse code. The Morse code...

    Program Description: A Java program is to be created to produce Morse code. The Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few special characters (such as period, comma, colon, and semicolon). In sound-oriented systems, the dot represents a short sound and the dash represents a long sound. Separation between words is indicated by a space, or, quite simply, the absence of a dot or dash. In a sound-oriented...

  • please implement this function by C language Write a string compare function which returns 1 if the strings match for n characters starting at offset m, O if the strings don't match. You must chec...

    please implement this function by C language Write a string compare function which returns 1 if the strings match for n characters starting at offset m, O if the strings don't match. You must check if m is within the length of both s and t. int submatch(char* s, char* t, int n, int m) Write a string compare function which returns 1 if the strings match for n characters starting at offset m, O if the strings don't match....

  • In Worked Example 10.1, add a default method default int [] values(int n) that yields an...

    In Worked Example 10.1, add a default method default int [] values(int n) that yields an array of the first n values of the sequence. Please use only the code provided below from the worked example and if possible provide screenshots using all the code provided and the test class provided: Sequence.java public interface Sequence { int next(); } LastDigitDistribution.java public class LastDigitDistribution { public void process(Sequence seq, int valuesToProcess) {       for (int i = 1; i <=...

  • Implement a rabin_hash method to make the following code work: int rabin_karp_batchmatch(int bsz, /* size of...

    Implement a rabin_hash method to make the following code work: int rabin_karp_batchmatch(int bsz, /* size of bitmap (in bits) to be used */ int k, /* chunk length to be matched */ const char *qs, /* query docoument (X)*/ int m, /* query document length */ const char *ts, /* to-be-matched document (Y) */ int n /* to-be-matched document length*/) { /*if the to-be-matched document is less than k length, return false*/ if (n < k) return 0; /*start our...

  • Trying to make an autocomplete java program that given a prefix, find all strings in set...

    Trying to make an autocomplete java program that given a prefix, find all strings in set that start with said prefix, in descending weight order. I created the classes Term.java, BinarySearchDeluxe.java, and Autocomplete.java posted below. I believe Term.java is correct, but where you see "//?" needs added code. I will also list before the code, the descriptions of said methods with "//?" to explain their purpose. DO NOT add more import packages or change the main methods as that is...

  • A bounded string list has an upper limit on its size, which is set when the...

    A bounded string list has an upper limit on its size, which is set when the list is created. The size limit specifies the maximum number of strings that can be stored in a bounded string list. String lists are mutable and allow random accesses to their elements using indexes. The list operations include BoundedStringList(int sizeLimit); void insert(String newString, int index); void delete(int index); String get(int index); The constructor creates an empty list with a maximum size of sizeLimit. The...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • For C++ This is the information about the meal class 2-1. (24 marks) Define and implement...

    For C++ This is the information about the meal class 2-1. (24 marks) Define and implement a class named Dessert, which represents a special kind of Meal. It is to be defined by inheriting from the Meal class. The Dessert class has the following constructor: Dessert (string n, int co) // creates a meal with name n, whose type // is "dessert" and cost is co The class must have a private static attribute static int nextID ; which is...

  • The question exercises the union, intersection, and the intersection operations of two set of strings (names)....

    The question exercises the union, intersection, and the intersection operations of two set of strings (names). For this type of problem, you need to preserve the original sets from being modified by some of the set method. You can used the clone( ) method to copy the sets or simply create another set using the same array contents as the original. You can find an example of the clone() for the LinkedHashSet set in   the provided “skeleton” program (assign8_Q3.java) code...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT