Anton Golovin (anton.golovin@gmail.com) SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
I hate signatures!
Fred Kleinschmidt wrote:First, there is really no need for a PolynomialTerm class; an nth order polynomial can be implemented using an array of n+1 values.
Piet Souris wrote:
My first suggestion is to add a Comparator<PolynomialTerm> to the code. If you have a Polynomial, then sort its terms with this comparator (nice if the terms with highest exponent come first in the list, just like in real maths).
Then, if you want to add two polynomials, sort both lists and then construct a new polynomial, in a way that you would do the merge of two arrays in a mergeSort. So, if you have thee two polynomials,
p1 = (a0, e0), (a1, e1), ...
p2 = (b0, f0), (b1, f1), ...
create a new List, and do:
if (e0 > f0) list.add((a0, e0))
else if (e0 == f0) list.add (a0 + b0, e0)
else ...
et cetera
I hate signatures!
Piet Souris wrote:
So, since the sum of two PolynomialTerms is not always another Polynomial, lets remove that method there and put it in the Polynomial class.
And about the use of a Comparator (or implementing Comparable): it is handy to get your List of Polynomials sorted according to the exponents. So, a Comparator could be like:
and, having a Polynomial with a list, you can simply say: list.sort(c);
A Polynomial list will then be of the form (say) 3x^4 + 5x^3 + 2x + 3
and when adding two Polynomials, involving merging the lists, you can simply compare the first terms of both lists, to see if the exponents are equal or not.
Serge Metellus wrote:
Paul Clapham wrote:your PolynomialTerm's Plus method is incorrectly designed. That's because the sum of two polynomial terms (say 2x and x^2) is not necessarily a polynomial term itself (in the example, the sum is 2x + x^2 which is not a polynomial term)
how do i go about fixing this method, i have tried a number of different ways but i can't figure out how to return two terms
I hate signatures!
Specific Requirements
1. Write a customized exception class PolynomialException for use with polynomials.
2. Write a class PolynomialTerm to represent one term of a Polynomial, each with a nonnegative integer exponent and a nonzero integer coefficient:
Instance variables of type int for the exponent and coefficient, and accessors.
A constructor that enforces the constraints described above
Method value( .. ) to evaluate a PolynomialTerm for a given int value of x.
Method plus( .. ) to return the sum of two PolynomialTerms.
Method times( .. ) to return the product of two PolynomialTerms.
Method derivative() to return the derivative of a PolynomialTerm.
Implements Comparable based on exponents only.
3. Write a class Polynomial to represent a Polynomial:
A customized linked list to store PolynomialTerms in descending order. The sole instance variable stores a reference to the first term.
A parameterless constructor that creates the 0polynomial (no terms).
A constructor public Polynomial(int[] data).The data parameter is an array of alternating exponents and coefficients; each pair of consecutive ints defines one PolynomialTerm. E.g. [1, 5, 3, 2, 0, 2, 2, 1] for Example 2 above. The terms may be in any order, but always with exponent first then coefficient.
Helper insert(PolynomialTerm term) to insert a new PolynomialTerm; throw an exception if the exponent of the new term matches an existing one.
Method isZero() to return true iff a Polynomial is the zeropolynomial.
Method value( .. ) to evaluate a Polynomial for a given int value of x.
Method plus( .. ) to return the sum of a pair of Polynomials.
Method times( .. ) to return the product of a pair of Polynomials.
Method derivative() to return the derivative of a Polynomial.
Algorithm Notes
1. Adding PolynomialTerms: (e, c1) + (e, c2) = (e, c1 + c2). The exponents must be the same. If the coefficient sum c1 + c2 = 0, the sum of the terms is null.
2. Multiplying PolynomialTerms: (e1, c1) * (e2, c2) = (e1 + e2, c1 * c2).
3. Adding Polynomials: Combines like terms â€“ add terms with the same exponent.
4. Multiplying Polynomials: Let P(x) = p1(x) + Pâ€™(x), p1(x) the 1st term, Pâ€™(x) the rest.
Then, P(x) * Q(x) = p1(x) * Q(x) + Pâ€™(x) * Q(x) .
5. Consider providing recursive implementations of the Polynomial methods.
I hate signatures!
Paul Clapham wrote:
So after all of the toandfro in this thread, what code do you have now, and what questions do you still have?
1. Adding PolynomialTerms: (e, c1) + (e, c2) = (e, c1 + c2). The exponents must be the same. If the coefficient sum c1 + c2 = 0, the sum of the terms is null.
I hate signatures!
I hate signatures!
Serge Metellus wrote:well after you wrote that and that the requirements specified 1 single variable repesenting the first term so i can only have one variable.
I hate signatures!
I hate signatures!
I hate signatures!
I hate signatures!
I am mighty! And this is a mighty small ad:
Thread Boost feature
https://coderanch.com/t/674455/ThreadBoostfeature
