Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Poly {
- //OVERVIEW:
- ...
- private int[ ] trms;
- private int deg;
- // constructors
- public Poly ( ) {
- //EFFECTS: Initializes this to be the zero polynomial.
- trms = new int[1]; deg = 0;
- }
- public Poly (int c, int n) throws NegativeExponentException {
- //EFFECTS: If n < 0 throws NegativeExponentException else
- //initializes this to be the Poly cxn.
- if (n < 0)
- throw new NegativeExponentException("Poly(int,int) constructor");
- if (c == 0) { trms = new int[1]; deg = 0; return; }
- trms = new int[n+1];
- for (int i = 0; i < n; i++) trms[i] = 0;
- trms[n] = c;
- deg = n;
- }
- private Poly (int n) { trms = new int[n+1]; deg = n; }
- // methods
- public int degree ( ) {
- //EFFECTS: Returns the degree of this, i.e., the largest exponent
- // with a non-zero coefficient. Returns 0 if this is the zero Poly.
- return deg;
- }
- public int coeff (int d) {
- //EFFECTS: Returns the coefficient of the term of this whose exponent is d.
- if (d < 0 || d > deg) return 0; else return trms[d];
- }
- public Poly sub (Poly q) throws NullPointerException {
- //EFFECTS: If q is null throws NullPointerException else
- // returns the Poly this - q.
- return add (q.minus( ));
- }
- public Poly minus ( ) {
- //EFFECTS: Returns the Poly -this.
- Poly r = new Poly(deg);
- for (int i = 0; i < deg; i++) r.trms[i] = - trms[i];
- return r;
- }
Add Comment
Please, Sign In to add comment