Write a class Polynomial that stores a polynomial such as p(x) = 5x10 + 9x7 - x
ID: 3605180 • 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 (additional object class) 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 (i.e., return polynomial as a String).
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, to compute p(x) × p(x):
Polynomial q = p.multiply(p);
System.out.println(q.toString());
Running this code should produce the output:
25x20 + 90x17 + 81x14 - 10x11 - 100x10 - 18x8 - 180x7 + x2 + 20x + 100
Add a Polynomial constructor that accepts a polynomial represented as a String. For example, instead of having to use the code shown above, the polynomial p could now be constructed as:
Polynomial p = new Polynomial("5x10 + 9x7 - x - 10");
You may want to consider adding a Term constructor that creates a Term from a String like "5x10".
To compute p(x) × p(x), the code is the same as above, and produces the same output. Here is a sample run (user input in color):
Enter a polynomial: 5x10 + 9x7 - x - 10
25x20 + 90x17 + 81x14 - 10x11 - 100x10 - 18x8 - 180x7 + x2 + 20x1 + 100
So now, you can even build a polynomial calculator!
Explanation / Answer
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.NoSuchElementException;
public class Polynomial
{
private int numOfTerms;
private int lastPower = 0;
private int maxPower = 0;
private LinkedList<Term> terms = new LinkedList<Term>();
public Polynomial()
{
}
public void addTerm(int c, int p)
{
int num = 0;
ListIterator<Term> iterator = terms.listIterator();
Term term = new Term(c,p);
if (numOfTerms == 0) //Our first term.
{
terms.add(term);
}
else //We have more than 1 term, set iterator to right position, and insert our new term.
{
if (term.power < lastPower) //It's alright to just tack the term onto the end of the list.
{
terms.add(term);
}
else if(term.power > maxPower) //Term belongs at the beginning of the list
{
iterator.add(term);
}
else //Need to move iterator to the right position. Iterator starts at the first term, which is the highest power.
{
while(num != 1) //This will keep the iterator moving, till its inbetween the right spot to add our term.
{
try
{
if (term.power < iterator.next().power)
{
System.out.println("moved 1 place over");
iterator.next();
}
else
num = 1;
}
catch (NoSuchElementException e)
{
System.err.println("Got us an error, setting to 1, and exitting");
num = 1;
}
} //We have the correct position now
iterator.add(term);
}
}
if (term.power > maxPower)
{
maxPower = term.power;
}
lastPower = term.power;
numOfTerms++;
}
public String toString()
{
String string = "";
String operator = "+";
for(int i = 0; i < numOfTerms; i++)
{
if (i == 0)
{
string += terms.get(i).coeff + "x^" + terms.get(i).power;
}
else if (i > 0 && terms.get(i).power > 1)
{
string += " + " + terms.get(i).coeff + "x^" + terms.get(i).power;
}
else if (i > 0 && terms.get(i).power == 1)
{
string += " + " + terms.get(i).coeff + "x";
}
else
{
string += " + " + terms.get(i).coeff;
}
}
return(string);
}
public int getMax()
{
return(maxPower);
}
/*Inner Class: Term
*Used to wrap power, and coefficient, into an object
*Has just a simple constructor to set values
*/
private class Term
{
public int power;
public int coeff;
public Term(int c, int p)
{
coeff = c;
power = p;
}
}
public int getSize()
{
return terms.size();
}
public int getTermPower(int i)
{
return terms.get(i).power;
}
public int getTermCoeff(int i)
{
return terms.get(i).coeff;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.