Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the following implementation in Java./* Given a string and a character,

ID: 3791116 • Letter: C

Question

Complete the following implementation in Java./* Given a string and a character, find the number of times the character * appears in the string. Matches are case-sensitive. * Give a recursive implementation. */public static int countChar(String str, char c) {return 0;} Complete the following implementation in Java./* Find the maximum value in a list of Integers, using recursion. * * which works a lot like cleanHotel(int lo, int hi) * use a recursive helper function: recursiveMaxHelper(List li, int lo, int hi) * */public static int recursiveMax(List li) {return 0;}

Explanation / Answer

Here is the code for you:

import java.util.*;
class RecursiveCountCharAndMax
{
/* Given a string and a character, find the number of times the character
* appears in the string. Matches are case-sensitive.
* Give a recursive implementation.
*/
public static int countChar(String str, char c)
{
if(str == "")
return 0;
else if(str.charAt(0) == c)
return 1 + countChar(str.substring(1), c);
else
return countChar(str.substring(1), c);
}
  
/* Find the maximum value in a list of Integers, using recursion.
*
* Hint: to keep track of which parts of the list still need to be visited,
* which works a lot like cleanHotel(int lo, int hi)
* use a recursive helper function: recursiveMaxHelper(List<Integer> li, int lo, int hi)
*
*/
public static int recursiveMax(List<Integer> li)
{
return recursiveMaxHelper(li, 0, li.size()-1);
}
public static int recursiveMaxHelper(List<Integer> li, int lo, int hi)
{
if(lo == hi)
return li.get(lo);
return li.get(lo) > recursiveMaxHelper(li, lo+1, hi) ? li.get(lo) : recursiveMaxHelper(li, lo+1, hi);
}
}