Question

In: Computer Science

JAVA Implement and test the following static recursive methods. 1) DigitCount – find the sum of...

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

Solutions

Expert Solution

/*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


Related Solutions

Problem 3: (a) Implement a sublinear running time complexity recursive function in Java public static long...
Problem 3: (a) Implement a sublinear running time complexity recursive function in Java public static long exponentiation(long x, int n) to calculate x^n. Note: In your function you can use only the basic arithmetic operators (+, -, *, %, and /). (b) What is the running time complexity of your function? Justify. (c) Give a number of multiplications used by your function to calculate x^63. Important Notes: • For the item (a): o You must add the main method in...
Using Python Implement Recursive Selection Sort with the following recursive method. 1. Find the smallest number...
Using Python Implement Recursive Selection Sort with the following recursive method. 1. Find the smallest number in the list and swaps it with the first number. 2.Ignore the first number and sort the remaining smaller list recursively.
IN JAVA. I have the following code (please also implement the Tester to test the methods...
IN JAVA. I have the following code (please also implement the Tester to test the methods ) And I need to add a method called public int remove() that should remove the first integer of the array and return it at the dame time saving the first integer and bring down all other elements. After it should decrease the size of the array, and after return and save the integer. package ourVector; import java.util.Scanner; public class ourVector { private int[]...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will accept a variable length parameter list of Strings and concatenate them into one string with a space in between and return it. 2. Overload this method with two parameters, one is a boolean named upper and one is a variable length parameter list of Strings. If upper is true, return a combined string with spaces in upper case; otherwise, return the combined string as...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants Create a class Die representing a die to roll randomly. ☑ Give Die a public final static int FACES, representing how many faces all dice will have for this run of the program. ☑ In a static block, choose a value for FACES that is a randomly chosen from the options 4, 6, 8, 10, 12, and 20. ☑ Give Die an instance variable...
Program in java 1- Implement an algorithms to calculate the sum of all numbers in an...
Program in java 1- Implement an algorithms to calculate the sum of all numbers in an array.   2- Implement an algorithm to determine if an array has all unique integer values. 3- Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of nums. Example 1: Input: nums = [1,2,3,4] Output: [1,3,6,10] Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
In Java For this assignment you will implement two methods, find() and replace() relating to Binary...
In Java For this assignment you will implement two methods, find() and replace() relating to Binary Search Trees. These methods are both to be recursive functions. The signatures for those functions must be: /* This method takes a tree node and a key as an argument and returns the tree node if the key is found and returns null if the key is not found. */ BST find(BST T, char key) /* This method takes a tree node and a...
Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...
Consider the following recursive method in Java public static int mystery(int n) {   if (n == 0)   return 1;    else    return 4 * mystery (n - 1);   } What is the output of  mystery (3) using the code segment above Show your work on your trace file
In java, Write a recursive function to calculate the sum of the nodes only on even...
In java, Write a recursive function to calculate the sum of the nodes only on even levels of the subtree. please do not add any parameters to do this function. private int sumEvenLevels(Node current){ //you can only pass in root. //code }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT