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

You will create a Java Project in Edipse with the name: hw7_flastname You will h

ID: 3685093 • Letter: Y

Question

You will create a Java Project in Edipse with the name: hw7_flastname You will have two packages: prob 1. prob2 You will write a number of recursive methods as described below. Make sure your method names, parameters, and returns types exactly match the descriptions as your code in mom will be replaced for testing. Do the following Create a probl package and a class named RecunionTester. Define the following static methods in RecursionTester Consider this function: power(x.n) = x degree + x^2 + x^4 +... + x^m. where m Is the largest even power that is less than or equal to n. For example: power(3.5) = 3 degree + 3^2 + 3^4. Note, this would give the same result as power(3.4). Write a recursive static method to evaluate this function for given m and n. Write a recursive static method named revcncStrmg that accepts an integer and returns the integer as a string In reverse order (without using any String or Character methods) For example. 4295 would be returned as *5924'. Write a recursive static method named countOccurrences that accepts a string, sfr and a character, key. The method returns the number of occurrences of key in str. For example: countOccurrences("enzeme entente off, V) returns 6. You are required to write an efficient method [i.e. you must use a helper method). Write a recursive static method named moke Pohndrome that accepts a string and returns a palindrome of that string. For example. makePalmdrome( 'abe" ) returns "abeeba". You may use the substring function (ie. you don't have to use a helper).

Explanation / Answer

C. Recursively reverse in a string :

import java.io.*;

import java.util.*;

public class StringRecursiveReversal {

    String reverse = "";

     

    public String reverseString(String str){

         

        if(str.length() == 1){

            return str;

        } else {

            reverse += str.charAt(str.length()-1)

                    +reverseString(str.substring(0,str.length()-1));

            return reverse;

        }

    }

     

    public static void main(String a[]){

        StringRecursiveReversal srr = new StringRecursiveReversal();

        System.out.println("Result: "+srr.reverseString("4295"));

    }

}

D. Recursively counting character occurrences in a string :

import java.io.*;

import java.util.*;

E. Recursve functon for Makepalindrome:

import java.io.*;

import java.util.*;

public class palindrome

{

static Scanner myScanner;

public static void main (String args[]) throws Exception

{

System.out.println("Hello! This is a program that creates palindromes.");

System.out.print("Would you like to try?: ");

while(promptUser())

{

try

{

getPalindrome(s);

printPalindrome();

}

catch (IOException exception)

{

System.out.println("Sorry, those arent words.");

}

}

System.out.print("Goodbye!");

}

static boolean promptUser()

{

myScanner = new Scanner(System.in);

String input = myScanner.nextLine();

if (input.charAt(0) == 'y')

{

return true;

}

else if

(input.charAt(0) == 'n')

{

return false;

} return false;

}

static String getPalindrome(String s)

{

myScanner = new Scanner(System.in);

System.out.print("Please enter a word: ");

s = myScanner.nextLine();

if (s.length() <= 1)

{

return s;

}

else

return (s.substring(1,s.length() - 1) + s.charAt(0)) + getPalindrome(s);

}

static void printPalindrome()

{

System.out.println(getPalindrome(s));

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote