in eclipse Write a program, RecursiveFinder, that has a static method int larges
ID: 3824114 • Letter: I
Question
in eclipse
Write a program, RecursiveFinder, that has a static method int largestElement(int []). In that method, using recursion, find the largest element in an array. You may not use/need any loops or any static variables for finding the largest element. If you need to copy arrays for your solution, consider the helper method in the Arrays class for doing so (link here) or you may use a loop to create a new array (but not to determine the answer). In the main method of RecursiveFinder, test this method using at least three arrays showcasing that your method can handle different properties like size and order of elements Write a program, Reverser, that has a static recursive method string reverse (string text) that reverses a string. For example, reverse ("Hello!") returns the string "!olleH". Implement a recursive solution by removing the first character, reversing the remaining text, and combining the two (reversed remaining text + first removed character). In the main method of Reverser, test this method by using at least three Strings of varying complexity and size. Feel free to model your code after the examples we did in class.Explanation / Answer
[1]
public class findMax {
private static int[] a = { 5, 6,10,3, 5, 2, 8, 4, 9 };
public static void main(String[] args) {
int result = LargestElement(0);
System.out.printf("Max element = %d", result);
}
public static int LargestElement(int i) {
// the anchor of the recursive method
if (i == a.length)
return Integer.MIN_VALUE;
return Math.max(a[i], LargestElement(i + 1));
}
}
[2]
public class RecursiveReversal {
static String rev = "";
public static String reverse(String str){
if(str.length() == 1){
return str;
} else {
rev += str.charAt(str.length()-1)
+reverse(str.substring(0,str.length()-1));
return rev;
}
}
public static void main(String a[]){
System.out.println("Result: "+reverse("Hello!"));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.