Class Poly

java.lang.Object
it.unimi.di.prog2.h10.Poly

public class Poly extends Object
Polys are immutable polynomials with integer coefficients.

A typical Poly is \( p = c_0 + c_1 x + c_2 x^2 + \cdots + c_n x^n \).

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final int[]
    The array of coefficients, the coeff[i] is the coefficient of \( x^i \).
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
     
    Initializes this to be the zero polynomial, that is \( p = 0 \).
    private
    Poly(int n)
    Initializes a polynomial of given degree (with all coefficients equal to 0).
     
    Poly(int c, int n)
    Initializes this to be the polynomial \(p = cx^n\).
  • Method Summary

    Modifier and Type
    Method
    Description
    add(Poly q)
    Performs polynomial addition.
    int
    coeff(int d)
    Returns the coefficient of the term of given exponent.
    int
    Returns the degree of this polynomial.
    boolean
     
    int
     
    Returns the negate polynomial.
    mul(Poly q)
    Performs polynomial multiplication.
    sub(Poly q)
    Performs polynomial subtraction.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • coeff

      private final int[] coeff
      The array of coefficients, the coeff[i] is the coefficient of \( x^i \).
  • Constructor Details

    • Poly

      public Poly()
      Initializes this to be the zero polynomial, that is \( p = 0 \).
    • Poly

      public Poly(int c, int n) throws NegativeExponentException
      Initializes this to be the polynomial \(p = cx^n\).
      Parameters:
      c - the coefficient.
      n - the degree.
      Throws:
      NegativeExponentException - if n < 0.
    • Poly

      private Poly(int n)
      Initializes a polynomial of given degree (with all coefficients equal to 0).
      Parameters:
      n - the degree.
  • Method Details

    • degree

      public int degree()
      Returns the degree of this polynomial.
      Returns:
      the largest exponent with a non-zero coefficient; returns 0 if this is the zero Poly.
    • coeff

      public int coeff(int d)
      Returns the coefficient of the term of given exponent.
      Parameters:
      d - the exponent of the term to consider.
      Returns:
      the coefficient of the considered term.
    • add

      public Poly add(Poly q) throws NullPointerException
      Performs polynomial addition.

      If \( p \) is this polynomial, returns \( p + q \).

      Parameters:
      q - the polynomial to add to this one.
      Returns:
      the sum among this and the given polynomial.
      Throws:
      NullPointerException - if q is null.
    • mul

      public Poly mul(Poly q) throws NullPointerException
      Performs polynomial multiplication.

      If \( p \) is this polynomial, returns \( p q \).

      Parameters:
      q - the polynomial to multiply by this one.
      Returns:
      the product among this and the given polynomial.
      Throws:
      NullPointerException - if q is null.
    • sub

      public Poly sub(Poly q) throws NullPointerException
      Performs polynomial subtraction.

      If \( p \) is this polynomial, returns \( p - q \).

      Parameters:
      q - the polynomial to subtract from this one.
      Returns:
      the subtraction among this and the given polynomial.
      Throws:
      NullPointerException - if q is null.
    • minus

      public Poly minus()
      Returns the negate polynomial.

      If \( p \) is this polynomial, returns \( -p \).

      Returns:
      this polynomial multiplied by \( -1 \).
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object