COMP 2100 Project 2: Working with Polynomials

Fall 2015

Due: end of Lab, Thursday 1 October 2015
40 points + possible 5 points extra credit

Overview

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.

Introduction

For this project you will develop a class to represent and manipulate simple polynomials over the variable X.  A polynomial is modeled by an object of the Polynomial class.  A polynomial consists of a list of terms.  Each term is modeled by an object of the Term class.  A Term object consists of an integer coefficient and an integer exponent.

I am providing four files for this project. They are bundled into Pr2.zip (click to download, extract all into your working folder).

  1. Polynomial.java, a stub version of the Polynomial class. It will compile but not pass any tests.
  2. Term.class, the compiled bytecode of a correct implementation of the Term class.  This is needed because Polynomial implementation cannot begin until Term is working.
  3. TermPolyDriver.java, a test driver. You can use this to test your Polynomial methods. If you choose to do the extra credit, there is a line in main() you will need to uncomment in order to also test the Term methods.
  4. PolynomialForDriver.class, which is needed by the test driver to assure that methods in your solution can be tested in isolation from each other.

Assignment: Implement and test the Polynomial Class

You are to implement the Polynomial class based on its Javadoc-produced Polynomial API. You will compile and test it using the provided Term.class file and its Javadoc-produced Term API. Implementation notes:
  1. The Polynomial API has information you need for implementing the methods.
  2. Use the provided Term.class in your implementation. The Term API is provided so you know how to use its methods, and so you can write its methods if you choose to do the bonus.
  3. The Polynomial class does not extend anything.  It has one instance variable with this declaration:

    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.

  4. You should develop the methods in the order listed in the scoring table. Be sure to test each one before proceeding to the next one.
  5. Most of the methods require you to traverse through the terms of the polynomial.  There are two equivalent ways to do this: a "foreach" loop or an Iterator

    "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!!

  6. The toString() method should produce the polynomial with terms ordered in decreasing order of exponent.  For example: "+3x^2-x+2".  Sorting these is very easy to do, thanks to a java.util class called Collections which provides many handy static methods that can be applied to an object of any of the Java collection classes.  All you need in your program is the line:

    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().

  7. The one-parameter constructor receives a String containing a list of space-separated integers.  The integers are logically grouped in pairs.  Each pair represents a term: the first number of the pair is the coefficient and the second is the exponent.  For example the String "3 2 -5 1 4 0" represents polynomial 3x^2-5x+4.  The trick to implementing this constructor is pulling out those values one by one and converting them from String to int!  There are several ways to do this but the easiest is to create a new Scanner object on the string (there is a constructor for this) then call its nextInt() method twice to retrieve each coefficient-exponent pair as two ints. It works like an iterator; you don't know how many pairs there are so you need to check using hasNext() before attempting to retrieve a pair.

Bonus: Implement the Term Class

Based on the Term API you may implement, for extra credit, the Term methods listed here by editing a partially-completed Term.java file that I will provide upon request after Polynomial is completed and tested.

Scoring

PointsDescription
3 Self-documenting code (useful variable names, indentation), identification
1 Polynomial no-parameter constructor
5Polynomial addTerm() method
5 Polynomial evaluate() method
5 Polynomial toString() method
5 Polynomial equals() method
6Polynomial sum() method
6Polynomial product() method
4Polynomial one-parameter constructor
5Bonus: Term methods compareTo(), combine(), sum() and product()

To Turn In

Drop your completed Polynomial.java (and Term.java if submitting the bonus) file into your DropBox.
[ COMP 2100 | Peter Sanderson | Math Sciences home page | Otterbein ]

Last updated: 
Peter Sanderson (PSanderson@otterbein.edu)