Question

In: Computer Science

Using Java with NO imports,    Method arrayOfSums returns an ArrayList of the same size as...

Using Java with NO imports,

   Method arrayOfSums returns an ArrayList of the same size as the largest ArrayList
       This method needs to be overloaded so that it works for 2 to 4 sent Arraylists.

       arrayOfSums([2,4,7], [3,7,14]) returns [5,11,21]
       arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3]) returns [15, 23, 21, 11]
       arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3], [3,5,4,1,2]) returns [18, 28, 25, 12, 2]
       arrayOfSums([8,6], [2,4,10]) returns [10,10,10]
       arrayOfSums([2,2,2,2,2], [3,2,5,2,3], [1,3,1,3,1]) returns [6, 7, 8, 7, 6]
       arrayOfSums([11,12,13,14], [14,13,12,11], [10,10,10,10], [5,6,7,6,5]) returns [40, 41, 42, 41, 5]
       @param nums1 ArrayList of integers of varying sizes
       @param nums2 ArrayList of integers of varying sizes
       @param nums3 ArrayList of integers of varying sizes
       @param nums4 ArrayList of integers of varying sizes
       @return an array of the sums of the corresponding elements from each array
   */
public static ArrayList<Integer> arrayOfSums(ArrayList<Integer> nums1, ArrayList<Integer> nums2, ArrayList<Integer> nums3, ArrayList<Integer> nums4 )
{
       ArrayList<Integer> numsSums = new ArrayList<Integer>();
       return numsSums;

}// end arrayOfSums

   /**
   * Do overloaded methods here.
   * to compile, I am adding the method line.
   */

       public static ArrayList<Integer> arrayOfSums(ArrayList<Integer> nums1, ArrayList<Integer> nums2, ArrayList<Integer> nums3)
{
           ArrayList<Integer> numsSums = new ArrayList<Integer>();
        return numsSums;

}// end arrayOfSums
       public static ArrayList<Integer> arrayOfSums(ArrayList<Integer> nums1, ArrayList<Integer> nums2)
       {
           ArrayList<Integer> numsSums = new ArrayList<Integer>();
       return numsSums;
       }// end arrayOfSums

Solutions

Expert Solution

public class ArrayofSums {
        public static void main(String[] args) {
                java.util.ArrayList list1 = new java.util.ArrayList();
                list1.add(6);
                list1.add(13);
                list1.add(4);
                list1.add(8);
                
                java.util.ArrayList list2 = new java.util.ArrayList();
                list2.add(7);
                list2.add(5);
                list2.add(9);
                
                java.util.ArrayList list3 = new java.util.ArrayList();
                list3.add(2);
                list3.add(5);
                list3.add(8);
                list3.add(3);
                System.out.println(arrayOfSums(list1,list2,list3));
                
        }
        public static java.util.ArrayList<Integer> arrayOfSums(java.util.ArrayList<Integer> nums1,
                        java.util.ArrayList<Integer> nums2, java.util.AbstractList<Integer> nums3,
                        java.util.ArrayList<Integer> nums4) {
                java.util.ArrayList<Integer> numsSums = new java.util.ArrayList<Integer>();
                // iterating through arraylist and getting elemenet from each list and adding to result
                for (int i = 0; i < nums1.size(); i++) {
                        int total=0;
                        //checking if num1 has enough elements
                        if(nums1.size()>i)
                                total+=nums1.get(i);
                        //checking if num2 has enough elements
                        if(nums2.size()>i)
                                total+=nums2.get(i);
                        //checking if num3 has enough elements
                        if(nums3.size()>i)
                                total+=nums3.get(i);
                        //checking if num4 has enough elements
                        if(nums4.size()>i)
                                total+=nums4.get(i);
                        
                        numsSums.add(total);
                }
                return numsSums;

        }// end arrayOfSums

        // Overloaded methods here to compile

        public static java.util.ArrayList<Integer> arrayOfSums(java.util.ArrayList<Integer> nums1,
                        java.util.ArrayList<Integer> nums2, java.util.ArrayList<Integer> nums3) {
                java.util.ArrayList<Integer> numsSums = new java.util.ArrayList<Integer>();
                // iterating through arraylist and getting elemenet from each list and adding to result
                for (int i = 0; i < nums1.size(); i++) {
                        int total=0;
                        //checking if num1 has enough elements
                        if(nums1.size()>i)
                                total+=nums1.get(i);
                        //checking if num2 has enough elements
                        if(nums2.size()>i)
                                total+=nums2.get(i);
                        //checking if num3 has enough elements
                        if(nums3.size()>i)
                                total+=nums3.get(i);
                        
                        numsSums.add(total);
                }
                return numsSums;

        }// end arrayOfSums

        public static java.util.ArrayList<Integer> arrayOfSums(java.util.ArrayList<Integer> nums1,
                        java.util.ArrayList<Integer> nums2) {
                java.util.ArrayList<Integer> numsSums = new java.util.ArrayList<Integer>();
                // iterating through arraylist and getting elemenet from each list and adding to result
                for (int i = 0; i < nums1.size(); i++) {
                        int total=0;
                        //checking if num1 has enough elements
                        if(nums1.size()>i)
                                total+=nums1.get(i);
                        //checking if num2 has enough elements
                        if(nums2.size()>i)
                                total+=nums2.get(i);
                        numsSums.add(total);
                }
                return numsSums;
        }// end arrayOfSums
}

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

Please Like and Support me as it helps me a lot


Related Solutions

Using Java with no imports,    Method arrayOfSums returns an ArrayList of the same size as...
Using Java with no imports,    Method arrayOfSums returns an ArrayList of the same size as the largest ArrayList        This method needs to be overloaded so that it works for 2 to 4 sent Arraylists.        arrayOfSums([2,4,7], [3,7,14]) returns [5,11,21]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3]) returns [15, 23, 21, 11]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3], [3,5,4,1,2]) returns [18, 28, 25, 12, 2]        arrayOfSums([8,6], [2,4,10]) returns [10,10,10]        arrayOfSums([2,2,2,2,2], [3,2,5,2,3], [1,3,1,3,1]) returns [6, 7, 8,...
Code with Java and no imports, Method sandwiched returns true if num is in the element...
Code with Java and no imports, Method sandwiched returns true if num is in the element before and after an element that is not equal to num sandwiched([4,5,4,6,7,3], 4) returns true sandwiched([2,1,2], 2) returns true sandwiched([3,3,3], 3) returns false sandwiched([4,5,6,4], 4) returns false sandwiched([1,1,2,3,1,4,1], 1) returns true @param nums Integer ArrayList @param num integer @return true if a single number is between elements equal to num */ public static boolean sandwiched(ArrayList nums, int num) { return false; }//end sandwiched
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the...
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the union of elements in two lists. For example: list1 contains elements [1, 2, 3, 4, 5] list2 contains elements [3, 4, 5, 6, 7] Diff() method should return an array list with elements [1, 2, 3, 4, 5, 6, 7].
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
I am trying to create a method in JAVA that takes in an ArrayList and sorts...
I am trying to create a method in JAVA that takes in an ArrayList and sorts it by the requested "amenities" that a property has. So if someone wants a "pool" and "gym" it would show all members of the array that contain a "pool" and "gym". It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. You can edit it...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the strings of even length from the list. 2. Given the following Vehicle interface and client program in the Car class: public interface Vehicle{ public void move(); } public class Car implements Vehicle{ public static void main(String args[]) Vehicle v = new Vehicle(); // vehicle declaration } The above declaration is valid? True or False? 3. Java permits a class to...
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
I am trying to create a method in JAVA that takes in an ArrayList<Property> and filters...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and filters it by the requested price range that a property has. So if someone wants a property between the value of 10(min) and 20(max) it would show all members of the array that meet those conditions.. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below....
I am trying to create a method in JAVA that takes in an ArrayList<Property> and sorts...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and sorts it by the amount of "reviews" that a property has in increasing order. So the most reviews first. So each listing in the array would contain a different number of reviews, and they should be sorted based on that value. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to...
Move all zeros in an arraylist to the end of the list using a temp(java). if...
Move all zeros in an arraylist to the end of the list using a temp(java). if it is an array the code would look like the code below, change it to fit an arraylist int count = 0;     int temp;     for (int i = 0; i < n; i++) {     if ((arr[i] != 0)) {         temp = arr[count];         arr[count] = arr[i];         arr[i] = temp;         count = count + 1;     }     } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT