Problem: With Java, For this lab, you will write a class called RecursionToolkit
ID: 3916602 • Letter: P
Question
Problem:
With Java,
For this lab, you will write a class called RecursionToolkit. In it are a number of recursive methods. You will then use a main method to test your code with the provided test information. If you are particularly savvy, you could write these as JUnit Test Cases (and will get a bit of extra credit as thanks for making my job easier).
The methods, and their test cases, follow:
(1) Harmonic Series
Write a method to calculate the nth term of the Harmonic Series. The series is defined as:
As an argument, your method should take n, corresponding to which “nth” term of the series the user wants. The return value is the result of the series up to that n. If the number is zero or negative, the result should be the integer 1 as a way of error handling.
Examples:
harmSer(5) should return 2.283333333333333
harmSer(2) should return 1.5
harmSer(20) should return 3.597739657143682
harmSer(-5) should return 1
(2) Sum Digits
Write a method to sum all the digits in an integer. Return 0 if the input is negative.
sumDigits(1234) should return 10
sumDigits(9) should return 9
sumDigits(5032134) should return 18
sumDigits(-50) should return 0
(3) Recursive Sequential Search
Write a method to recursively search an array of Strings for a given target, using sequential search. Each call is only concerned with searching the current element – otherwise, a recursive call (or return) should be made.
Return a -1 if the element is not found, and the index of the element if the element is found
Searching the array {“Apple”, “Orange”, “Grapefruit”, “Pear”} for “Orange,” should return the integer 1. Searching for “Pineapple,” should return -1
(4) String Cleaning
Write a method for which, given a String, will clean redundant characters. That, characters that are the same and are neighbors should be reduced to one character.
For example, using the String “yyzzza” as an argument should return “yza” and “Hello” should return “Helo”.
Explanation / Answer
import java.util.Arrays;
class Main {
public static String Cleaning(String word)
{
// base case is to return the first character
if(word.length() == 1)
return word.charAt(0)+"";
// when the last two characters are same, recursively calling without last chracter
if(word.charAt(word.length()-1) == word.charAt(word.length()-2))
return Cleaning(word.substring(0, word.length()-1));
// otherwise it comes here to consier the last character for output and recursively calls
return Cleaning(word.substring(0, word.length()-1))+ word.charAt(word.length()-1);
}
public static int Sequential_Search(String[] arr, String word)
{
// base case is to return -1
if(arr.length == 0)
return -1;
// when word is found, returning index
if(arr[arr.length-1] == word)
return arr.length-1;
// recursively searching without last string in the array
return Sequential_Search(Arrays.copyOfRange(arr, 0, arr.length-1), word);
}
public static int sumDigits(int n)
{
// base case is to return 0
if(n<=0)
return 0;
// taking the last digit and adding it to result and recursivley calling without last digit
return (n%10) + sumDigits(n/10);
}
public static double harmSer(int n)
{
// base case is to return 1
if(n <= 1)
return 1;
// recursively calling with one less n
return (1/(float)n) + harmSer(n-1);
}
public static void main(String[] args) {
// sample runs
System.out.println(harmSer(5));
System.out.println(harmSer(2));
System.out.println(harmSer(20));
System.out.println(harmSer(-5));
System.out.println();
System.out.println(sumDigits(1234));
System.out.println(sumDigits(9));
System.out.println(sumDigits(5032134));
System.out.println(sumDigits(-50));
System.out.println();
System.out.println(Sequential_Search(new String[]{"Apple", "Orange", "Grapefruit", "Pear"}, "Orange"));
System.out.println(Sequential_Search(new String[]{"Apple", "Orange", "Grapefruit", "Pear"}, "PineApple"));
System.out.println();
System.out.println(Cleaning("yyzzza"));
System.out.println(Cleaning("Hello"));
}
}
/* SAMPLE OUTPUTS
2.283333346247673
1.5
3.597739700227976
1.0
10
9
18
0
1
-1
yza
Helo
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.