Question: Java code please public class Recursion { /** * Write a recursive function that finds the index of s2 in s1. Do not use any

Java code please

public class Recursion {

/** * Write a recursive function that finds the index of s2 in s1. Do not use any * string functions except for .length(), .equals(), and .substring(). Do not use * any loops, or any data structures. * @param s1 * @param s2 * @return Returns the index of the first time that * s2 appears in s1 or -1 if s2 is not contained * in s1. */ public static int indexOf(String s1, String s2) { return -1; }

/** * Write a recursive function that removes the first k even numbers * from the stack. If there are less than k even elements in the stack, * just remove all even elements. Do not use any loops or data structures * other than the stack passed in as a parameter. * @param stack * @param k * @return Returns the number of elements removed from the stack. */ public static int removeEvenNumbers(Stack stack, int k) { return 0; }

/** * Write a recursive function that accepts an integer and * returns a new number containing only the even digits, in the same * order. If there are no even digits, return 0. Your function should * work for positive or negative numbers. You are NOT allowed * to use any data structures. You are not allowed to use Strings to * solve this problem either. * @param n * @return The input with only the even digits remaining in the same * order. */ public static int evenDigits(int n) { return 0; }

/** * Write a recursive function that evaluates a Queue as a mathematical * expression. This queue can have any of the following characters: * { '(', ')', '+', '*'} or any single digit number. Evaluate this expression and * return the result. For example, for the expression: * "(((1+2)*(3+1))+(1*(2+2)))", each of these characters would be in the * q. As you recursively evaluate characters from the expression, you will * remove the characters from the q. After evaluating the above expression, * you should return 16. You are guaranteed that there are NO two digit numbers, * and that the expression is well formed (parenthesis match, etc...). Do not use any * loops. Do not use any data structures besides the q passed in as a parameter. * @param q * @return The result of the mathematical expression. */ public static int evaluate(Queue q) { return 0; }

/** * Write a recursive function that accepts a stack of integers and * replaces each int with two copies of that integer. For example, * calling repeatStack and passing in a stack of { 1, 2, 3} would change * the stack to hold { 1, 1, 2, 2, 3, 3}. Do not use any loops. Do not use * any data structures other than the stack passed in as a parameter. * @param stack */ public static void repeatStack(Stack stack) {}

/** * Write a recursive function that accepts a Queue. It * should change every int in this queue to be double its original * value. You may NOT use loops or any other data structures besides * the queue passed in as a parameter. You may use a helper function. * @param q */ public static void doubleElements(Queue q) {}

}

sample outputs here

System.out.println(Recursion.indexOf("Hello", "lo")); // 3 Stack stack = new Stack(); stack.push(2); stack.push(3); stack.push(4); stack.push(55); stack.push(6); stack.push(17); stack.push(8); System.out.println(Recursion.removeEvenNumbers(stack, 2)); // 2 System.out.println(stack); // [2, 3, 4, 55, 17] System.out.println(Recursion.evenDigits(-1364035)); // 640 String expr = "(((1+2)*(3+1))+(1*(2+2)))"; Queue q = new LinkedList(); for (char ch: expr.toCharArray()) { q.add(ch); } System.out.println(Recursion.evaluate(q)); // 16 System.out.println(stack); // [2, 3, 4, 55, 17] Recursion.repeatStack(stack); System.out.println(stack); // [2, 2, 3, 3, 4, 4, 55, 55, 17, 17] Queue q2 = new LinkedList(); q2.add(34); q2.add(15); q2.add(0); Recursion.doubleElements(q2); System.out.println(q2); [68, 30, 0]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!