Question: I have a bug in my division method. It is failing when assigning the first term to binomial in the while loop. After resolving that

I have a bug in my division method. It is failing when assigning the first term to binomial in the while loop. After resolving that error, I need to make sure that division is giving the right output.

Can someone help me spot this bug and fix the division method?

public static Polynomial[] divrem(Polynomial p1, Polynomial p2) { Polynomial quotient = new Polynomial(); Polynomial remainder = new Polynomial(); Polynomial binomial = new Polynomial(); Polynomial Subtractant = new Polynomial(); if(p1.first == null) { remainder.first = null; return new Polynomial[] {quotient, remainder}; } else if(p2.first == null) { throw new ArithmeticException("Div By Zero"); } else if(p2.first.exp > p1.first.exp) { remainder.first = p2.first; return new Polynomial[] {quotient, remainder}; } else { remainder.first = p1.first; while(remainder.first.exp >= p2.first.exp && remainder.first != null) { System.out.println(p1.first.exp + " < " + p2.first.exp); binomial.first = new Term(Rational.div(remainder.first.coef, p2.first.coef), remainder.first.exp - p2.first.exp); quotient = add(quotient,binomial); Subtractant = mul(p2, binomial); remainder = sub(remainder, Subtractant); } return new Polynomial[] {quotient, remainder}; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!