PLEASE ADD COMMENTS AND JAVADOC COMMENTS!!!!! import java.util.LinkedList; impor
ID: 3915943 • Letter: P
Question
PLEASE ADD COMMENTS AND JAVADOC COMMENTS!!!!!
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;
}
}
}
Explanation / Answer
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))); //calling function
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; //return the results
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.