Question

implement and test several recursive methods below. Test all these in the same program. Keep adding...

implement and test several recursive methods below. Test all these in the same program. Keep adding methods and testing until all are working.(java)
1.A recursive method to find the sum of all elements of an integer array. The method should have 2 parameters. The array and where to start in the array. Example of initial call:
int sum = sumOfValues(array, 0);
2.A recursive method to find the largest element in an integer array. The method should have 2 parameters as in question1. [Hint: Find the largest in a subset containing all but the first element. Then return the larger of these]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package mis;

public class RecursiveSumTest {
   static int largest=-1;
   public static int sumOfValues(int ar[],int s){
       if(s<0 || s>=ar.length) //if invalid index return 0
           return 0;
       else{
           return ar[s]+sumOfValues(ar,s+1); //else return sum of current element
           //and recursively call for next element of array
       }
   }
   public static int findLargest(int ar[],int s){
       if(s<0 || s>=ar.length) //if invalid index return 0
           return 0;
       else{
           //return maximum of both current element and
           //recursively next element of array
           return Integer.max(ar[s],findLargest(ar,s+1));
       }
   }

   //driver program to test
   public static void main(String[] args) {
      
       int ar[]={1,3,7,2,3};
       int sum=sumOfValues(ar,1);
       System.out.println(sum);
       int largets=findLargest(ar,0);
       System.out.println(largets);
       ar=new int[]{9,34,7,2,3};
       sum=sumOfValues(ar,4);
       System.out.println(sum);
       largets=findLargest(ar,3);
       System.out.println(largets);
   }
  

}

output

15
7
3
3

Add a comment
Know the answer?
Add Answer to:
implement and test several recursive methods below. Test all these in the same program. Keep adding...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • You will implement and test several short recursive methods below. With the proper use of recursi...

    this can be done in one class or two separate classes,in java thanks! You will implement and test several short recursive methods below. With the proper use of recursion, none of these methods should require more than a dozen lines of code. Test all these in the same class. Keep adding methods and testing until all are working 5. A Fractal Pattern Examine this pattern of stars and blanks, and write a recursive method that can generate patterns such as...

  • Please use java code. Implement a test class named BatArray1Test that tests all the functionality of...

    Please use java code. Implement a test class named BatArray1Test that tests all the functionality of the following given 3 methods, including the invalid arguments: 1) commonEnd Given two arrays of ints, a and b, return true if they have the same first element or they have the same last element. This method should return false if either array is empty or null. 2) midThree Given an array of integers, return a new array of length 3 containing the elements...

  • Write and implement a recursive version of the sequential search algorithm. Test the algorithm with an...

    Write and implement a recursive version of the sequential search algorithm. Test the algorithm with an array that have ten integer values, user can enter the values or assigned the values to the array in the main method. The search key should be a value in the array or not in the array. JAVA!!!

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    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...

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • Write a JAVA program with methods that initializes an array with 20 random integers between 1...

    Write a JAVA program with methods that initializes an array with 20 random integers between 1 and 50 and then prints four lines of output, containing 1)The initialized array. 2)Every element at an even index. 3)Every even element. 4)All elements in reverse order. Requirements (and hints): 1. Build a method that takes any integer array as input and prints the elements of the array. ALL printing in this lab must be done using a call to the printArray method and...

  • Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement...

    Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement some list functionalities on an ArrrayList of generic type using type parameters in method definition Our goal for this lab is to continue using the divide and conquer approach of splitting a list into its head and tail and keep recursing on the tail (which happens to be smaller). However, instead of trying the approach on a string (a list of characters) we would...

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...

  • In Java write the following array- processing methods into the same application, Lab13.java. Use the main...

    In Java write the following array- processing methods into the same application, Lab13.java. Use the main method in this application to test your methods. 1) Write the void method, shiftLeft, which accepts an array of int and changes the elements of this array so that the index of each element is now less by one and the last element of the array is now zero. For example, if the parameter array is {7, 3, 2, 9, 5}, then when this...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT