Question: Consider the Polynomial interface provided in A04. Suppose we'd like to implement the interface using an array. For example, the ArrayPolynomial 2.1x3 + 5.0x -
Consider the Polynomial interface provided in A04. Suppose we'd like to implement the interface using an array. For example, the ArrayPolynomial 2.1x3 + 5.0x - 4.5 would be represented by the array:
-4.5 5.0 0.0 2.1
The first represents the constant term -4.5.
The second cell represents the linear term 5.0x.
The fourth cell represents the term 2.1x3.
(The third cell is zero because there is no x2 term.)
Start the definition of this class by giving:
The class header.
The instance variable(s).
A constructor that takes the (expected) degree of the Polynomial. (Remember that the degree of a polynomial is the highest power in its non-zero terms. The polynomial represented by the array above has a power of 3.) The Polynomial starts off as zero.
A constructor that takes no arguments, and creates a Polynomial of default (expected) degree, using a class constant (which you must also provide).
NOTE! You do not need to provide definitions for any of the interface methods! This is just the start of the class definition.
_______________________________________________________________________________________________________________________________
POLYNOMIAL INTERFACE CLASS:
/** * An interface with some basic polynomial methods. A polynomial is the sum of * several terms, each of which is a multiple of a power of x. The powers are * non-negative integers. The multiple is double, and terms multiplied by zero * are ignored. The multiplier for a term is called its coefficient. * For example: ** 0.2x5 + 1x2 + -3.5x1 + 2x0 *has four (non-zero) terms, as follows: *
| coefficient | power |
|---|---|
| 0.2 | 5 |
| 1.0 | 2 |
| -3.5 | 1 |
| 2.0 | 0 |
* Terms are printed in order from highest power to lowest power. Each (non- * zero) term shows the coefficient and the power of x, except for terms with a * coefficient of one, or a power of one or less. The coefficient one may be * left out. Terms with power one leave the power off, and terms with power zero * leave both the power and the x off. Other powers are preceded with a "hat" * symbol (^, also known as a circumflex). For example, the polynomial above * could be written as: * 0.2x^5 + x^2 + -3.5x + 2.0 * * Polynomials with no terms are written as zero. *
* The highest power in the polynomial is called the degree of the * polynomial. If there are no terms in the polynomial, then the power is zero. *
* Polynomials are evaluated by replacing the x with a given value and * calculating the sum. For example, the polynomial above at 2.0 would evaluate * to *
* 0.2×25 + 1×22 + -3.5×21 + 2×20 * = 0.2×32 + 1×4 + -3.5×2 + 2×1 * = 6.4 + 4 + -7 + 2 = 5.4 ** * @author Mark Young (A00000000) */ public interface Polynomial { /** * The power of the term with the highest power. * * @return the degree of this polynomial (power of highest non-zero * coefficient) */ public int getDegree(); /** * The coefficient of the x
power term. * * @param power the power we want the coefficient of * @return the coefficient of the term with that power, or zero if no such * term exists */ public double getCoefficient(int power); /** * Evaluate the polynomial at the given point. * * @param x the point to evaluate the polynomial at * @return the value of the polynomial at that point */ public double evaluateAt(double x); /** * Create a String representation of this polynomial. Only terms with non- * zero coefficients form part of the String representation. * * @return a String representing this polynomial */ public String toString(); /** * Add another term to this polynomial. If this polynomial already has a * term with the same power, the terms are combined, which might result in * that term being "zeroed out" and removed from the polynomial. * * This method returns the polynomial is was called on, allowing the method * to be called repeatedly on one object to add multiple terms. For example, * poly.add(3, 2).add(2, 1).add(7, 0) adds * 3x2 + 2x + 7 to poly. * * @param coefficient the coefficient of the term to add * @param power the power of the term to add * @return the same polynomial, as modified by the addition of the term */ public Polynomial add(double coefficient, int power); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
