Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 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) {}

} here are some hitsimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

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 possible

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions