1 Create a class that implements Polynomial and all of its m
1. Create a class that implements Polynomial and all of its methods. (See the Hints section
below for hints).
Hints
Leverage one of Java’s List classes in your Polynomial implementation.
Don’t extend a List class. Instead use a List instance variable.
Create a regular or embedded class that models a polynomial term.
Use the List instance variable to store terms.
Use the List get, add, and listIterator methods when implementing the methods in
your Polynomial interface.
The provided jar file includes code to help you test your implementation. Use this testing
code.
Enable assertions by setting the “-ea” argument for the java interpreter. For
guidance for doing this in Eclipse, see
http://stackoverflow.com/questions/11415160/how-to-enable-the-java-keyword-as
sert-in-eclipse-program-wise
Create an implementation of PolynomialFactory. It should simply have a single
method that returns a newly constructed instance of your Polynomial
implementation.
Write a test method that passes a PolynomialFactory instance to
PolynomialTester.runTests(PolynomialFactory), which should raise an exception
if it finds a problem with your Polynomial implementation.
Solution
import java.util.LinkedList;
import java.util.ListIterator;
public class Poly
{
public Poly()
{
termList = new LinkedList<term>();
}
public Poly(int coeff, int exp)
{
termList = new LinkedList<term>();
term firstTerm = new term(coeff, exp);
termList.addFirst(firstTerm);
}
/* a constructor that defines a polynomial with 3 terms */
public Poly(int c1, int e1, int c2, int e2, int c3, int e3)
{
termList = new LinkedList<term>();
term term1 = new term(c1,e1);
term term2 = new term(c2,e2);
term term3 = new term(c3,e3);
termList.addLast(term1);
termList.addLast(term2);
termList.addLast(term3);
}
/* adding operator methods */
public Poly add(Poly li2) /* return this + li2 */
{
Poly result = new Poly();
ListIterator<term> itr = termList.listIterator();
ListIterator<term> itr2 = li2.termList.listIterator();
term myTerm;
term li2Term;
while (itr.hasNext() && itr2.hasNext()) {
myTerm = itr.next();
itr.previous();
li2Term = itr2.next();
itr2.previous();
if (myTerm.exp == li2Term.exp){
term term1 = new term((myTerm.coeff + li2Term.coeff),myTerm.exp);
result.termList.addLast(term1);
itr.next();
itr2.next();
}
else if (myTerm.exp < li2Term.exp){
term term2 = new term(li2Term.coeff,li2Term.exp);
result.termList.addLast(term2);
itr2.next();
}
else {
term term3 = new term(myTerm.coeff,myTerm.exp);
result.termList.addLast(term3);
itr.next();
}
}
while (itr.hasNext()){
myTerm = itr.next();
term term4 = new term(myTerm.coeff,myTerm.exp);
result.termList.addLast(term4);
}
while (itr2.hasNext()){
li2Term = itr2.next();
term term5 = new term(li2Term.coeff,li2Term.exp);
result.termList.addLast(term5);
}
return result;
}
listiterator can be used to traverse the list in forward and backward direction.
import java.util.LinkedList;
import java.util.ListIterator;
public class ListIteratorExample {
public static void main(String[] args) {
LinkedList<String> linkedlist = new LinkedList<String>();
// Entering elements to LinkedList
linkedlist.add(\"Apple\");
linkedlist.add(\"orange\");
linkedlist.add(\"Banana\");
linkedlist.add(\"mango\");
linkedlist.add(\"Pineapple\");
ListIterator listIt = linkedlist.listIterator();
System.out.println(\"Forward iteration:\");
while(listIt.hasNext()){
System.out.println(listIt.next());
}
System.out.println(\"\ Backward iteration:\");
while(listIt.hasPrevious()){
System.out.println(listIt.previous());
}
}
}
Output:
Forward iteration:
Apple
Orange
Banana
mango
Pineapple
Backward iteration:
Pineapple
mango
Banana
Orange
Apple.
Polynomial Factory
Class PolynomialFactory<BASE extends IRingElement<BASE>>
public class PolynomialFactory<BASE extends IRingElement<BASE>>
extends RingElementFactory<Polynomial<BASE>>


