Using Java: 1. Recursive Multiplication Write a recursive function that accepts
ID: 3827072 • Letter: U
Question
Using Java:
1. Recursive Multiplication
Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows:
5×6=6+6+6+6+6
2. Recursive findings
Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in the array. Demonstrate the method in a program.
3. String Reverse
Write a recursive method that accepts a string as its argument and prints the string in reverse order. Demonstrate the method in a program.
4. maxElement Method
Write a method named maxElement, which returns the largest value in an array that is passed as an argument. The method should use recursion to find the largest element. Demonstrate the method in a program.
5. Recursive Power Method
Write a method that uses recursion to raise a number to a power. The method should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer. Demonstrate the method in a program.
6. Sum of Numbers
Write a method that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the method will return the sum of 1, 2, 3, 4, . . . 50. Use recursion to calculate the sum. Demonstrate the method in a program.
Explanation / Answer
HI, I have implemented first functions.
Please repost others in separate post.
Please let me know in case of any issue in first 3.
public class RecursiveFunctions {
public static int recursivemultiplication(int x, int y){
// making y positive
if(y < 0)
return recursivemultiplication(-x, -y);
// base case
if(y == 0)
return 0;
// recursive case
return x + recursivemultiplication(x, y-1);
}
public static boolean reFinding(int arr[], int start, int key){
// base case
if(start >= arr.length)
return false;
if(arr[start] == key)
return true;
return reFinding(arr, start+1, key);
}
public static void reString(String s, int index){
if(index < 0)
return;
System.out.print(s.charAt(index));
reString(s, index-1);
}
public static void main(String[] args) {
System.out.println("5*7 : "+recursivemultiplication(5, 7));
int arr[] = {55,3,12,65,22,9};
System.out.println("22 is avaiulable ? "+reFinding(arr, 0, 22));
String s = "pk this is";
reString(s , s.length()-1);
}
}
/*
Sample run:
5*7 : 35
22 is avaiulable ? true
si siht kp
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.