Question: Using Java, take the array recursion project and add the following recursive array methods to it. Add code to the main method to test each
Using Java, take the array recursion project and add the following recursive array methods to it. Add code to the main method to test each of these methods.
1. A method to rotate n elements in the array to the left. If n = 5, only the first 5 elements in the array rotate left.
2. A method to rotate n elements in the array to the right. If n = 5, only the first 5 elements in the array rotate right.
3. A method to determine if the first n elements in an array are already sorted. The method should return true if it is already sorted and false if it isn't already sorted. (This method will not actually do the sorting.)
Recursion Project:
class Recursion{ private int[] a; public Recursion(int[] array) { //Constructor a = array; } public void printF(int n) { //Print Array Forward if(n > 0) { printF(n-1); System.out.print(a[n - 1]); } } public void printB(int n) { //Print Array Backwards if(n>0){ System.out.print(a[n-1]); printB(n-1); } } public int maxA(int n) { //Largest Array Element if(n==1) return a[0]; return Math.max(maxA(n-1), a[n-1]); } public int minA(int n) { //Smallest Array Element if(n==1) return a[0]; return Math.min(minA(n-1), a[n-1]); } public void reverseA(int l, int r) { //Reverse Array if(l
public class JJL8 { public static void main(String[] args) { int[] a = {1,2,3,4,5,6,7}; Recursion r = new Recursion(a); r.printF(5); System.out.println(); r.printB(5); System.out.println(); System.out.println(r.maxA(5)); System.out.println(r.minA(5)); r.reverseA(1, 5); System.out.println(""); int[] b = {1,1,2,2,3,4,5}; Recursion r2 = new Recursion(b); System.out.println(r2.mode(5)); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
