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

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

  • 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!!!

  • Exercise 5: Design and implement a Java program (name it ArrayMethods), that defines 4 methods as...

    Exercise 5: Design and implement a Java program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax(int[] arr) determines and returns the largest value within an array int arrayMin(int[] arr) determines and returns the smallest value within an array void arrayReverse(int[] arr) reverses the array (for example: 7 8 1 2 becomes 2 1 8 7) void arraySquared(int[] arr) changes every value within the array to value² Test your methods by creating an array of length 5 within...

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

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

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

  • Create a class called Lab7b and in it implement all of the methods below. Also, write...

    Create a class called Lab7b and in it implement all of the methods below. Also, write a main method with test calls to all of these methods. Don’t forget to turn in your file to Canvas before the end of the lab today. int[][] random(int N, int start, int end) returns an N-by-N matrix of random integers ranging from start to end; int rowSum(int[][] a, int i) returns the sum of the elements in row i of the 2-D array...

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

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
ADVERTISEMENT