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

***NOTE*** THREE ACTIVITIES IN ACTIVITY ONE, PLUS ADD PSEUDOCODE FOR THE THREE R

ID: 3687802 • Letter: #

Question

***NOTE***

THREE ACTIVITIES IN ACTIVITY ONE, PLUS ADD PSEUDOCODE FOR THE THREE RECURSIONS

ACTIVITY TWO ADD CODE TO THE ONE IN ACTIVITY THREE AND PROVIDE A TABLE FOR THE NUMBERS GIVEN IN ACTIVITY TWO

Activity 1. A collection of short methods.

CHANGE JAVA CODE TO RECURSIVE ON ALL 3 ACTIVITIES

PROVIDE PESUDOCODE

PROVIDE TABLE FOR ACTIVITY TWO

Change iteritain to RECURSION JAVA

#1 CHANGE TO RECURSION AND PROVIDE PSEUDOCODE

public static long FibonacciIter(int n) {

        long fn0=0, fn1=1;

        long result=0;

        for (int i=2; i<=n; i++) {

            result=fn0+fn1;

            fn0=fn1;

            fn1=result;

        }return result;   

#2 CHANGE TO RECURSION AND PROVIDE PSEUDOCODE

public static boolean checkPalindromeIter(String str) {

        for (int i=0; i<str.length()/2;i++) {

            if (str.charAt(i)!=str.charAt(str.length()-1-i))                return false;  

        }

        return true;

    }

#3 CHANGE TO RECURSION AND PROVIDE PSEUDOCODE

Note: You can access the ascii table at: http://www.asciitable.com.

public static String DecipherIter(String str, int shift) {

        String result = "";

        int code;

        for (int i=0; i<str.length(); i++) {

           code = (int) str.charAt(i)+shift;

           result = result + (char) code;   

        }

       

        return result;   

Activity 2. A Glimpse at Algorithm Performance. In this activity, you will have to go back to Functionality 1 from Activity 1 (Fibonacci numbers). For this functionality, you will have to report the amount of time it takes to run each of the two methods (iterative, recursive) for increasing rank of the Fibonacci number to be computed.

Note: to measure the time it takes to run a given piece of code, you have to insert the following code in each of your methods. It is already in the java file, in the main method.

long startTime = System.currentTimeMillis();

// ... your program...

long endTime   = System.currentTimeMillis();

long totalTime = endTime - startTime;

System.out.println(totalTime);

Instructions:

Execute both your Fibonacci methods for increasing values of the input n. E.g., n=1, 5, 10, 20, 50, 100, 500, 1000.

In your file named “yourLastName-yourFirstName-lab8.docx”, report the execution times in a clear table and explain the results you observe.

long startTime = System.currentTimeMillis();

// ... your program...

long endTime   = System.currentTimeMillis();

long totalTime = endTime - startTime;

System.out.println(totalTime);

Explanation / Answer

This question has multiple sub-parts. Please post one more question for the same.

1)

recursive code will be:

public static long FibonacciIter(int n) {
   if (n == 0) return 0;
if (n<= 2) return 1;

long result = FibonacciIter(n - 1) + FibonacciIter(n - 2);
return result;
}

2)

recursive code will be:

public static boolean checkPalindromeIter(String str) {
if(str.length() == 0 || str.length() == 1)
return true;
if(str.charAt(0) == str.charAt(str.length()-1))
return checkPalindromeIter(str.substring(1, str.length()-1));
return false;
}