Homework 4

Learning Objectives

In this homework, you will apply the concepts you learned in class to reinforce your understanding of:

Description

Polynomials are a common type of mathematical function. A polynomial involving a single variable can be represented as an array of terms, where the index of the array identifies the degree for that term. For example, we could represent 3x2 + 2x with an array, coeffs, containing three elements:

coeff[0]=0
coeff[1]=2
coeff[2]=3

That is because polynomial 3x2 + 2x is equivalent to 3x2 + 2x1 + 0x0.

This representation works well for many but not all situations. For example, this representation would not work for polynomails with negative exponents (because here we represent the exponent as the index into the coefficient array). Additionally, this would be an inefficient representation of polynomials that have a hight degree but only a few terms, such as 5x1000+3. This means that we may need multiple implementations of a polynomial, depending on various factors.

For this assignment, I am providing an abstract Polynomial class. Your job is to implement a DensePolynomial class using the representation discussed above (with the limitations discussed above). From the computer science stand point, a dense polynomial is the polynomial whose zero coefficients are stored explicitly, as in our example above.

Requirements

Inheritance

Your DensePolynomial must extend my abstract Polynomial class and implement all of Polynomial's abstract methods.

Liskov Substitution Principle

Some of the Polynomial's methods have Polynomial as a parameter, for example

public Polynomial add (Polynomial q);

Your implementation of these methods should not violate the Liskov Substitution Principle, meaning that the Polynomial object passed as an argument to such a method could be any kind of Polynomial (perhaps not a DensePolynomial). This means that you must operate on the argument that was passed in at the abstract Polynomial level. You may find it helpful to add private methods to your DensePolynomial class, but you should not create any additional public methods and you cannot modify my abstract Polynomial class.

Constructor

Additionally, you will need to add a public constructor to your DensePolynomail class. This constructor must be of the form

/**
  * Constructs a polynomial with the given coffeficients in front of the given exponents
  * The lengths of coefficients and exponents arrays are expected to be the same.
  * If not, the constructor should throw an IllegalArgumentException.
  * The values of coefficients array are the coefficients that correspond to the values in the exponents array.
  * For example, to construct a polynomial 5x1000+3, the coefficeints would be [5,3] and exponents would be [1000,0].
  */
public DensePolynomial(double []coeffitients, int []exponents)

toString

You should implement a toString method in your DensePolynomial class. This method should display a polynomial in a standard mathematical way. Terms should be sorted by exponent, terms with a zero coefficient should not be printed, there should be at most one term printed per exponent. For example, the following is in canonical form:
2x3 + 3x + 1
while these are not:

Unit Tests

You must also create a DensePolynomialTest class that thoroughly tests you DensePolynomial code using JUnit. Remember to keep yout test cases small and create separate test cases to test different scenarios. Of course, if in the process of writing unit tests you discover bugs in your code, you should fix those bugs. To ease the process of unit testing, in your DensePolynomial class, you will need to overrride the equals method of the Object class. Below is an example of how getting started:
public boolean equals(Object o)
{
    DensePolynomial denseP = (DensePolynomial)o;
    // now add the code for comparing this object with denseP
}

Submitting your solution

Submit your DensePolynomial.java and DensePolynomialTest.java into hw3 directory of your git repos. You will need to have my original Polynomial.java file in that directory in order for your code to work, but you don't have to submit it (although, it will not hurt your grade if you do submit it).

Grading

All submitted code must compile. Code that does not compile will receive a grade of zero. I will be compiling your submissions with the following command: javac DensePolynomial.java
javac -cp $CLASSPATH DensePolynomialTest.java

Here, CLASSPATH will be set '/usr/share/java/junit.jar:.' (if running on hopper).

I will use the list of questions below. Each question can score you from 0 to 2 points: No - 0, Somewhat - 1, Yes - 2. The sum of these points will be your grade for this homework.

  1. Does your DensePolynomial class extend my abstract Polynomial class?
  2. Does your DensePolynomial class follow the Liskov Substitution Principle?
  3. Did you include the required constructor in your DensePolynomial class?
  4. Does each method of the DensePolynomial class work correctly? (Here, I'm referring to the methods defined by the abstract Polynomial class). The abstract Polynomial class contains 7 methods. For the grading purposes, it's 2 points per method, for a total of 14 points.
  5. Does your implementation of toString method match my specification?
  6. Did you include a JUnit style test: DensePolynomialTest class?
  7. Did you include a test case for each method of the Polynomial class? The abstract Polynomial class contains 7 methods. For the grading purposes, it's 2 points per method for a total of 14 points.
  8. Did you follow the unit testing principles when writing DensePolynomialTest class?