In: Computer Science
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
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