Question

In: Computer Science

Write a recursive method to sum the values in an array of integers. Create a file...

Write a recursive method to sum the values in an array of integers. Create a file ArraySum.java and add the recursive method public int sumOfArray (). Use the driver class ArraySumDriver.java to populate your array and demonstrate that your method works.

////ArraySumDriver.java///////
public class ArraySumDriver {
   private final static int ARRAY_SIZE = 6;

   /**
    * @param args
    */
   public static void main(String[] args) {
      int index = 0;

      Integer[] myArray = new Integer[ARRAY_SIZE];
      ArraySum arraySum = new ArraySum();
      
      myArray[index++] = 3;
      myArray[index++] = 5;
      myArray[index++] = 2;
      myArray[index++] = 6;
      
      int sum = arraySum.sumOfArray(myArray, 3);
      System.out.println(sum);
      
      myArray[index++] = 7;
      myArray[index++] = 1;
      
      sum = arraySum.sumOfArray(myArray, 5);
      System.out.println(sum);
   }

}

Solutions

Expert Solution

JAVA PROGRAM

class ArraySum // create class ArraySum
{
//implement sumOfArray() method with two arugments
//First argument Integer array a and Second argument int size
public int sumOfArray(Integer a[],int size)
{
if(size<=0) return 0; // check condition size<=0 then return 0
return sumOfArray(a,size-1)+a[size-1]; // else, call recusively sumOfArray() and calculate sum of array elements
}
}

// Implement Driver class ArraySumDriver
public class ArraySumDriver
{
// declre final static integer ARRAY_SIZE=6
// does not change ARRAY_SIZE it is final
private final static int ARRAY_SIZE=6;

public static void main(String [] args)
{
   int index=0; // declare constant integer index=0
   Integer [] myArray=new Integer[ARRAY_SIZE]; // create Integer array object
   ArraySum arraySum=new ArraySum(); // create instance of ArraySum is arraySum
   // declare array elements one by one
   myArray[index++]=3;
   myArray[index++]=5;
   myArray[index++]=2;
   myArray[index++]=6;
  
   int sum=arraySum.sumOfArray(myArray,3); // calling sumOfArray() method and return result sum
   System.out.println(sum); // display sum of only first 3 elements in array

   myArray[index++]=7;
   myArray[index++]=1;

   sum=arraySum.sumOfArray(myArray,5); // calling sumOfArray() method and return result sum
   System.out.println(sum); // display sum of only first 5 elements in array
}
}

OUTPUT

10
23


Related Solutions

Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
IN JAVA Methods**: Sort three values Write a method Ascend3 with an array of integers of...
IN JAVA Methods**: Sort three values Write a method Ascend3 with an array of integers of size three as the parameter, that sorts the values of the array into ascending order. Ex: If the array contains [5, 2, 7], after the call Ascend3(int[] vals), the array will now hold [2, 5, 7]. Hints: Return type should be void. One approach puts the three values into an array, then sorts the array. We won't be describing that approach here. Instead, we'll...
Write an application that uses multithreading to compute the sum of the integers in an array...
Write an application that uses multithreading to compute the sum of the integers in an array of size 100,000. You can populate the array with random numbers and your application should display the sum. (JAVA)
IN JAVA Create an array and add random values to it, and then find the sum...
IN JAVA Create an array and add random values to it, and then find the sum of the values using recursion.
Write a method that takes an array of integers as input. The method should find and...
Write a method that takes an array of integers as input. The method should find and display two indices m and n such that if you sort the elements from index m to index n the entire array would be sorted. The method should minimize m − n, that is it should find the smallest subsection for the array that needs to be sorted. For example, int[] a = {2, 4, 6, 7, 9, 8, 12, 15, 5, 13, 18,...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the range of 0 to 100. There may be repeats. The numbers must not be ordered/sorted. The task is to find and print the two smallest numbers. You must accomplish this task without sorting the file and without using arrays for any purpose. It is possible that the smallest numbers are repeated – you should print the number of occurrences of the two smallest numbers....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Please write in java: Write a recursive method toNumber that forms the integer sum of all...
Please write in java: Write a recursive method toNumber that forms the integer sum of all digit characters in a string. For example, the result of toNumber("3ac4") would be 7. Hint: If next is a digit character ('0' through '9'), Character.isDigit(next) is true and the numeric value of next is Character. digit(next, 10).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT