Question
(1) The Constructor For the constructor, we pass an integer array as the parameter to initialize the coefficients array. We declare the method header for
(1) The Constructor For the constructor, we pass an integer array as the parameter to initialize the coefficients array. We declare the method header for the constructor as follows: public Polynomial(int [] coefficients); Normally, we would think that the highest power of the variable x in the polynomial would be 14 (the length of the array minus one). However, the user may provide an array with a small size, i.e., an array contains 3 coefficients, which means the larger indexed coefficients are 0. Hence the length of the array does not directly indicate the degree of the polynomial. To cope with this, you need to copy the array elements one by one using a loop instead of just assigning the parameter array to the coefficients instance reference. One can create a Polynomial object by calling the constructor. Here is an example: int [] a = {1, -2, 2}; Polynomial p = new Polynomial(a); // representing 2*x2 - 2*x1 + 1*x0 2 (2) Arithmetic Methods The methods implementing the polynomial arithmetic should operate on the coefficients of this polynomial (the current instance) with the coefficients of that polynomial (The parameter instance). The word that is not a Java reserved word. We can use it for a variable name in the parameter list. For example, use as the method header for plus: public Polynomial plus(Polynomial that) Calculate the new coefficients for the result of each arithmetic method and return them in a new Polynomial object, as follows: Allocate a result array to hold the coefficients of the sum/difference. Execute the required steps to perform the arithmetic. Calculate new coefficients and store them in the array. Construct and return a new polynomial using the array of coefficients that you calculated above. (3) Derivative Method In calculus, the derivative of a term in a polynomial can be calculated easily. The derivative of the term Cnxn is n*Cnx(n 1). The derivative of the entire polynomial is the sum of the derivatives of every term. Use the Polynomial arithmetic methods as needed. (4) Evaluate Method The evaluate method, which has the following header, calculates the polynomial at a specific value of x. public double evaluate(double x) (5) CompareTo Method This is the method required to implement the interface Comparable. Look up the interface Comparable on the Sun website and study the meaning of implementing this interface. You should also include an equals method for comparisons. The compareTo method implements the natural ordering of polynomials as follows: 3 A polynomial is lower than a polynomial with a non-zero coefficient of a higher power. If the number of coefficients is the same, a polynomial is lower based on the lower of the largest coefficient that is different. If the number of coefficients and all the coefficients of each power is the same, the polynomials are equal. Once you have written a compareTo method, you can use it to write a simple equals method. Return true or false based on the return value from a call to the compareTo method being equal to 0 or not. For example, in another class, you can compare two polynomials p and q using: int n = p.compareTo(q); // n = -, 0, or + value based on comparison boolean b = p.equals(q); // b = true or false based on comparison (6) ToString Method This method displays a Polynomial object as a String. The String representing a polynomial is a concatenation of the coefficients in the array and their corresponding powers of x. See the provided TestPolynomial class to understand the exact format your code needs to produce. Although the ^ symbol is not used as an exponentiation operator in Java, we use it to indicate superscribing in the string representation of our polynomials.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started