For this project you will implement and test the Polynomial class which uses the provided Term.class. For up to 5 points extra credit, implement and test four selected Term methods based on their specification.
I am providing four files for this project. They are bundled into Pr2.zip (click to download, extract all into your working folder).
protected LinkedList<Term> poly;
The LinkedList class is, like ArrayList, part of the Java Collections Framework in the java.util package. So it has just about all the same methods that ArrayList does. However it is implemented differently. Don't be concerned with the implementation just yet. Remember that you have access to the same variety of methods that ArrayList has. The relationship between Polynomial and LinkedList is "has-a". A polynomial has-a linkedList.
"foreach" loop: The FOR statement would look something like this: for (Term t : poly) then in the body of the loop use variable t to refer to the Term for that iteration. It is updated for each iteration.
Iterator is a java.util class. You can get an Iterator from poly (that's the instance variable), then use its hasNext() and next() methods together in a while loop to process the polynomial term by term. You might guess that boolean method hasNext() will let you know if there are more terms in the polynomial, and Object method next() returns the next term. These two collaborate to traverse the list something like this:
Iterator<Term> iter = poly.iterator(); // start at position 0 of list
while (iter.hasNext()) {
Term term = iter.next();
// do whatever processing you need to do with this
term
}
It seems funny that the terms are coming from the list's iterator. But think of the iterator as something attached to the list that will give you the next list element every time you request one. No need for arrays or index variables!!
Collections.sort(poly, Collections.reverseOrder());
Interesting fact: The sort method determines order by using the Term class compareTo() method! The order is based primarily on the exponent and secondarily on the coefficient. Thus reverse order will sort them from higher to lower exponent.
Hint: You can simplify implementation of toString() by calling the Term class' toString() method on all its terms.
Hint: You can simplify implementation of the equals() method by using toString().
Points | Description |
---|---|
3 | Self-documenting code (useful variable names, indentation), identification |
1 | Polynomial no-parameter constructor |
5 | Polynomial addTerm() method |
5 | Polynomial evaluate() method |
5 | Polynomial toString() method |
5 | Polynomial equals() method |
6 | Polynomial sum() method |
6 | Polynomial product() method |
4 | Polynomial one-parameter constructor |
5 | Bonus: Term methods compareTo(), combine(), sum() and product() |