Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help in the rest of it public class Polynomial { // this is a nested static class, it defines a type // all the

Need help in the rest of it

public class Polynomial { // this is a nested static class, it defines a type // all the instance varaibles can be access directly inside Polynomial class even though they have // private modifier, you can also make them public private static class TermNode{ private double coefficient; private int exponent; private TermNode next; public TermNode(int exp, double coeff, TermNode nextTerm ) { coefficient= coeff; exponent = exp; next = nextTerm; } } // instance variables of Polynomial // first is the term of the Polynomial with the highest degree // except the constant term, only non-zero terms are stored. private TermNode first; /**Postcondition: Creates a polynomial which is 0. * **/ public Polynomial() { first = new TermNode(0,0, null); } /**Postcondition: Creates a polynomial which has a single term a0*x^0 * @param a0 The value to be set as the coefficient of the constant (x^0) term. * **/ public Polynomial(double a0) { first = new TermNode(0,a0,null); }

/** Postcondition: Creates a copy of Polynomial p * @param p the Polynomial which is to be copied. * **/

public Polynomial(Polynomial p) { TermNode poly = p.first; while (poly != null) { add_to_coef(poly.coefficient, poly.exponent); poly = poly.next; } }

/** Postcondition: Adds the given amount to the coefficient of the specified exponent. * @param amount The amount to be added to the coefficient. * @param exponent The degree of the term whose coefficient is to be modified. * (1) Note that the exponent can be arbitrary * (2) If you want, you can assume the amount is not 0, however, it is possible that * after you add the amount, the coefficient becomes 0, in which case, you should delete the TermNode * **/

public void add_to_coef(double amount, int exponent) { TermNode poly = first; while (poly.next != null) { if(poly.exponent == exponent) { poly.coefficient = amount; return; } poly = poly.next; } if(poly.exponent == exponent) { poly.coefficient = amount; return; } poly.next = new TermNode(exponent, amount, null); }

/** Postcondition: Sets the coefficient of a specified term to a specified value. * @param coefficient The new value of the coefficient. * @param exponent The degree of the term whose coefficient is to be modified. * (1) Note that the exponent can be arbitrary * (2) The coefficient may be 0 * **/ public void assign_coef(double coefficient, int exponent) {

}

/** Postcondition: Returns coefficient at specified exponent of this polynomial. * @param exponent The exponent of the term whose coefficient is sought. * @return The coefficient of the term. * @throws Exception if the degree of the activating polynomial is less than that of the requested term. * **/

public double coefficient(int exponent) { }

/** @return The value of this Polynomial with the given value for the variable x. * @param x The value at which the Polynomial is to be evaluated. * using Horner's method to evaluation * see the link here * https://en.wikipedia.org/wiki/Horner%27s_method * ***/

public double eval(double x) { TermNode t = first; double sum = t.coefficient; int curExponent = t.exponent; while(t.next!=null) { } return sum; }

/**@return Returns a string representing the polynomial expression with coefficients displayed to the tenths place, * omitting any coefficients that are zero. * If all coefficients are 0, then the zero function is reported. * **/ public String toString() { String s = ""; TermNode poly = first; while (poly != null) { if(poly.coefficient >= 0 && poly != first) { s += "+"; } s += poly.coefficient; s += "x^"; s += poly.exponent; poly = poly.next; } return s; } /**@return Returns a Polynomial that is the sum of p and this Polynomial. * @param p The Polynomial to be added to the activating Polynomial. * **/ public Polynomial add(Polynomial p) { }

/** Postcondition: Returns a new polynomial obtained by multiplying this term and p. For example, if this polynomial is 2x^2 + 3x + 4 and p is 5x^2 - 1x + 7, then at the end of this function, it will return the polynomial 10x^4 + 13x^3 + 31x^2 + 17x + 28. @param p The polynomial to be multiplied. @return The product of the activating Polynomial and p. **/ public Polynomial multiply(Polynomial p) { } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Systems Analysis And Synthesis Bridging Computer Science And Information Technology

Authors: Barry Dwyer

1st Edition

0128054492, 9780128054499

More Books

Students also viewed these Databases questions

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago