Write the below following programs in Java not in other languages: Part 1: Recur
ID: 3685973 • Letter: W
Question
Write the below following programs in Java not in other languages:
Part 1: Recursive Conversion
Convert the following function to one that uses recursion.
void sign (int n)
{
while (n > 0)
cout << “No Parking ”;
n--;
}
Demonstrate the function with a driver program.
Part 2: isMember Function
Write a recursive Boolean function named isMember. The function should accept two arguments, an array and a value. The function should return true if the value is found in the array or false if the value is not found in the array. Demonstrate the function in a driver program.
Part 3: String Reverser
Write a recursive function that accepts a string object as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
Part 4:Ackermann's Function
Ackermann's function is a recursive mathematical algorithm that can be used to test how well a computer performs recursion. Write a method ackennann(m# n), which solves Acker-
mann's function. Use the following logic in your method:
If m = 0 then return n + I
If n = 0 then return ackermann(m — l, l)
Otherwise, return ackercrmann(n — 1, ackermanna(m, n — 1))
Test your method in a program that displays the return values of the following method calls
ackermann(O, 0) ackermann(0, 1) ackcermann(1, 1) ackermann(1, 2)
ackermann(l, 3) ackermann(2, 2) ackermann(3, 2)
Explanation / Answer
Answer for problems 1 and 2
public class RecursiveFunctions {
public static void main(String args[]) {
sign(5);
int arr[] = {1,5,9,0,3,35,56};
System.out.println(" 3 is in array:" + isMember(arr, 3));
System.out.println(" 55 is in array:" + isMember(arr, 55));
}
public static void sign (int n)
{
if (n > 0) {
System.out.println("No Parking ");
sign(n-1);
} else
return;
}
public static boolean isMember(int arr[], int value) {
if (arr.length > 0 && arr[0] == value)
return true;
else if(arr.length > 0) {
int subarr[] = new int[arr.length-1];
for(int i = 1; i < arr.length; i++)
subarr[i-1] = arr[i];
return isMember(subarr, value);
}
return false;
}
}
--output----------------
No Parking
No Parking
No Parking
No Parking
No Parking
3 is in array:true
55 is in array:false
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.