Write a class Polynomial that stores a polynomial such as p(x) = 5x10 + 9x7 – x
ID: 3856895 • Letter: W
Question
Write a class Polynomial that stores a polynomial such as p(x) = 5x10 + 9x7 – x - 10
as a linked list of terms. A term contains the coefficient and the power of x. For example, you would store p(x) as
(5,10),(9,7),(-1,1),(-10,0)
Supply methods to add, multiply, and print polynomials. Supply a constructor that makes a polynomial from a single term. For example, the polynomial p can be constructed as
Polynomial p = new Polynomial(new Term(-10, 0));
p.add(new Polynomial(new Term(-1, 1));
p.add(new Polynomial(new Term(9, 7));
p.add(new Polynomial(new Term(5, 10));
Then compute p(x) x p(x)
Polynomial q = p.multiply(p);
q.print();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.util.LinkedList;
import java.util.ListIterator;
/**
A class to represent a polynomial.
*/
public class Polynomial
{
. . .
/**
Constructs an empty polynomial
*/
public Polynomial()
{
. . .
}
/**
Constructs a new polynomial with the given term
@param t the term to initialize the polynomial with
*/
public Polynomial(Term t)
{
. . .
}
/**
Adds the polynomial such that the terms are in sorted order
from highest power to lowest
@param p the polynomial to add
*/
public void add(Polynomial p)
{
. . .
}
/**
Multiplies the given polynomial with this one and returns the result
@param p the polynomial to multiply
@return this * p
*/
public Polynomial multiply(Polynomial p)
{
. . .
}
/**
Prints the polynomial "nicely" so that it reads
from highest term to lowest and doesn't have a
leading "+" if the first term is positive.
*/
public void print()
{
. . .
}
}
/**
A class to to test the Polynomial class.
*/
public class PolynomialTester
{
public static void main(String[] args)
{
Polynomial p = new Polynomial(new Term(-10, 0));
p.print();
System.out.println(" Expected: - 10.0");
p.add(new Polynomial(new Term(-1, 1)));
p.print();
System.out.println(" Expected: - 1.0x - 10.0");
p.add(new Polynomial(new Term(9, 7)));
p.print();
System.out.println(" Expected: 9.0x^7 - 1.0x - 10.0");
p.add(new Polynomial(new Term(5, 10)));
p.print();
System.out.println(" Expected: 5.0x^10 + 9.0x^7 - 1.0x - 10.0");
Polynomial q = p.multiply(p);
q.print();
System.out.println(" Expected: 25.0x^20 + 90.0x^17 + 81.0x^14 - 10.0x^11 - 100.0x^10 - 18.0x^8 - 180.0x^7 + 1.0x^2 + 20.0x + 100.0");
}
}
/**
A class to represent an algebraic term.
*/
public class Term
{
private double coefficient;
private int power;
/**
A constructor to initialize a single term with a given coefficient and power
@param coefficent the coefficient
@param power the power
*/
public Term(double coefficient, int power)
{
this.coefficient = coefficient;
this.power = power;
}
/**
@return the coefficient
*/
public double getCoefficient()
{
return coefficient;
}
/**
@return the power
*/
public int getPower()
{
return power;
}
/**
Multiplies two coefficient together and returns the result
@param t the other term
@return this * t as a term
*/
public Term multiply(Term t)
{
return new Term(coefficient * t.coefficient, power + t.power);
}
/**
Adds the term to this term if the powers are the same
@param t the term to attempt to add
*/
public void addIfSamePower(Term t)
{
if (t.power == power)
{
coefficient += t.coefficient;
}
}
/**
Returns a string representation of the term with a ^ representing the exponent
@return a string representation of a term
*/
public String toString()
{
if (power == 0)
{
return Math.abs(coefficient) + "";
}
else if (power == 1)
{
return Math.abs(coefficient) + "x";
}
else
{
return Math.abs(coefficient) + "x^" + power;
}
}
}
java netbeans
Explanation / Answer
Given below is the completed code for Polynomial.java
You can use the Term.java and PolynomialTester.java from your question and run the PolynomialTester.java. The actual and expected outputs in the test match.
In case of any issues , post a comment. Please rate the answer, if it helped.
Polynomial.java
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
/**
A class to represent a polynomial.
*/
public class Polynomial
{
LinkedList<Term> terms;
/**
Constructs an empty polynomial
*/
public Polynomial()
{
terms = new LinkedList<Term>();
}
/**
Constructs a new polynomial with the given term
@param t the term to initialize the polynomial with
*/
public Polynomial(Term t)
{
terms = new LinkedList<Term>();
terms.add(t);
}
/**
Adds the polynomial such that the terms are in sorted order
from highest power to lowest
@param p the polynomial to add
*/
public void add(Polynomial p)
{
int idx = 0;
boolean added;
Term currTerm;
//compare each newTerm to current term
for(Term newTerm : p.terms)
{
added = false;
while(!added && idx < terms.size()) //as long as the newTerm is not added or we have more terms to compare
{
currTerm = terms.get(idx);
if(currTerm.getPower() == newTerm.getPower()) //if the terms being compared are same power
{
currTerm.addIfSamePower(newTerm);
added = true;
}
else if(newTerm.getPower() > currTerm.getPower()) //if new terms power is more than the current term
{
terms.add(idx, new Term(newTerm.getCoefficient(), newTerm.getPower()));
added = true;
}
//otherwise move on to next term in the existing list since the power of newTerm is lesser than currTerm
idx++;
}
if(!added) //if the newTerm did not match any powers in existing or its power is not in the range of the existing
terms.add(new Term(newTerm.getCoefficient(), newTerm.getPower())); //add to end
}
}
/**
Multiplies the given polynomial with this one and returns the result
@param p the polynomial to multiply
@return this * p
*/
public Polynomial multiply(Polynomial p)
{
Polynomial ans = new Polynomial();
for(Term t1 : p.terms)
{
for(Term t2 : terms)
{
Term t3 = t1.multiply(t2);
ans.add(new Polynomial(t3));
}
}
return ans;
}
/**
Prints the polynomial "nicely" so that it reads
from highest term to lowest and doesn't have a
leading "+" if the first term is positive.
*/
public void print()
{
String str = "";
for(int i = 0; i < terms.size(); i++)
{
Term t = terms.get(i);
if(t.getCoefficient() > 0)
{
if(i != 0)
str += " + " ;
}
else
{
str += " - ";
}
str += t.toString();
}
System.out.print(str);
}
}
output
- 10.0
Expected: - 10.0
- 1.0x - 10.0
Expected: - 1.0x - 10.0
9.0x^7 - 1.0x - 10.0
Expected: 9.0x^7 - 1.0x - 10.0
5.0x^10 + 9.0x^7 - 1.0x - 10.0
Expected: 5.0x^10 + 9.0x^7 - 1.0x - 10.0
25.0x^20 + 90.0x^17 + 81.0x^14 - 10.0x^11 - 100.0x^10 - 18.0x^8 - 180.0x^7 + 1.0x^2 + 20.0x + 100.0
Expected: 25.0x^20 + 90.0x^17 + 81.0x^14 - 10.0x^11 - 100.0x^10 - 18.0x^8 - 180.0x^7 + 1.0x^2 + 20.0x + 100.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.