Unit 10 Write Code for Toggle Code

This is the write code problems associated with the mixed up code problems. Remember to use recursion!

Finish the reverse method. This should take in a parameter myText and return a reversed version of it. For example, reverse("Cat") would return "taC". Fill in the missing code (labeled with YOUR CODE HERE comments) to complete the problem.

Fill in the multiply method. It should take in two non-negative integers and return their product, using the fact that multiplication is repeated addition (e.g., 3x4 = 3 + 3 + 3 + 3). Thus, multiply(3, 4) would return 12. Do NOT use multiplication; only use addition.

Write the sumElements method. It should add up all of the elements in the passed-in array from index onward - so calling sumElements(nums, 1) with nums as {1,2,3,12} would return 17 (as that is 2 + 3 + 12). Be sure to use recursion when creating the method.

Finish the removeStar method. It should remove any occurrence of an asterisk (“*”) from a passed-in string. For example, calling removeStar on "ab*c**d" would return "abcd".

Write the exponent program. This should take in two int parameters - base and power - and return base ^ power (base multiplied by itself power times). For example, exponent(3, 5) would return 243 because that is 3x3x3x3x3.

Write the numFiveOccurrence method. It should return the number of times that 5 is present in the passed int array arr. Just as in problem three (sumElements), there will also be an index parameter to make recursion possible. The initial call to numFiveOccurrence will be with index 0, and, from then on, numFiveOccurrence(arr, index) should return the number of 5s in arr from index onward.

Write the repeatThis method. It should take in two parameters - a String s and an int i - and return a new String composed of s i times. For example, repeatThis("Cat", 2) would return "CatCat".

Write the findNumX method. This should take in a String s and return the number of occurrences of the character 'x' (NOT including 'X'). For example, findNumX("axbcx") would return 2.

Write the countTo method. This should take in an integer x and return a String with the positive numbers from 1 to x (inclusive) with “…” after each. For example, countTo(5) would return “1…2…3…4…5…”.

Write the displayEvenDigits method. It should take in an integer num and return a String version of num with the odd digits replaced by '_'. For example, displayEvenDigits(42356) should return "42__6". To achieve this recursively, you should use modulo and division to get the least-significant digit and then pass a version of num without that digit (hint: use integer division).

Write the fibonacci method. This program should take in an integer n and return the n``th fibonacci number. The 0th fibonacci number is ``0 and the 1st is 1. From then on, the n``th fibonacci number is the ``n-1``th fibonacci number + the ``n-2``th fibonacci number. For example, the first few fibonacci numbers are 0, 1, 1, 2, 3, 5, 8. ``fibonacci(4) should return 3, as that is the 4th fibonacci number (remember that 0 is the 0th!). fibonacci(6) should return 8, as that is the 6th fibonacci number.

Create the spaceDash method. It should take in a String str and return a new String that has all of the spaces in str replaced by dashes.

Write the numberOf2s method. It should take in an integer n and count the number of 2s in the digits. Try to do this without converting n to a String. Here’s a hint: modulo and integer division will both be very useful.

Write the sum method. It should take in an integer n and recursively find and return the sum of the digits of n. For example, sum(362) would return 11, as that is 3+6+2.

Write the evenDigits method. This should take in an integer n and recursively return the number of even digits in n.

Write the factorial method. This should take in an integer n and return the factorial of n. The factorial of N is equal to N x (N - 1) x (N - 2)… x 2 x 1. Note that N times the factorial of (N - 1) would be N factorial.

You have attempted of activities on this page