I am working on this lab assignment for school and I was wondering if anyone could give me some advice. My instructor wants us to add different number objects up from an array list and display the result. He wants us to do the addition in a non-loop recursive format. I've done a similar problem like this before with just integers, but I cannot seem to get this solution because there is more than one primitive type this time. Here is the Code I have so far:
Main:
import java.math.BigInteger;
import java.util.ArrayList;
public class TestSummation {
public static void main(String[] args) {
ArrayList<Number> aList = new ArrayList<Number>();
aList.add(new Integer(4));
aList.add(new Double(3.423));
aList.add(new Float(99.032));
aList.add(new BigInteger("32432"));
double result = Summation.sum(aList);
System.out.println(result);
}
}
Class that holds the recursive method:
import java.util.ArrayList;
public class Summation {
public static double sum(ArrayList<Number> aList) {
if (aList.size() < 1) {
return 0;
} else {
return sum(aList);
}
}
}
Right now this code throws a StackOverflowException and I was wondering if anyone had advice that would help me. I definitely know I need to add more to it, but I feel I'm on the right track with this current section of code. I'm just hitting a bad roadblock right now. Thanks in advance for any advice!
aList.size() < 1is the same thing asaList.isEmpty()