PolynomialTester.java /** A class to to test the Polynomial class. */ public cla
ID: 3857778 • Letter: P
Question
PolynomialTester.java
/**
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");
}
}
Polynomial.java
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()
{
. . .
Term.java
}
}
/**
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;
}
}
}
Explanation / Answer
Below is your code.Note that I have changed Term.java and Polynomial.java : -
PolynomialTester.java
/**
* 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");
}
}
Term.java
/**
* A class to represent an algebraic term.
*/
public class Term implements Comparable<Term>, Cloneable{
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 Object clone() {
Term t = new Term(this.getCoefficient(),this.getPower());
return t;
}
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;
}
}
@Override
public int compareTo(Term o) {
if(this.power > o.power) {
return -1;
} else if(this.power < o.power) {
return 1;
} else {
if (this.coefficient > o.coefficient) {
return -1;
} else if(this.coefficient < o.coefficient) {
return 1;
} else {
return 0;
}
}
}
public void setCoefficient(double coefficient) {
this.coefficient = coefficient;
}
public void setPower(int power) {
this.power = power;
}
}
Polynomial.java
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
/**
* A class to represent a polynomial.
*/
public class Polynomial {
private LinkedList<Term> terms = new LinkedList<Term>();
/**
* Constructs an empty polynomial
*/
public Polynomial() {
terms = new LinkedList<>();
}
/**
* Constructs a new polynomial with the given term
*
* @param t
* the term to initialize the polynomial with
*/
public Polynomial(Term t) {
terms = new LinkedList<>();
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) {
ListIterator<Term> p1, p2;
Polynomial result = new Polynomial();
int i;
for (p1 = terms.listIterator(); p1.hasNext();) // create new polynomial
result.terms.add((Term) p1.next().clone()); // out of copies
for (p2 = p.terms.listIterator(); p2.hasNext();) // of this
result.terms.add((Term) p2.next().clone()); // polynomial and
// polyn2;
for (i = 0, p1 = result.terms.listIterator(); p1.hasNext(); i++, p1 = result.terms.listIterator(i)) {
Term term1 = p1.next();
for (p2 = p1; p2.hasNext();) {
Term term2 = p2.next();
if (term1.equals(term2)) {
term2.setCoefficient(term2.getCoefficient() + term1.getCoefficient());
result.terms.remove(term1);
if (term2.getCoefficient() == 0) // remove terms with zero
result.terms.remove(term2); // coefficients;
i = -1; // to become i = 0 after autoincrement;
break;
}
}
}
Collections.sort(result.terms);
this.terms = result.terms;
}
/**
* 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 returnPoly = new Polynomial();
Iterator<Term> thisIter = this.terms.iterator();
while (thisIter.hasNext()) {
Term thisTerm = (Term) thisIter.next();
Iterator<Term> rhsIter = p.terms.iterator();
while (rhsIter.hasNext()) {
Term rhsTerm = (Term) rhsIter.next();
returnPoly.insertTerm(
thisTerm.getCoefficient() * rhsTerm.getCoefficient(),
thisTerm.getPower() + rhsTerm.getPower() );
}
}
return returnPoly;
}
/**
* 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 returnString = "";
Iterator<Term> termsIterator = terms.iterator();
// handle the zero polynomial case
if (!termsIterator.hasNext()) {
System.out.println("0");
}
assert termsIterator.hasNext();
// handle the first term
Term term = (Term) termsIterator.next();
// handle the coefficient
double coef = term.getCoefficient();
assert (coef != 0);
if (coef < 0) {
returnString = returnString + " - ";
coef = -coef;
}
// ASSERT: coef is the absolute value of term.getCoefficient()
if (coef != 1.0 || term.getPower() == 0) {
returnString = returnString + coef;
} else if(coef == 1.0) {
returnString = returnString + "1.0";
}
// handle the exponent
if (term.getPower() == 0) {
;
} else if (term.getPower() == 1) {
returnString = returnString + "x";
} else {
if (term.getPower() < 0) {
returnString = returnString + "x^(" + term.getPower() + ")";
} else {
// exponent is >= 2
returnString = returnString + "x^" + term.getPower();
}
}
while (termsIterator.hasNext()) {
term = (Term) termsIterator.next();
// handle the coefficient
coef = term.getCoefficient();
assert (coef != 0);
if (coef < 0) {
returnString = returnString + " - ";
coef = -coef;
} else {
returnString = returnString + " + ";
}
// ASSERT: coef is the absolute value of term.getCoefficient()
if (coef != 1.0 || term.getPower() == 0) {
returnString = returnString + coef;
} else if(coef == 1.0) {
returnString = returnString + "1.0";
}
// handle the exponent
if (term.getPower() == 0) {
;
} else if (term.getPower() == 1) {
returnString = returnString + "x";
} else {
if (term.getPower() < 0) {
returnString = returnString + "x^(" + term.getPower() + ")";
} else {
// exponent is >= 2
returnString = returnString + "x^" + term.getPower();
}
}
}
System.out.println(returnString);
}
public void insertTerm(double coefficient, int exponent) {
// don't insert the term if the coefficient is 0
if (coefficient == 0) {
return;
}
ListIterator<Term> it = terms.listIterator();
while (it.hasNext()) {
Term term = it.next();
if (term.getPower() < exponent) {
// insert new term before this one
it.previous();
it.add(new Term(coefficient, exponent));
return;
} else if (term.getPower() == exponent) {
// add coefficient of current term and the provided coefficient
if ((term.getCoefficient() + coefficient) != 0) {
term.setCoefficient( term.getCoefficient() + coefficient );
} else {
// term should be eliminated, since its coefficient is 0
it.remove();
}
return;
}
}
// went through the entire list without finding a term with
// exponent less than exponent:
// add new term at end of list.
it.add( new Term(coefficient, exponent));
}
}
Sample Ourput:
- 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