Question
please help with this recursion problem. Recursion.java public class Recursion { /** * Returns the value of x * y, computed via recursive addition. *
please help with this recursion problem.
Recursion.java
public class Recursion {
/** * Returns the value of x * y, computed via recursive addition. * x is added y times. Both x and y are non-negative. * @param x non-negative integer multiplicand 1 * @param y non-negative integer multiplicand 2 * @return x * y */ public static int recursiveMultiplication(int x, int y) { // TODO }
/******************************************************************************/ /** * Reverses a string via recursion. * @param s the non-null string to reverse * @return a new string with the characters in reverse order */ public static String reverse(String s) { // TODO }
/******************************************************************************/ private static int maxHelper(int[] array, int index, int max) { // TODO }
/** * Returns the maximum value in the array. * Uses a helper method to do the recursion. * @param array the array of integers to traverse * @return the maximum value in the array */ public static int max(int[] array) { return maxHelper(array, 0, Integer.MIN_VALUE); }
/******************************************************************************/
/** * Returns whether or not a string is a palindrome, a string that is * the same both forward and backward. * @param s the string to process * @return a boolean indicating if the string is a palindrome */ public static boolean isPalindrome(String s) { // TODO }
/******************************************************************************/ private static boolean memberHelper(int key, int[] array, int index) { // TODO }
/** * Returns whether or not the integer key is in the array of integers. * Uses a helper method to do the recursion. * @param key the value to seek * @param array the array to traverse * @return a boolean indicating if the key is found in the array */ public static boolean isMember(int key, int[] array) { return memberHelper(key, array, 0); }
/******************************************************************************/ /** * Returns a new string where identical chars that are adjacent * in the original string are separated from each other by a tilde '~'. * @param s the string to process * @return a new string where identical adjacent characters are separated * by a tilde */ public static String separateIdentical(String s) { // TODO } }
RecursionTestCases.java
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test;
public class RecursionTestCases {
@Test public void testRecursiveMultiplication01() { assertEquals(0, Recursion.recursiveMultiplication(4, 0)); }
@Test public void testRecursiveMultiplication02() { assertEquals(0, Recursion.recursiveMultiplication(0, 3)); }
@Test public void testRecursiveMultiplication03() { assertEquals(3, Recursion.recursiveMultiplication(3, 1)); }
@Test public void testRecursiveMultiplication04() { assertEquals(48, Recursion.recursiveMultiplication(12, 4)); }
@Test public void testRecursiveMultiplication05() { assertEquals(1111, Recursion.recursiveMultiplication(101, 11)); }
@Test public void testReverse01() { assertEquals("", Recursion.reverse("")); }
@Test public void testReverse02() { assertEquals("I", Recursion.reverse("I")); }
@Test public void testReverse03() { assertEquals("123", Recursion.reverse("321")); }
@Test public void testReverse04() { assertEquals(".erots eht ot tnew maS", Recursion.reverse("Sam went to the store.")); }
@Test public void testReverse05() { assertEquals("!raeY weN yppaH", Recursion.reverse("Happy New Year!")); }
@Test public void testMax01() { assertEquals(12, Recursion.max(new int[] {12})); }
@Test public void testMax02() { assertEquals(3, Recursion.max(new int[] {0, 1, 3, 2})); }
@Test public void testMax03() { assertEquals(-1, Recursion.max(new int[] {-2, -3, -1, -12, -15})); }
@Test public void testMax04() { assertEquals(Integer.MAX_VALUE, Recursion.max(new int[] {Integer.MAX_VALUE, 20000, Integer.MAX_VALUE, 9, Integer.MIN_VALUE})); }
@Test public void testMax05() { assertEquals(10, Recursion.max(new int[] {1, 3, 4, 6, 5, 10, 9, 8, 7, 2})); }
@Test public void testIsPalindrome01() { assertTrue(Recursion.isPalindrome("")); }
@Test public void testIsPalindrome02() { assertTrue(Recursion.isPalindrome("a")); }
@Test public void testIsPalindrome03() { assertTrue(Recursion.isPalindrome("racecar")); }
@Test public void testIsPalindrome04() { assertFalse(Recursion.isPalindrome("racefcar")); }
@Test public void testIsPalindrome05() { assertTrue(Recursion.isPalindrome("saippuakivikauppias")); }
@Test public void testIsMember01() { assertFalse(Recursion.isMember(13, new int[] {12})); }
@Test public void testIsMember02() { assertTrue(Recursion.isMember(3, new int[] {0, 1, 3, 2})); }
@Test public void testIsMember03() { assertFalse(Recursion.isMember(-11, new int[] {-2, -3, -1, -12, -15})); }
@Test public void testIsMember04() { assertTrue(Recursion.isMember(Integer.MIN_VALUE, new int[] {Integer.MAX_VALUE, 20000, Integer.MAX_VALUE, 9, Integer.MIN_VALUE})); }
@Test public void testIsMember05() { assertTrue(Recursion.isMember(10, new int[] {1, 3, 4, 6, 5, 10, 9, 8, 7, 2})); }
@Test public void testSeparateIdentical01() { assertEquals("", Recursion.separateIdentical("")); }
@Test public void testSeparateIdentical02() { assertEquals("A", Recursion.separateIdentical("A")); }
@Test public void testSeparateIdentical03() { assertEquals("noadjacentchars", Recursion.separateIdentical("noadjacentchars")); }
@Test public void testSeparateIdentical04() { assertEquals("AB~BAC~C~CAB~BA", Recursion.separateIdentical("ABBACCCABBA")); }
@Test public void testSeparateIdentical05() { assertEquals("~~~0~0123~~~4~~~3210~0~~~~~", Recursion.separateIdentical("~~00123~~4~~32100~~~")); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Lets fill in the TODOs in the Recursionjava file public class Recursion public static ...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