179k views
0 votes
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 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.

User OussaMah
by
8.0k points

1 Answer

3 votes

Final answer:

The ArrayPolynomial class represents polynomials using an array where each element corresponds to a coefficient for a specific power of x. It includes instance variables, a constructor with an expected degree argument to initialize the coefficients, and a default constructor with a predefined degree.

Step-by-step explanation:

To begin defining a class for a polynomial represented by an array in alignment with an interface, we'll start with the class header and the essential components, such as instance variables and constructors. Below is the structured start to the implementation:



Class Header and Instance Variable

public class ArrayPolynomial implements Polynomial {
private double[] coefficients;
}



Constructors

Constructor with Degree:

public ArrayPolynomial(int degree) {
coefficients = new double[degree + 1];
// Initialize all elements to zero
Arrays.fill(coefficients, 0.0);
}

Default Constructor:

private static final int DEFAULT_DEGREE = 2; // Example default degree

public ArrayPolynomial() {
this(DEFAULT_DEGREE);
}

In this setup, the ArrayPolynomial class implements the Polynomial interface and uses an array to store coefficients where the index represents the power of x corresponding to the coefficient. The constructor taking an integer argument sets the expected degree of the polynomial, and the default constructor utilizes a predefined degree, demonstrated here as a constant DEFAULT_DEGREE.

User Cardoso
by
7.3k points