In: Computer Science
JAVA
Implement and test the following static recursive methods.
1) DigitCount – find the sum of the digits in an integer digitCount(12345) would be 5
2) Power2 - Efficiently compute exponentiation for non-negative exponents by the following recursive algorithm:
bx = 1 if x == 0
bx = (b(x/2))2 if x is even bx = b * (b(x-1)) if x is odd
3) Write a recursive method isBackwards that has two String parameters and returns true if the two Strings have the same sequence of characters but in the opposite order (ignoring white space and capitalization), and returns false otherwise. The method should throw an IllegalArgumentException if either String is null.
Test this method with following examples, isBackwards("Fried", "deirf") -> true isBackwards("Sit", "Toes") -> false isBackwards("", "") -> true
isBackwards("I madam", "mad am I") -> true
/*Java test program to test the three functions , DigitCount , power2 and isBackwrards and then print the results on the console output*/
//RecursiveFunctions.java
public class RecursiveFunctions
{
public static void main(String[] args)
{
int n=12345;
//Calling DigitCount method
System.out.println("Digit
count of "+n+" is "+DigitCount(n));
int x=5;
//Calling power2 method
System.out.println("2
^"+x+" = "+ power2(x));
String first="Fried";
String second="deirf";
System.out.println(first);
System.out.println(second);
first=first.toLowerCase().trim();
second=second.toLowerCase().trim();
//Calling isBackwards method
if(isBackwards(first,second))
System.out.println("Equal.");
else
System.out.println("Not eqaul");
System.out.println();
first="I madam";
second="mad am I";
System.out.println(first);
System.out.println(second);
first=first.toLowerCase().trim();
second=second.toLowerCase().trim();
//Calling isBackwards method
if(isBackwards(first,second))
System.out.println("Equal.");
else
System.out.println("Not eqaul");
System.out.println();
first="Sit";
second="Toes";
System.out.println(first);
System.out.println(second);
first=first.toLowerCase().trim();
second=second.toLowerCase().trim();
if(isBackwards(first,second))
System.out.println("Equal.");
else
System.out.println("Not eqaul");
}
/* 3. Recursive method isBackwards that
takes left and right two string variables
* and finds if they are equal in reverse of right is
equal to left
* otherwise returns false*/
public static boolean isBackwards(String left,String
right)
{
//Replace whitespaces with ""
string
left =
left.toLowerCase().trim().replace(" ", "");
right =
right.toLowerCase().trim().replace(" ", "");
//Returns true if both are
emoty
if(left.isEmpty()&&
right.isEmpty())
return
true;
//Throws if any of the
string is null
else if(left==null ||
right==null)
throw new
IllegalArgumentException();
//Get the first character and last
character of right string is equal
else
if(left.charAt(0)==right.charAt(right.length()-1))
{
//call the
isBackwards method with substring which includes the string except
the first character in left string
//and the
substring of the right which excludes the last character.
return
isBackwards(left.substring(1), right.substring(0,
right.length()-1));
}
else
//otherwise
return false
return
false;
}
/*2. Recursive method that takes a
power,x
* and raise the 2 to the power of x*/
public static int power2(int x)
{
if (x != 0)
//calling
recursive method,power2
return
(2*power2(x-1));
else
return 1;
} //end of the method,power2
/*1. Recursive method to find the sum of
the */
public static int DigitCount (int n)
{
if (n == 0)
return 0;
return 1+ DigitCount(n / 10);
} //end of the method,DigitCount
}
Sample Output:
Digit count of 12345 is 5
2 ^5 = 32
Fried
deirf
Equal.
I madam
mad am I
Equal.
Sit
Toes
Not eqaul