Objectives To create a linked-chain data structure To understand how to apply ma
ID: 3860417 • Letter: O
Question
Objectives To create a linked-chain data structure To understand how to apply mathematical algorithms Instructions polynomial. The Polynomial class must support the following: For this program, you will implement two classes, one representing a single term, and one representing a 2 constructors o A constructor that takes two parameters: exponent and coefficient Recall that if we have, for example, the term 3x2 then 3 is the coefficient, and 2 is the exponent o A copy constructor This constructor will take, as its argument, a reference to the Polynomial which it is supposed to copy .A print method o Operations The method should print the polynomial o clone method -This method is inherited from Object You must override it to work correctly It copies and returns a new Polynomial object, which is a (deep) copy of the original that it is cloningExplanation / Answer
Given below is the code for question. Please rate if it helped. thank you.
Polynomial.java
//A class to represent a polynomial.
public class Polynomial implements Cloneable
{
private Term firstTerm;
public Polynomial(int coefficient, int exponent)
{
firstTerm = new Term(coefficient, exponent);
}
//copy constructor
public Polynomial(Polynomial poly)
{
add(poly);
}
private void add(Term t2)
{
if(firstTerm == null)
{
firstTerm = new Term(t2.getCoefficient(), t2.getExponent());
return;
}
Term t1 = firstTerm, prev = null;
while( t1 != null)
{
if(t1.getExponent() == t2.getExponent())
{
t1.setCoefficient(t1.getCoefficient() + t2.getCoefficient());
return;
}
else if(t1.getExponent() > t2.getExponent())
{
prev = t1;
t1 = t1.getNext();
}
else{
break;
}
}
Term newT = new Term(t2.getCoefficient(), t2.getExponent());
if(prev == null)
{
newT.setNext(firstTerm);
firstTerm = newT;
}
else
{
newT.setNext(prev.getNext());
prev.setNext(newT);
}
}
/*
Adds the polynomial such that the terms are in sorted order
from highest power to lowest
*/
public void add(Polynomial p)
{
if(p == null)
return;
for(Term t = p.firstTerm; t != null; t = t.getNext())
add(t);
}
/*
Prints the polynomial highest term to lowest and doesn't have a
leading "+" if the first term is positive.
*/
public void print()
{
String str = "";
for(Term t = firstTerm; t != null; t = t.getNext())
{
if(t.getCoefficient() > 0)
{
if(t != firstTerm)
str += " + " ;
}
else
{
str += " - ";
}
str += t.toString();
}
System.out.print(str);
}
@Override
protected Object clone() throws CloneNotSupportedException {
Polynomial newPoly = new Polynomial(null);
newPoly.add(this);
return newPoly;
}
//inner class for each Term in polynomial
public class Term
{
private int coefficient;
private int exponent;
private Term next; //next term's link
public Term(int coefficient, int exponent)
{
this.coefficient = coefficient;
this.exponent = exponent;
this.next = null;
}
public int getCoefficient()
{
return coefficient;
}
public int getExponent()
{
return exponent;
}
public Term getNext()
{
return next;
}
public void setNext(Term n)
{
next = n;
}
public void setCoefficient(int coeff)
{
coefficient = coeff;
}
public String toString()
{
if (exponent == 0)
{
return Math.abs(coefficient) + "";
}
else if (exponent == 1)
{
return Math.abs(coefficient) + "x";
}
else
{
return Math.abs(coefficient) + "x^" + exponent;
}
}
}
}
TestPolynomial.java
public class TestPolynomial {
public static void main(String[] args) {
//set p1 = 3x^2 + 2x + 5
Polynomial p1 = new Polynomial(3, 2) ; //3x^2
p1.add(new Polynomial(2,1)); //add 2x
p1.add(new Polynomial(5,0)); //add 5
System.out.print(" p1 = ");
p1.print();
//set p2 = 3x + 2x
Polynomial p2 = new Polynomial(3, 1) ; //3x
p2.add(new Polynomial(2,0)); //add 2
System.out.print(" p2 = ");
p2.print();
System.out.println(" adding p2 to p1");
p1.add(p2);
System.out.print(" now p1 = ");
p1.print();
}
}
output
p1 = 3x^2 + 2x + 5
p2 = 3x + 2
adding p2 to p1
now p1 = 3x^2 + 5x + 7
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.