0

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!

1
  • 1
    Note: aList.size() < 1 is the same thing as aList.isEmpty() Commented Oct 6, 2013 at 21:49

3 Answers 3

1

Recursion always works by cons two different cases:

  • the base case which is used to end the recursion
  • the recursive case which is applied on a specific N-th step

By thinking about your problem you can consider as your base case either a list of size 1 or a list of size 0. Let's choose the first for simplicity: the sum of all values of a list of size 1 is the only value that it is contained.

Now let's look for the recursive case: suppose that the list has length N. What we know is that the sum of all elements of a list of N elements is the N-th element added to the sum of a list containing N-1 elements (by removing the N-th element). It's pretty self explicatory as most of recursive implementations.

As you can see the recursive step reduces the size of the list so that the algorithm step-by-step reaches the base case, this is not what is happening to your code.

Sign up to request clarification or add additional context in comments.

1 Comment

It would be better to use a list size of 0 as the base case in order to deal with an empty list to start with.
1

Since you've done something similar with integers, I gather that the problem is how to deal with the multitude of number class types. Since you want a double result and all Number objects must implement a doubleValue() method, you can use that to build your recursion. To recurse, you need to take the first element's value and add it to the (recurive) summation of the sublist that starts with the second element:

public class Summation {
    public static double sum(List<Number> aList) {
        final int len = aList.size();
        if (len == 0) {
            return 0;
        }
        final double val = aList.get(0).doubleValue();
        if (len == 1) {
            return val;
        }
        // help future compilers recognize tail recursion
        return val + sum(aList.sublist(1, len));
    }
}

1 Comment

Thank you so much, Ted! The ".doubleValue();" is what I was forgetting and it works perfectly now! I usually don't ask for help because I like to try and figure it out on my own, but recursion confuses me terribly. Again, thank you so much!
0

Well, without giving it all away, I'll tell you that your code is throwing the exception because you're not modifying the list at all, simply passing it back into your recursive method, which is resulting in an infinite loop. The bit you'll have to add involves modifying the list before passing it into the recursive call (and, of course, doing the actual summation).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.