CS106代写:Polynomial

Introduction

关于多项式的Java作业代写,实现多项式的加减法即可。

Background

You should already be quite familiar with polynomials, thanks to years of math classes. We can consider a polynomial of one variable (x) to be a list of terms, with each term consisting of two parts: a coefficient and an exponent. For example, the polynomial 2x^3 - 1 + x^-4 has three terms, 2x^3, -1x^0, and 1x^-4. In the first term, the coefficient is 2 and the exponent is 3; in the second, they are -1 and 0, respectively. Using an array of term objects as our polynomial representation, we will represent this sample polynomial with an array of just three elements. No terms with coefficients of zero are stored.
As a polynomial is a collection of terms, a polynomial class is likely to have methods dealing with the general issue of term quantity; for example, the number of terms in the polynomial. And, lots of other classes are likely to need quantity-related methods, too. Sounds like a job for an interface! Because we will present interfaces in class after this program is assigned, we are providing the Quantity interface for you to use. You can find it on the class web page. Just store Quantity. java in the same place as the files for the three classes described below.

Assignment

Implement the following three components in separate .java files:

  1. The class Term. A Term object represents a term of a polynomial. The implementation details of Term are up to you, with the understanding that these objects exist to support the needs of your Polynomial class (see below). (Of course, we expect that you will create a well-designed, well-implemented, and well-documented Term class!)
  2. The class Polynomial, which implements the Quantity interface. It is to have just one constructor:

    • Polynomial() - Create an empty (termless) Polynomial.
  3. The twelve public methods of this class are:

    • Polynomial add(Polynomial p) - Mathematically add the polynomial p to the current Polynomial, returning a new Polynomial object without changing either of the existing Polynomial objects. If both Polynomial objects possess terms with matching exponents, sum each pair of matching terms.
    • void addTerm(int c, int e) - Add to the current Polynomial a term With coefficient c and exponent e. If a term With exponent e already exists, sum the terms and replace the old coefficient With the sum.
    • Polynomial replicate () - Create a new Polynomial that possesses a copy of the content of the current Polynomial using a completely neW collection of objects.
    • boolean equals (Polynomial p) - Returns true if p has the same number of non-zero terms With the same coefficients and same exponents as this Polynomial, false otherwise.
    • double evaluate(double x) - Evaluate this Polynomial on the value x. Return the result of the evaluation.
    • int getCoefficient(int e) - Return the coefficient currently associated With the term With exponent e.
    • boolean isEmpty() - Returns true if the Polynomial currently has holds no terms, false otherwise.
    • boolean isFull () - Returns true if the Polynomial currently has no available space for additional terms, false otherwise.
    • int holding() - Returns the number of terms with non-zero coefficients that are currently in the Polynomial.
    • Polynomial negate () - Creates a new Polynomial of new objects that has the same number of terms with the same exponents as does the current Polynomial, but the signs on the coefficients are switched (positive becomes negative and negative becomes positive).
    • void scalarMultiply(int s) - Multiply each term’s coefficient by the value s.
    • String toString() - Create a String representation of the current Polynomial, with the terms in decreasing order by exponent, with all coefficients and exponents displayed and parenthesized and one space on either side of the additions and subtractions. For example, the polynomial: x^-4 + 2x^3 - 1 would be represented by the string “(2)x ^ (3) - (1)x ^ (0) + (1)x ^ (-4)”.
  4. The class Program4, which will contain your main method. The purpose of Program4 is to test the correct operation of the Polynomial class. As usual, write a good, complete set of tests.