Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Recursion: A word is considered elfish if it contains the letters: e, l, and f in it, in any order. For example, we would say
Recursion: A word is considered elfish if it contains the letters: e, l, and f in it, in any order. For example, we would say that the following words are elfish: whiteleaf, tasteful, unfriendly, and waffles, because they each contain those letters.
- Write a recursive method called elfish(), that, given a word, tells us whether or not that word is elfish. The signature of the method should be:
public static boolean elfish(String word)
- Write a more generalized recursive method called x-ish. That, given two words, returns true if all the letters of the first word are contained in the second. The signature of the method should be:
public static boolean xish(String x, String word)
Sample Outputs:
Suppose the test program is Testish class, the sample outputs are:
$ java Testish unfriendly unfriendly is elfish $ java Testish elf unfriendly unfriendly contains elf $ java Testish els unfriendly unfriendly does not contain els $ java Testish Please provide one or two strings
Requirements:
- Files to be submitted: Testish.java.
- String input: the user provide the strings via command line arguments.
- If there is no argument provided, your program terminates with a user friendly message informing the user that at least one string is expected (see above sample output).
- If there is one argument provided, your program will treat it as elfish test and check whether or not the provided string contains e, l, and f.
- If there are at least two arguments provided, only the first two are used. Your program will perform xish check: whether the second string contains the first string.
- Make sure method elfish() and xish() are recursive. Any non-recursive() implementation will receive zero credit.
- String is provided in java.lang package, which is included by default. Your program is not allowed to import any other class in this assignment.
Hints:
- The key to design a recursive method is to (1) have the method call itself and (2) establish a base case. Considering the generic case with two input strings s1 and s2, the basic idea is to linearly scan string s1 one character (starting from the leftmost one) at a time and try to find a matching character in string s2. A failure will indicate a false result. A success then will continue with a recursive method call: move the character pointer of s1 to one position to the right and repeat the above process. Obviously, the base case of this recursion is when the character pointer of s1 is pointing to the end of s1 (you should return true in this case, why?).
- You are allowed to use any method defined in Java String class. These methods can be found at Java String API. In particular, you may find the following methods useful:
- charAt();
- indexOf();
- isEmpty();
- length();
- substring().
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started