Please implement a java program below with these 3 different function of recusive method. Thanks
Write a recursive method that returns true if the String contains the char, otherwise returns false.
public boolean recursiveContains(char c, String s){
// Implement body
}
Write a recursive method that returns true if all the characters in the String are identical, otherwise false. If the String is empty, all the characters are identical.
public boolean recursiveAllCharactersSame(String s){
// Implement body
}
Write a recursive method that returns the number of characters in the String, which may be 0.
public int recursiveCount(char c, String s){
// Implement body
————————————————————————————–
public class Recursive{
public static double powerN(double x, int n){
if(n==0)
{
return 1;
}
else if(n==1)
{
return x;
}
else
return x*powerN(x,n-1);
}
public static double harmonicSum(int n){
if(n==0)
{
return 0;
}
else
{
return ((double)1/n)+harmonicSum(n-1);
}
}
public static void reverseDisplay(int value){
if (value <= 0)
return;
System.out.print(value % 10);
reverseDisplay(value / 10);
}
public static void reverseDisplay(String value){
if (value.length() == 0)
return;
System.out.print(value.substring(value.length() – 1));
reverseDisplay(value.substring(0, value.length()-1));
}
public static void main(String[] args) {
double m;
m = powerN(15,3);
System.out.println(m);
System.out.println(harmonicSum(1992));
reverseDisplay(123456789);
System.out.println();
reverseDisplay(“Norin Chea”);
}
}
Expert Answer
public boolean recursiveContains(char c, String s){
if (s == null || s.length() == 0) return false;
if (s.length() == 1) return s.charAt(0) == c;
if (s.charAt(0) == c) return true;
return recursiveContains(c, s.substring(1));
}
public boolean recursiveAllCharactersSame(String s){
if (s == null || s.length() <= 1) return true;
if (s.charAt(0) != s.charAt(1)) return false;
return recursiveAllCharactersSame(s.substring(1));
}
public int recursiveCount(char c, String s){
if (s== null || s.length() == 0) return 0;
if (s.length() == 1)
{
if (c == s.charAt(0)) return 1;
else return 0;
}
if (s.charAt(0) == c) return 1+recursiveCount(c, s.substring(1));
return recursiveCount(c, s.substring(1));
}