Need help with this assigment, arraylist needs to be used, not arrays or lists,
ID: 3882143 • Letter: N
Question
Need help with this assigment, arraylist needs to be used, not arrays or lists, etc
The Term Class
Create a class to represent a term in an algebraic expression. As defined here, a term consists of the variable x, an integer coefficient, and a nonnegative integer exponent. E.g.
in the term 4x2, the coefficient is 4 and the exponent 2
in -6x8, the coefficient is -6 and the exponent 8
Your class will have a constructor that creates a Term object with a coefficient and exponent passed as parameters, and accessor methods that return the coefficient and the exponent, respectively. Your class will also override toString to return a Term in this general form:
ax^b where a is the coefficient and b the exponent
with these special cases:
Case
Returns
a = 1
x^b
b = 1
ax
b = 0
a
a = 1, b = 1
x
You may assume that the coefficient will not be zero.
The Polynomial Class
A “skeleton” of the Polynomial class you are to use is on the class web site. All you have to do is write the bodies of the methods.
To get credit for this assignment, your Polynomial class must use an ArrayList-of-Term as its principal data structure
To get credit, you must not declare any additional instance variables or methods and must not modify any of the method declarations in any way (not necessary).
As defined here, a polynomial is a sequence of terms. E.g.
1.
2.
3.
The terms of polynomial 1 are (3,2), (4,4) and (1,6). The terms of polynomial 2 are (2,0), (5,2), (6,3) and (2,7). Polynomial 3 has only one term (4,10)
Note that the Polynomial class has:
A constructor that creates an empty Polynomial (a Polynomial with 0 terms)
A method with signature
public void addTerm(int coefficient, int exponent)
which creates a Term and places it in its proper position in the Polynomial
The terms of the Polynomial are stored in ascending order by exponent (see above) so you never have to “sort” the list. Terms with the same exponent may be stored in any order but will appear after all terms with lesser exponents and before all terms with greater exponents
A method with signature
public Polynomial polyAdd(Polynomial p)
which adds Polynomial p to this Polynomial and returns the sum
A method with signature
public Polynomial polyMultiply(Polynomial p)
which multiplies this Polynomial by p and returns the product
An overridden toString method that returns a String representation of a polynomial as a sum of terms. For example polynomial 1 above would have this String representation:
3x^2 + 4x^4 + x^6
A private method with signature
private void collectTerms()
which “collects” like terms of this Polynomial. E.g.
Before: x^2 + 3x^2 + 2x^2 + 3x^3 + 5x^4 + 2x^4
After: 6x^2 + 3x^3 + 7x^4
Polynomials must always be printed with terms collected
The Test Class
Test class PolynomialTester.java is available online. Make no changes to the test class.
Javadoc Comments
Make sure you properly document the Polynomial and Term classes and all public methods, including all parameters, and all return statements. Then run the javadoc utility to generate the “help” pages (Polynomial.html and Term.html).
Case
Returns
a = 1
x^b
b = 1
ax
b = 0
a
a = 1, b = 1
x
Explanation / Answer
Given below is the completed code for Polynomial.java
Please do rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
import java.util.ArrayList;
public class Polynomial
{
// do NOT modify this instance variable declaration
private ArrayList polynomial ;
public Polynomial() // constructor
{
// TO DO: write body of this constructor
polynomial = new ArrayList();
}
public void addTerm(int coeff, int expo)
{
polynomial.add(new Term(coeff, expo));
}
public String toString()
{
collectTerms();
String s = "";
for(int i = 0; i < polynomial.size(); i++)
s += " " + polynomial.get(i);
return s;
}
// collect terms of a Polynomial object. I.e. replace all terms having the
// same exponent with a single term which is their sum
private void collectTerms()
{
for(int i = 0; i < polynomial.size(); i++)
{
Term t1 = (Term)polynomial.get(i);
int expo = t1.getExponent();
int coeff = t1.getCoefficient();
for(int j = i+1; j < polynomial.size(); )
{
Term t2 = (Term) polynomial.get(j);
if(t2.getExponent() == expo)
{
coeff += t2.getCoefficient();
polynomial.remove(j);
}
else
j++;
}
if(coeff == 0)
polynomial.remove(i);
else if(coeff != t1.getCoefficient())
polynomial.set(i, new Term(coeff, expo));
}
}
public Polynomial polyMultiply(Polynomial p)
{
collectTerms();
p.collectTerms();
Polynomial result = new Polynomial();
for(int i = 0; i < polynomial.size(); i++)
{
Term t1 = (Term)polynomial.get(i);
for(int j = 0; j < p.polynomial.size(); j++)
{
Term t2 = (Term)p.polynomial.get(j);
int coeff = t1.getCoefficient() * t2.getCoefficient();
int expo = t1.getExponent() + t2.getExponent();
result.addTerm(coeff, expo);
}
}
result.collectTerms();
return result;
}
public Polynomial polyAdd(Polynomial p)
{
Polynomial result = new Polynomial();
for(int i = 0; i < polynomial.size(); i++)
{
Term t = (Term)polynomial.get(i);
result.addTerm(t.getCoefficient(), t.getExponent());
}
for(int i = 0; i < p.polynomial.size(); i++)
{
Term t = (Term)p.polynomial.get(i);
result.addTerm(t.getCoefficient(), t.getExponent());
}
result.collectTerms();
return result;
}
} // end of Polynomial class definition =========================
output of PolynomialTester.java
=========
p1 =
p2 =
p1 + p2 =
p1 =
p2 =
p1 * p2 is
p0 = +1x^2 +5x^0 +4x^1
p1 = +6x^2 +4x^5 +3x^3 +5x^6
p2 = +3x^8 +2x^5 +1x^2
p1+p2 = +7x^2 +6x^5 +3x^3 +5x^6 +3x^8
p1 = +6x^2 +4x^5 +3x^3 +5x^6
p2 = +3x^8 +2x^5 +1x^2
p1*p2 = +26x^10 +16x^7 +6x^4 +12x^13 +19x^11 +11x^8 +3x^5 +15x^14
p2 = +3x^8 +2x^5 +1x^2
p2*p2 = +9x^16 +12x^13 +10x^10 +4x^7 +1x^4
p0 = +1x^2 +5x^0 +4x^1
p2 = +3x^8 +2x^5 +1x^2
p0*p2 = +3x^10 +2x^7 +1x^4 +15x^8 +10x^5 +5x^2 +12x^9 +8x^6 +4x^3
p0 = +1x^2 +5x^0 +4x^1
p2 = +3x^8 +2x^5 +1x^2
p0+p2 = +2x^2 +5x^0 +4x^1 +3x^8 +2x^5
After p1 = p1+p2 p1 = +7x^2 +6x^5 +3x^3 +5x^6 +3x^8
After p2 = p2*p2 p2 = +9x^16 +12x^13 +10x^10 +4x^7 +1x^4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.