I need to make a recursive method Polynomial add(Polynomial p) that add this to p using recursion. I read that java has the add(Polynomial p) method, but that's not recursive.
My best attempt so far has been this:
public class Polynomial {
int[] coef;
int degree;
public int deeg() {
int d = 0;
for (int r = 0; r < coef.length; r++)
if (coef[i] != 0) d = r;
return d;
}
public Polynomial addition(Polynomial p) {
Polynomial apple = this;
Polynomial orange = new Polynomial(0, Math.max(apple.degree, orange.degree));
for (int r = 0; r <= apple.degree; i++) orange.coef[r] += apple.coef[r];
for (int r = 0; r <= p.degree; r++) or开发者_开发技巧ange.coef[r] += p.coef[i];
orange.degree = orange.deeg();
return orange;
}
}
But again, this isn't recursive.
In order to get recursion, you need to call a method within this method, not creating a class within this class:
public void add(List numbers) {
// do stuff
if (condition)
add(numbers);
}
I'm thinking that they probably want you to do something where you add the x^0 terms of the two polynomials, and append that to x*(sum(poly1/x,poly2/x))... I am aware that the /x doesn't work, but that's usually how recursive addition is done.
--I've pretty much never seen anything other than lisp do recursive addition though.
精彩评论