Guest User

Untitled

a guest
Jan 21st, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. public class Poly {
  2. //OVERVIEW:
  3. ...
  4. private int[ ] trms;
  5. private int deg;
  6.  
  7. // constructors
  8. public Poly ( ) {
  9. //EFFECTS: Initializes this to be the zero polynomial.
  10. trms = new int[1]; deg = 0;
  11. }
  12.  
  13. public Poly (int c, int n) throws NegativeExponentException {
  14. //EFFECTS: If n < 0 throws NegativeExponentException else
  15. //initializes this to be the Poly cxn.
  16. if (n < 0)
  17. throw new NegativeExponentException("Poly(int,int) constructor");
  18. if (c == 0) { trms = new int[1]; deg = 0; return; }
  19. trms = new int[n+1];
  20. for (int i = 0; i < n; i++) trms[i] = 0;
  21. trms[n] = c;
  22. deg = n;
  23. }
  24.  
  25. private Poly (int n) { trms = new int[n+1]; deg = n; }
  26.  
  27. // methods
  28. public int degree ( ) {
  29. //EFFECTS: Returns the degree of this, i.e., the largest exponent
  30. // with a non-zero coefficient. Returns 0 if this is the zero Poly.
  31. return deg;
  32. }
  33.  
  34. public int coeff (int d) {
  35. //EFFECTS: Returns the coefficient of the term of this whose exponent is d.
  36. if (d < 0 || d > deg) return 0; else return trms[d];
  37. }
  38.  
  39. public Poly sub (Poly q) throws NullPointerException {
  40. //EFFECTS: If q is null throws NullPointerException else
  41. // returns the Poly this - q.
  42. return add (q.minus( ));
  43. }
  44.  
  45. public Poly minus ( ) {
  46. //EFFECTS: Returns the Poly -this.
  47. Poly r = new Poly(deg);
  48. for (int i = 0; i < deg; i++) r.trms[i] = - trms[i];
  49. return r;
  50. }
Add Comment
Please, Sign In to add comment