EE 564, Homework 2: Data Abstraction (out Jan. 22, due Jan. 29)


1. Are the following statements true? If you answer no, explain why. (2%)

(a) A professor says as long as using code does not modify the representation of a data type, we have achieved as much as possible from data abstractions. Is this true?

(b) To avoid representation exposure, all you need to do is making all data members private.

2. There are four categories of operations for data abstractions: creator, mutator, observer, and producer. Select two classes from the Java Standard Edition and categorize their instance methods into these four. (2%)

3. Programming (6%)

Implement a class Poly that can be used to represent an integer polynomial such as 0, 2+x, or 5 + 2x^8 + 3x^14. It is anticipated that this class will be used to store mostly sparse polynomials, which miss a large number of terms between two consecutive ones (for example, 5 + 2x^8 + 3x^14 misses 12 terms for x, x^2, x^3, ..., x^7, x^9, ..., x^13 ). Thus it is decided that the rep for Poly should consist of two vectors: exponents and coeffs, to store the exponent and coefficient of each term of a polynomial, respectively. The values of the exponents vector for the three sample polynomials would be <0>, <0, 1>, and <0, 8, 14>, and coeffs <0>, <2, 1>, <5, 2, 3>.

To facilitate testing, put Poly into a package called clarkson.math.

The following is a partial specification for the Poly class:

public class Poly {
/**
 * OVERVIEW:
 * A typical polynomial is c0 + c1 X + c2 X^2 + ...

 * The rep invariant is:

         PUT YOUR DEFINITION HERE

 *  The abstraction function is: 

         PUT YOUR DEFINITION HERE

 */

/**
 * If e<0 then throws NegativeDegreeException.
 * if c==0 && e!=0 throws ZeroCoeffException.
 * else initializes this to be the polynomial c X^e.
 * @param c coefficient
 * @param e exponent
 */
public Poly(int c, int e)

/**
 * Initialize this to be a zero polynomial.
 */
public Poly()

/**
 * If p is null throws NullPointerException; else returns the Poly this+p
 * @param p a polynomial object
 * @return this+p
 */
public Poly add(Poly p)
 
/**            
 * @return the degree of this, i.e., the largest exponent with a non-zero coefficient.                 
 * Returns 0 if this is the zero Poly.
 */            
public int degree()

/**
 * @param d exponent
 * @return the coefficient of the term of this whose exponent is d.
 * Returns 0 if d is less than 0 or greater than the degree of this.
 * E.g. (7x^2+2X^3).coeff(2)=7
 */
public int coeff(int d)

// EFFECTS: prints a polynomial in ascending order of its exponents. E.g 2 + 2x + 3x^2
public String toString()

public boolean repOk()     

} // end of class Poly


Your solution will be marked according to the following criteria:
(1) Correctness: How many test cases does it pass?   (4%)
(2) Clarity of implementation: spec. of rep invariant and abstraction function, comments, coding style (2%)