title: Recursively reducing arrays using abstract methods [Java]?
body: <p>I'm working on the following problem on my assignment: <a href="http://i.imgur.com/AofwhXh.png" rel="nofollow">http://i.imgur.com/AofwhXh.png</a></p>
  
  <p>I'm having trouble with recursively reducing an array within a static method that applies an abstract method to the array. I'm unsure how to make the static method recursive. Here is what  I have so far</p>
  
  <p>For my interface class I have the following:</p>
  
  <pre><code>public interface ReduceFun {
      public int f(int a, int b);
  }
  </code></pre>
  
  <p>Classes that implement this:</p>
  
  <pre><code>class Add implements ReduceFun {
  
  @Override
  public int f(int a, int b) {
      return a + b;
  }
  
  }
  
  class Multiply implements ReduceFun {
  
  @Override
  public int f(int a, int b) {
      return a * b;
  }
  
  }
  
  class Minus implements ReduceFun {
  
  @Override
  public int f(int a, int b) {
      return 2*b - a;
  }
  
  }
  </code></pre>
  
  <p>The static method in my driver class is:</p>
  
  <pre><code>public static int ReduceArray(ReduceFun w, int[] a){
      int total = 0;
      int temp;
      if (a.length == 1)
      return a[0];
      else 
          for(int i=a.length-1; i &gt; 0; i--)
              if (i==1)
                  return w.f(a[0], a[1]); //Base Case
              else
                  //Stuck here
      return total;
  }
  </code></pre>
  
  <p>I tried calling <code>total = total + ReduceArray(w, a);</code>, which lead to an overflow error because it pretty much leads nowhere since the parameters don't keep track of the index. I was also thinking of creating a <code>int[] b</code> that is <code>int[] a</code> but with one less index. This way, I can keep track of the index, but I'm also stuck on what to make the return statement. I'd appreciate any and all help </p>