You are on page 1of 3

Java Programming (CodeLab)

1. Write a static method, getBigWords, that gets a String parameter and returns an array whose elements are the words in the parameter that contain more than 5 letters. (A word is defined as a contiguous sequence of letters.) So, given a String like "There are 87,000,000 people in Canada", getBigWords would return an array of two elements, "people" and "Canada".

2. Write a method called

fact that recursively calculates the factorial value of its single fact is a long ..

int

parameter. The value returned by

3. Two non-negative integers x and y are equal if either: Both are 0, or x-1 and y-1 are equal Write a boolean -method named equals that recursively determines

whether its two int parameters are equal and returns true if they are and false otherwise.

4. The sum of the numbers from 1 to n can be defined recursively as follows: The sum from 1 to 1 is 1. The sum from 1 to n is n more than the sum from 1 to n-1. Write a int -method named sum

that accepts an int parameter, n, and recursively calculates and returns the sum of the numbers from 1 to n. .

5. Write a recursive, int-valued method, len, that accepts a string and returns the number of characters in the string. The length of a string is: 0 if the string is the empty string (""). 1 more than the length of the rest of the string beyond the first character.

6. Write a recursive, string-valued method, reverse, that accepts a string and returns a new string consisting of the original string in reverse. For example, calling reverse with the string goodbye returns the string eybdoog. Reversing a string involves: Nothing if the string is empty or has only 1 character (reversing a single character string does not change anything) Otherwise concatenate the last character with the result of reversing the string consisting of the second through the next-to-last character, followed by the first character. In the above example, you would concatenate the 'e' (last character of goodbye) with the result of calling reverse on oodby (the string from the second character to the next-to-last), with the 'g' (first character).

7. The Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, 21, has as its first 2 values, 0 and 1; each successive value if then calculated as the sum of the previous two values. The first element in the series is the 0'th element, thus the value 8 is element 6 of the series. The n'th element of the series, written as fib(n), is thus defined as: n if n = 0 or n = 1 fib(n-1) + fib(n-2) Write the int-valued method fib, that takes a single int parameter (say n), and

recursively calculates and then returns the n'th element of the Fibonacci series.

8. Write a recursive, boolean-valued method, containsVowel, that accepts a string and returns true if the string contains a vowel. A string contains a vowel if: The first character of the string is a vowel, or The rest of the string (beyond the first character) contains a vowel

9. Write a recursive, string-valued method, replace, that accepts a string and returns a new string consisting of the original string with each blank replaced with an asterisk (*) Replacing the blanks in a string involves: Nothing if the string is empty Otherwise: If the first character is not a blank, simply concatenate it with the result of replacing the rest of the string If the first character IS a blank, concatenate an * with the result of replacing the rest of the string.

10. A palindrome is a string that reads the same forwards or backwards; for example dad, mom, deed (i.e., reversing a palindrome produces the same string). Write a recursive, boolean-valued method, isPalindrome that accepts a string and returns whether the string is a palindrome. A string, s, is a palindrome if: s is the empty string or s consists of a single letter (which reads the same back or forward), or the first and last characters of s are the same, and the rest of the string (i.e., the second through next-to-last characters) form a palindrome.

You might also like