Question
Java code please import java.util.Queue; import java.util.Stack; public class Recursion { /** * Write a recursive function that finds the index of s2 in s1.
Java code please
import java.util.Queue; import java.util.Stack;
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
/** * 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
/** * 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
/** * Write a recursive function that accepts a Queue
} here are some hits
Helper Functions In this class we always strive to solve problems in the simplest way possible. As such, if you are using helper functions when they aren't required, you may be deducted points. Though if that is the only way to solve it, then still submit that solution since you will gain some points for it. There is no penalty for using a helper function for doubleElements. Testing You are responsible for testing your code (as it should be). The testing code that you will write is in Recursion TestClass.java. We have given you one example of the format of a test. You can also refer to any of the drills, we are using the same style of testing as is done for the drills. Remember you need to test the normal case, and the edge cases. What are all the weird (but valid) inputs that you might receive? What sorts of inputs might cause issues with your code? . For the evaluate function, you might find Character.isDigit(char ch) and Character.getNumericValue(char ch) helpful. For the doubleElements function, it might be useful to have another parameter. This might mean creating a helper function. The expressions that are passed into the evaluate function have to follow a strict pattern. For instance, ((4)+(3)) is not a valid expression since there are parenthesis around just a single number. (1+3+5)* 3) is also invalid since there are spaces and/or there are three values inside one set of parentheses. Lastly you are guaranteed that every expression must be in parenthesis. 1+5 is not valid, (1+5) is valid. To put it as simply as possible, expressions must be of the form (expr op expr) without spaces, but I needed to provide spaces to make it clear. expr = expression and op = operation. Should use only static methods. Each static method should be short and concise. A method should only perform one task. If you have a method that is performing multiple tasks, break it into smaller meth- ods. We think a good rule of thumb for method length is 30 lines. Make things as simple as possible. This means avoiding nested loops, nested condi- tionals, and too many levels of user-defined methods calling other user-defined method (chaining) whenever possible. Helper Functions In this class we always strive to solve problems in the simplest way possible. As such, if you are using helper functions when they aren't required, you may be deducted points. Though if that is the only way to solve it, then still submit that solution since you will gain some points for it. There is no penalty for using a helper function for doubleElements. Testing You are responsible for testing your code (as it should be). The testing code that you will write is in Recursion TestClass.java. We have given you one example of the format of a test. You can also refer to any of the drills, we are using the same style of testing as is done for the drills. Remember you need to test the normal case, and the edge cases. What are all the weird (but valid) inputs that you might receive? What sorts of inputs might cause issues with your code? . For the evaluate function, you might find Character.isDigit(char ch) and Character.getNumericValue(char ch) helpful. For the doubleElements function, it might be useful to have another parameter. This might mean creating a helper function. The expressions that are passed into the evaluate function have to follow a strict pattern. For instance, ((4)+(3)) is not a valid expression since there are parenthesis around just a single number. (1+3+5)* 3) is also invalid since there are spaces and/or there are three values inside one set of parentheses. Lastly you are guaranteed that every expression must be in parenthesis. 1+5 is not valid, (1+5) is valid. To put it as simply as possible, expressions must be of the form (expr op expr) without spaces, but I needed to provide spaces to make it clear. expr = expression and op = operation. Should use only static methods. Each static method should be short and concise. A method should only perform one task. If you have a method that is performing multiple tasks, break it into smaller meth- ods. We think a good rule of thumb for method length is 30 lines. Make things as simple as possible. This means avoiding nested loops, nested condi- tionals, and too many levels of user-defined methods calling other user-defined method (chaining) whenever possibleStep 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