Use separate compilation to implement a polynomial ADT to manipulate the followi
ID: 3805121 • Letter: U
Question
Use separate compilation to implement a polynomial ADT to manipulate the following type of polynomials: (1) involving a single variable x, e.g., p = 5.1 * x^10 + 7.2 * x^4 - 11.0 (where the number after the ^ symbol is the exponent/degree of a term); (2) the degree of each term is a non-negative integer and doesn't exceed 200; and (3) the coefficient of each term is a floating number. Please identify a proper data representation schema to store such polynomials and hide such data from external users. Additionally, your ADT is required to include the following member functions:
1) A default constructor, which initializes the polynomial to 0.0
2) A constructor with one integer parameter, which indicates the highest degree allowed of a given polynomial.
3) A read-only accessor degree() that returns the degree of a polynomial, which is defined as the highest power of any term contained in the polynomial with a nonzero coefficient. For instance, the degree of the above underlined polynomial is 10.
4) A read-only accessor getCoeff(int power) that returns the coefficient of the term x^(power).
5) One mutator that interacts with the end user to to obtain a polynomial from the keyboard. For example, if a polynomial contains three terms, your method will ask the end user to type in the three terms one by one. For each term, you are going to prompt the user to type its coefficient and then degree.
6) A mutator setCoeff(int power, double newCoefficient) that sets the coefficient of the term x^(power) to newCoefficient.
7) An overloaded division operator (/) as a member function. This operator divides a polynomial by a scalar variable. Also, this operator is not allowed to change the calling object. For instance, let p = 5.1 * x^10 + 7.2 * x^4 - 11.0, p/2.0 = 2.55 * x^10 + 3.6 * x^4 - 5.50, but p will remain unchanged.
8) An overloaded negation operator (-) as a member function. This operator will negate the coefficient of each term in a polynomial. For instance, given p = 5.1 * x^10 + 7.2 * x^4 - 11.0, -p will not only change p to -5.1 * x^10 - 7.2 * x^4 + 11.0but also returns this negated polynomial.
9) An overloaded addition (+) operator as a member function that sums up two polynomials. This operator is not allowed to change either of the two polynomials participating in the addition and returns the sum as another polynomial. For instance, let p1, p2, p3 be three polynomials, the statement "p3=p1+p2" will not change p1 or p2 and will save the sum to p3.
10) Finally, overload the “put” operator (<<) as a friend of the above Polynomial ADT to print out a polynomial on an output stream.For instance, let p = 5.1 * x^10 + 7.2 * x^4 - 11.0, the statement "cout<
Lastly, in your main() function, please provide a user-friendly interface that allows the grader to test each and every function implemented above.
Use separate compilation to implement a polynomial ADT to manipulate the following type of polynomials: (1) involving a single variable x e.g., p -5.1 x 10 7.2 xn4 11.0 (where the number after the A symbol s the exponent degree of a term); (2) the degree of each term is a non-negative integer and doesn't exceed 200; and 3)the coefficient of each term is a floating number. Please identify a proper data representation schema to store such polynomials and hide such data from external users. Additionally, your ADT is required to include the following member functions 1. A default constructor, which initializes the polynomial to 0.0 A constructor with one integer parameter, which indicates the highest degree allowed of a given polynomial 3. A read-only accessor degree0 that returns the degree of a polynomial, which is defined as the highest power of any term contained in the polynomial with a nonzero coefficient. For instance, the degree of the above underlined polynomial is 10 4. A read-only accessor getCoeff int power) that returns the coefficient of the term x (power) 5. One mutator that interacts with the end user to to obtain a polynomial from the keyboard. For example, if a polynomial contains three terms your method will ask the end user to type in the three terms one b one. For each term, you are going to prompt the user to type its coefficient and then degree 6. A mutator setCoeff (int power, double newCoefficient) that sets the coefficient of the term xn(power) to newCoefficien 7. An overloaded division operator I) as a member function. This operator divides a polynomial by a scalar variable. Also, this operator is not allowed to change the calling object. For instance, let p 5.1 x 10 7.2 4 0 2.55 x 10 3.6 x 4 5.50, but p will remain unchanged 8. An overloaded negation operator as a member function. This operator will negate the coefficient of each term in a polynomial. For instance, given p 5.1 x 10 t 7 2 x 4 11.0, -p will not only change p to -5.1 x 10 7 4 t 11.0but also returns this negated polynomial 9. An overloaded addition operator a a member function that sums up two polynomials. This operator is not allowed to change either of the two polynomials participating in the addition and returns the sum as another polynomial. For instance, let p1, p2, p3 be three polynomials, the statement "p3 p1+p2 w not change pl or p2 and will save the sum to p3. ill 10. Finally, overload the put" operator as a friend of the above Polynomial ADT to print out a polynomial on an output stream.For instance, let p E 5.1 x 10 t 7 2 x 4 11.0, the statement "coutExplanation / Answer
public class Poly implements Function, Comparable { private int[] coefficients = null; private int degree = 0; public Poly(int[] coef) { coefficients = coef; degree = this.checkDegree(); } private int checkDegree() // Any time we create or modify a polynomial, we'll run this to update the degree variable. { int i = 0; for (i = (coefficients.length - 1); i > 0; i--) { if (coefficients[i] != 0) break; } return (i); } public int degree() { return this.degree; } public String toString() { String text = ""; String[] textElement = new String[(degree + 1)]; for (int i = 0; i 0) textElement[0] = textElement[0].substring(0, textElement[0].indexOf("x")); // Chop off the x^0 part, leaving just the coefficient. if ((degree > 0) && (textElement[1].length() > 0)) textElement[1] = textElement[1].substring(0, (textElement[1].indexOf("x") + 1)); // Chop off the ^1 part, leaving just the coefficient and x. if ((degree > 0) && ((coefficients[0] == 1) || (coefficients[0] == -1))) // Having eliminated all of the "1"s, we have to replace them in the constant term. textElement[0] = (textElement[0] + "1"); for (int i = degree; i >= 0; i--) text = text + textElement[i]; return (text); } public Poly add(Poly a) { if (degreeRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.