Guest User

Untitled

a guest
Jan 21st, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. public Poly add (Poly q) throws NullPointerException {
  2. //EFFECTS: If q is null throws NullPointerException else
  3. // returns the Poly this + q.
  4. Poly la, sm;
  5. if (deg > q.deg) {la = this; sm = q;} else {la = q; sm = this;}
  6. int newdeg = la.deg;
  7. // new degree is the larger degree
  8. if (deg == q.deg)
  9. // unless there are trailing zeros
  10. for (int k = deg; k > 0; k--)
  11. if (trms[k] + q.trms[k] != 0) break; else newdeg--;
  12. Poly r = new Poly(newdeg);
  13. // get a new Poly
  14. int i;
  15. for (i = 0; i <= sm.deg && i <= newdeg; i++)
  16. r.trms[i] = sm.trms[i] + la.trms[i];
  17. for (int j = i; j <= newdeg; j++) r.trms[j] = la.trms[j];
  18. return r;
  19. }
  20.  
  21. public Poly mul (Poly q) throws NullPointerException {
  22. //EFFECTS: If q is null throws NullPointerException else
  23. // returns the Poly this * q.
  24. if ((q.deg == 0 && q.trms[0] == 0) ||
  25. (deg == 0 && trms[0] == 0)) return new Poly( );
  26. Poly r = new Poly(deg+q.deg);
  27. r.trms[deg+q.deg] = 0;
  28. // prepare to compute coeffs
  29. for (int i = 0; i <= deg; i++)
  30. for (int j = 0; j <= q.deg; j++)
  31. r.trms[i+j] = r.trms[i+j] + trms[i]*q.trms[j];
  32. return r;
  33. }
  34. }
Add Comment
Please, Sign In to add comment