Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Poly add (Poly q) throws NullPointerException {
- //EFFECTS: If q is null throws NullPointerException else
- // returns the Poly this + q.
- Poly la, sm;
- if (deg > q.deg) {la = this; sm = q;} else {la = q; sm = this;}
- int newdeg = la.deg;
- // new degree is the larger degree
- if (deg == q.deg)
- // unless there are trailing zeros
- for (int k = deg; k > 0; k--)
- if (trms[k] + q.trms[k] != 0) break; else newdeg--;
- Poly r = new Poly(newdeg);
- // get a new Poly
- int i;
- for (i = 0; i <= sm.deg && i <= newdeg; i++)
- r.trms[i] = sm.trms[i] + la.trms[i];
- for (int j = i; j <= newdeg; j++) r.trms[j] = la.trms[j];
- return r;
- }
- public Poly mul (Poly q) throws NullPointerException {
- //EFFECTS: If q is null throws NullPointerException else
- // returns the Poly this * q.
- if ((q.deg == 0 && q.trms[0] == 0) ||
- (deg == 0 && trms[0] == 0)) return new Poly( );
- Poly r = new Poly(deg+q.deg);
- r.trms[deg+q.deg] = 0;
- // prepare to compute coeffs
- for (int i = 0; i <= deg; i++)
- for (int j = 0; j <= q.deg; j++)
- r.trms[i+j] = r.trms[i+j] + trms[i]*q.trms[j];
- return r; }
- }
Add Comment
Please, Sign In to add comment