Write four programs one to solve each problem below These pr
Write four programs one to solve each problem below. These programs should use recursion to solve these problems. Test you programs with the given test data for each problem. Upload your source into blackboard.
Problem 1: maximum Write a recursive method to find the maximum value of an array of ints. If an empty array is given as input you should return Integer.MIN_VALUE. maximum(new int[] {1,5,4,3,2}) returns the int value 5. maximum(new int[] { }) returns Integer.MIN_VALUE (-2147483648).
Problem 2: reverse Write a recursive method that takes a String and returns the reversed String (characters in the opposite order). Feel free to create your own private, recursive method. Alternatively the String.substring and String.toChar methods will be helpful. reverse(\"abcde\") returns \"edcba\".
Problem 3: euler Write a recursive method that takes a double value x, and computes the function e^x using the infinite series e^x = 1 + (x^1)/1! + (x^2)/2! + (x^3)/3! + . . . euler(2) returns something close to 7.389056098930649. Due to round off error, the last several digits may differ slightly. Matching 6 digits is sufficient.
Problem 4: collatz Write a recursive method that calculates the length of the collatz sequence for a given input. The collatz sequence for an input may be calculated as follows. If the current value is even, the next value is that number divided by two (n/2). If the current value is odd, the next value is that number multiplied by three and then incremented (3n+1). This process continues until the next value is 1 For input = 3 the collatz sequence would be {3,10,5,16,8,4,2,1}. Only positive inputs (greater than 1) will be given! collatz(3) returns 8.
Solution
Problem 1: maximum Write a recursive method to find the maximum value of an array of ints. If an empty array is given as input you should return Integer.MIN_VALUE. maximum(new int[] {1,5,4,3,2}) returns the int value 5. maximum(new int[] { }) returns Integer.MIN_VALUE (-2147483648).
Function:
Problem 2: reverse Write a recursive method that takes a String and returns the reversed String (characters in the opposite order). Feel free to create your own private, recursive method.
FUNCTION:
public static String reverseGivenString(String s){
if(s.length() == 1){
return s;
} else {
reverse += s.charAt(s.length()-1) +reverseGivenString(s.substring(0,s.length()-1));
return reverse;
}
}
Problem 3: euler Write a recursive method that takes a double value x, and computes the function e^x using the infinite series e^x = 1 + (x^1)/1! + (x^2)/2! + (x^3)/3! + . . . euler(2) returns something close to 7.389056098930649. Due to round off error, the last several digits may differ slightly. Matching 6 digits is sufficient.
FUNCTION:
double expo( double x) { // x=2 , n=100
int n=100;
double sum = 1.0; // initialize sum of series
for (int i = n - 1; i > 0; --i )
sum = 1 + x * sum / i;
return sum;
}
OUTPUT: e(2) = 7.389056
Problem 4: collatz Write a recursive method that calculates the length of the collatz sequence for a given input. The collatz sequence for an input may be calculated as follows. If the current value is even, the next value is that number divided by two (n/2). If the current value is odd, the next value is that number multiplied by three and then incremented (3n+1). This process continues until the next value is 1 For input = 3 the collatz sequence would be {3,10,5,16,8,4,2,1}. Only positive inputs (greater than 1) will be given! collatz(3) returns 8.
COMPLETE JAVA CODE WITH FUNCTION:
import java.util.*;
public class HelloWorld{
public static void main(String[] args) {
System.out.print(\"Enter the number\ \");
Scanner in=new Scanner(System.in);
int n =in.nextInt();
int max=0, length=0;
for(int i=n; i>0; i--) {
int size = HelloWorld.getcollatz(n);
if(size>length) {
max = i;
length = size;
}
}
System.out.println( length);
}
private static final Map<Integer,Integer> pResults = new HashMap<>();
private static int getcollatz(int n) {
int ans = 1;
if(pResults.containsKey(n)) {
return pResults.get(n);
} else {
if(n==1) ans = 1;
else if(n%2==0) ans+= getcollatz(n/2);
else ans += getcollatz(3*n + 1);
pResults.put(n, ans);
return ans;
}
}
}

