A java program for this question please!
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().
Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)
import java.util.*;
public class TreeNode {
private int val; // The value in the node
private TreeNode left; // The left subtree (null
if empty)
private TreeNode right; // The right subtree
(null if empty)
// Recursion problem 1. Write the bodies of the
following two methods size
/** Return the size of the tree with this as
the root. */
public int size() {
return ((left == null) ?
0 : left.size()) +
((right == null)? 0 : right.size()) +
1;
}
/** Return the size of the tree whose root is
r. */
public static int size(TreeNode r) {
return ((r.left == null)
? 0 : size(r.left)) +
((r.right == null)? 0 : size(r.right)) +
1;
}
/** Return the number of leaves of this
binary tree with
* its root to be this node. */
public int getNumOfLeaves() {
return (left == null
&& right == null) ? 1 :
(left == null) ? right.getNumOfLeaves() :
(right == null)? left.getNumOfLeaves() :
left.getNumOfLeaves() + right.getNumOfLeaves();
}
/** Return the number of leaves of this
binary tree with
* its root to be r. */
public static int getNumOfLeaves(TreeNode r)
{
return (r.left == null
&& r.right == null) ? 1 :
(r.left == null) ? getNumOfLeaves(r.right) :
(r.right == null)? getNumOfLeaves(r.left) :
getNumOfLeaves(r.left) + getNumOfLeaves(r.right);
}
/** Return the result of b**c in O(c)
time.
* Precondition: c >= 0. */
public static double bPowerc(double b, int c)
{
assert c >= 0;
return (c == 0) ? 1
:
(c % 2 == 1) ? b * bPowerc(b, c-1) :
bPowerc(b*b, c/2);
}
/** Return true if all the letters in
String x are included in
* String s and in the same order. */
public static boolean isXish(String x, String s)
{
return x.equals("") ?
true :
s.indexOf(x.charAt(0)) >= 0 &&
isXish(x.substring(1),
s.substring(s.indexOf(x.charAt(0))+1));
}
/** Return true if the given word String s
contains the letters e,
* l and f without considering the order.
*/
public static boolean isElfish(String s) {
return s.contains("e")
&& s.contains("l") && s.contains("f");
}
/** Return a set of all permutations of s.
* Precondition: s contains no duplicates
--all chars are different*/
public static Set<String>
permutations(String s) {
if (s.length() ==
0)
return new HashSet<String>();
else if (s.length() ==
1) {
Set<String> permutationSet = new
HashSet<String>();
permutationSet.add(s);
return permutationSet;
}
else {
Set<String> permutationSetLesser =
permutations(s.substring(1));
Set<String> permutationSet = new
HashSet<String>();
for (String permutationLesser: permutationSetLesser)
for (int offset = 0; offset <= permutationLesser.length();
offset++)
permutationSet.add(new
StringBuilder(permutationLesser).insert(offset,
s.charAt(0)).toString());
return permutationSet;
}
}
// Write your coin-game stuff here (it is
optional)
public enum Player {
Alice, Bob;
/** Return the
opponent of this player. */
public Player opponent()
{
return this == Alice ? Bob : Alice;
}
/** Return the player
as a String. */
public String toString()
{
return this == Alice ? "Alice" : "Bob";
}
}
public static class PickCoinResult {
private Player
winner;
private int
NumOfStrategies;
/** Constructor.
*/
public
PickCoinResult(Player win, int NOS) {
winner = win;
NumOfStrategies = NOS;
}
/** Get the value of
fields. */
public Player
getWinner() {
return winner;
}
public int getNOS()
{
return NumOfStrategies;
}
}
/** Return the winner of the pick coin game
and the number of strategies. */
public static PickCoinResult pickCoin(int
NumOfCoins, Player first, Player second) {
assert NumOfCoins >
0;
if (NumOfCoins == 1
|| NumOfCoins == 2)
return new PickCoinResult(first, 1);
else if (NumOfCoins ==
3)
return new PickCoinResult(second, 2);
else if (NumOfCoins ==
4)
return new PickCoinResult(first, 3);
else {
// NumOfCoins > 4
if (NumOfCoins % 3 == 0) {
int NumOfStrategies = pickCoin(NumOfCoins - 1, second,
first).getNOS() +
pickCoin(NumOfCoins - 2, second, first).getNOS() +
pickCoin(NumOfCoins - 4, second, first).getNOS();
return new PickCoinResult(second, NumOfStrategies);
}
else {
int NumOfStrategies = ((NumOfCoins - 1) % 3 == 0) ?
(pickCoin(NumOfCoins - 1, second, first).getNOS() +
pickCoin(NumOfCoins - 4, second, first).getNOS()) :
pickCoin(NumOfCoins - 2, second, first).getNOS();
return new PickCoinResult(first, NumOfStrategies);
}
}
}
/** Constructor: a one-node tree with value v
*/
public TreeNode(int v) {
val= v;
}
/**Constructor: a treee with value v, left
subtree left, and right subtee right */
public TreeNode(int v, TreeNode left, TreeNode
right) {
val= v;
this.left= left;
this.right= right;
}
public @Override String toString() {
return
toString("");
}
/** Like toString() except that everything is
indented by sp (assumed to be
* a string with blanks in it) */
public String toString(String sp) {
if (left == null
&& right == null) {
return sp + val;
}
return sp + val +
" " + (left == null ? sp + " " + null : left.toString(sp + " "))
+
" " + (right == null ? sp + " " + null : right.toString(sp + "
"));
}
/** = a tree (1 2 (3 (4 5 6) null)) */
public static TreeNode tree1() {
TreeNode t1= new
TreeNode(1);
TreeNode t2= new
TreeNode(2);
TreeNode t3= new
TreeNode(3);
TreeNode t4= new
TreeNode(4);
TreeNode t5= new
TreeNode(5);
TreeNode t6= new
TreeNode(6);
return new TreeNode(1,
t2, new TreeNode(3, new TreeNode(4, t5, t6), null));
}
}
--------------------------------------------------------------------------------------------------------------------
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.*;
public class TreeNodeTest {
@Test
public void sizeleavesTest() {
TreeNode tree1 =
TreeNode.tree1();
assertEquals(6,
tree1.size());
assertEquals(6,
TreeNode.size(tree1));
assertEquals(3,
tree1.getNumOfLeaves());
assertEquals(3,
TreeNode.getNumOfLeaves(tree1));
}
@Test
public void bPowercTest() {
double[] bArray = {2, 3,
3.5, 4};
int[] cArray = {2, 3, 4,
5};
double[] expected = {4,
27, 150.0625, 1024};
double actual;
for (int i = 0; i
< bArray.length; i++) {
actual = TreeNode.bPowerc(bArray[i], cArray[i]);
assert expected[i] == actual;
}
}
@Test
public void isXishTest() {
String[][] testCases =
{{"ab", "abc"}, {"ba", "abc"}, {"ab", ""}};
boolean[] expected =
{true, false, false};
boolean actual =
false;
for (int i = 0; i
< testCases.length; i++) {
actual = TreeNode.isXish(testCases[i][0], testCases[i][1]);
assertEquals(expected[i], actual);
}
}
@Test
public void permutationsTest() {
String[] testCases =
{"", "a", "ab", "abc", "abcd"};
Set<String> actual
= null;
for (int i = 0; i<
testCases.length; i++) {
System.out.println("Test Case " + (i+1) + ":");
actual = TreeNode.permutations(testCases[i]);
for (String permutation: actual)
System.out.print(permutation + ' ');
System.out.println();
}
}
@Test
public void pickCoinTest() {
int[] numOfCoinsCases =
{1, 2, 3, 10, 25, 30};
TreeNode.Player[][]
PlayerCases = {{TreeNode.Player.Alice, TreeNode.Player.Bob},
{TreeNode.Player.Bob, TreeNode.Player.Alice},
{TreeNode.Player.Alice, TreeNode.Player.Bob},
{TreeNode.Player.Alice, TreeNode.Player.Bob},
{TreeNode.Player.Alice, TreeNode.Player.Bob},
{TreeNode.Player.Alice, TreeNode.Player.Bob}};
TreeNode.Player[]
expectedWinner = {TreeNode.Player.Alice,
TreeNode.Player.Bob,
TreeNode.Player.Bob,
TreeNode.Player.Alice,
TreeNode.Player.Alice,
TreeNode.Player.Bob};
int[] expectedNOS = {1, 1, 2, 22, 3344, 18272};
TreeNode.PickCoinResult actualResult = null;
for (int i = 0; i
< numOfCoinsCases.length; i++) {
actualResult = TreeNode.pickCoin(numOfCoinsCases[i],
PlayerCases[i][0], PlayerCases[i][1]);
assertEquals(expectedWinner[i], actualResult.getWinner());
assertEquals(expectedNOS[i], actualResult.getNOS());
}
}
}
![Elfish [-fldeaProjects/Elfish] - ./src/TreeNodeTest.java [Elfish Project *증 *一 eTreeNodejava GTreeNodeTest.java import static](http://img.homeworklib.com/questions/8e385050-9f10-11eb-8461-09d891fab962.png?x-oss-process=image/resize,w_560)
A java program for this question please! Recursion: A word is considered elfish if it contains...
JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using recursion. The method signatures are included in the starter code below, with a more detailed explanation of what function the method should perform. You will be writing the following methods: stringClean() palindromeChecker() reverseString() totalWord() permutation() You will be using tools in the String class like .substring(), .charAt(), and .length() in all of these methods, so be careful with indices. If you get stuck, think...
Question 2: Finding the best Scrabble word with Recursion using java Scrabble is a game in which players construct words from random letters, building on words already played. Each letter has an associated point value and the aim is to collect more points than your opponent. Please see https: //en.wikipedia.org/wiki/Scrabble for an overview if you are unfamiliar with the game. You will write a program that allows a user to enter 7 letters (representing the letter tiles they hold), plus...
Write a java program that demonstrates recursion. This program can be in a java GUI frame or as a console application. On this one it is up to you. (It would probably work better as a console program for this particular program.) The input for this program is a number of boxes. Ask the user for this with a textbox or a prompt on the command line. Demonstrate recursion with a function called LoadTruck() that takes a number of boxes...
using java code Create a program that has a main method and a recursive method: isPalindrome – recursive method that will receive a String and will return a Boolean that will evaluate whether the String is a palindrome or not. The main method will also ask the user for a word/phrase and using the recursive method isPalindrome it will print a message whether the word/phrase is a palindrome or not.
In java, write a program with a recursive method which asks the user for a text file (verifying that the text file exists and is readable) and opens the file and for each word in the file determines if the word only contains characters and determines if the word is alpha opposite. Now what I mean by alpha opposite is that each letter is the word is opposite from the other letter in the alphabet. For example take the word...
The program needs to be written in C. Write a function void camelCase(char* word) where word consists of more than two words separated by underscore such as “random_word” or "hello_world_my_name_is_sam". camelCase() should remove underscores from the sentence and rewrite in lower camel case” (https:// en.wikipedia.org/wiki/Camel_case). Watch out for the end of the string, which is denoted by ‘\0’. You have to ensure that legal strings are given to the camelCase() function. The program should only run when the input is...
Use stacks to implement a program in JAVA that will prompt the use to type a word, then print that word in reverse order of letters, then ask the user for another word, and so on. The program should terminate when the user enters "STOP" in upper case. Your deliverables shall include: class and method specifications; class and method definitions; at test class with at least two @test methods.
MUST BE IN JAVA AND USE RECURSION Write a recursive method that returns the number of all occurrences of a given word in all the files under a directory. Write a test program. Use the following header: public static long findInFile(File file, String word)
For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...
Write a Java program that reads a word and prints its bigrams substrings. A character bigram is defined as a continuous sequence of two characters in a word. For example, if the user provides the input "rum", the program prints ru um Hint: 1. set two pointers, one always moves from the beginning of the string and the other moves from i+2, in which i is the current position of the first pointer. 2. print an error message if the...