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
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)
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).
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Directions: Write a C++ program that will create an array of four integers. It will allow...
Directions: Write a C++ program that will create an array of four integers. It will allow the user to enter in four valid scores and store them in the array. ( valid range is 0 - 100) It will compute the letter grade based upon the four scores, namely, A = 90 - 100, B= 80- 89, C = 70-79, D = 60-69, otherwise F. It will display the scores and letter grade to the screen. NOTE: No menu is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT