TD Bonus — Polynomial Arithmetic
S7 Inf A3 — Java for Graphical and Mobile Programming
Stéphane Derrode — Centrale Lyon
Stéphane Derrode — Centrale Lyon
For students who are comfortable with Parts 1–3. This subject can be tackled after TD3 or worked on independently. It combines encapsulation (CM1), collections (CM2), and class design principles (CM3).
Context
You will implement a small polynomial arithmetic library. A polynomial is an expression of the form:
P(x) = 5 + 3x − 3.5x²
It is represented as a sum of monomials, where each monomial has the form a·xⁿ (a coefficient a and a degree n).
Example: the polynomial 11.4 − 1.45x² is stored as two monomials: - monomial of degree 0: coefficient 11.4 - monomial of degree 2: coefficient −1.45
Part 1 — The Monomial Class
1.1 Specification
Create src/main/java/com/s7infa3/poly/Monomial.java.
| Member | Type | Description |
|---|---|---|
degree |
int |
Degree of the monomial (≥ 0) — read-only after creation |
coefficient |
double |
Coefficient — can be updated |
Monomial(int degree, double coefficient) |
constructor | Throws IllegalArgumentException if degree < 0 |
getDegree() |
int |
Returns the degree |
getCoefficient() |
double |
Returns the coefficient |
addCoefficient(double value) |
void |
Adds value to the current coefficient |
toString() |
String |
See display rules below |
1.2 Display rules for toString()
| Case | Example output |
|---|---|
| General case | 3.4 X^3 |
| Degree 1 | 3.0 X (not 3.0 X^1) |
| Degree 0 | 2.5 (not 2.5 X^0) |
| Coefficient = 0 | "" (empty string — the monomial produces no output) |
1.3 Test
Monomial m1 = new Monomial(3, 3.4);
System.out.println(m1); // 3.4 X^3
Monomial m2 = new Monomial(1, 3.0);
System.out.println(m2); // 3.0 X
Monomial m3 = new Monomial(0, 2.5);
System.out.println(m3); // 2.5
Monomial m4 = new Monomial(2, 0.0);
System.out.println(m4); // (empty — no output)
m1.addCoefficient(4.7); // 3.4 + 4.7 = 8.1
System.out.println(m1); // 8.1 X^3
// Exception test
try {
new Monomial(-1, 5.0);
} catch (IllegalArgumentException e) {
System.out.println("Caught: " + e.getMessage());
}
Part 2 — The Polynomial Class
2.1 Representation
A Polynomial is represented by an ArrayList<Monomial>. The polynomial is created with a maximum degree maxDegree. It always holds exactly maxDegree + 1 monomials (indices 0 to maxDegree), all initialised with coefficient 0.
Example:
new Polynomial(3)creates a polynomial of degree ≤ 3 with monomials at degrees 0, 1, 2, and 3.
Create src/main/java/com/s7infa3/poly/Polynomial.java.
2.2 Specification
| Member | Description |
|---|---|
Polynomial(int maxDegree) |
Creates the polynomial; throws IllegalArgumentException if maxDegree < 0 |
addMonomial(Monomial m) |
Adds the coefficient of m to the monomial of matching degree; throws IllegalArgumentException if m.getDegree() > maxDegree |
toString() |
See display rules below |
evaluate(double x) |
Returns the numerical value of P(x); use Math.pow(x, n) for xⁿ |
derivative() |
Returns a new Polynomial representing P'(x) |
2.3 Display rules for toString()
- Print only the non-zero monomials, separated by
" + "(or" - "if the next coefficient is negative — optional refinement) - Terms are printed from highest degree to lowest
- If the polynomial is identically zero, return
"0"
Example: the polynomial 11.4 − 1.45x² should display as: -1.45 X^2 + 11.4
2.4 Derivative rules
The derivative of a·xⁿ is n·a·xⁿ⁻¹. In particular:
- derivative of a constant (degree 0) is 0
- the result polynomial has degree maxDegree - 1
2.5 Test
Polynomial p = new Polynomial(3);
p.addMonomial(new Monomial(0, 11.4));
p.addMonomial(new Monomial(2, -1.45));
System.out.println(p); // -1.45 X^2 + 11.4
System.out.println(p.evaluate(1.0)); // 9.95
Polynomial dp = p.derivative();
System.out.println(dp); // -2.9 X
System.out.println(dp.evaluate(2.0)); // -5.8
// Test: add two monomials of the same degree
Polynomial p2 = new Polynomial(2);
p2.addMonomial(new Monomial(1, 3.0));
p2.addMonomial(new Monomial(1, 2.0)); // same degree → coefficients add
System.out.println(p2); // 5.0 X
Part 3 — Polynomial Addition
Add a method Polynomial add(Polynomial other) that returns a new Polynomial equal to this + other.
Rules:
- The result polynomial has maxDegree = Math.max(this.maxDegree, other.maxDegree)
- Add coefficients term by term for matching degrees; copy as-is for degrees only present in one polynomial
Polynomial a = new Polynomial(2);
a.addMonomial(new Monomial(0, 5.0));
a.addMonomial(new Monomial(2, 2.0));
Polynomial b = new Polynomial(3);
b.addMonomial(new Monomial(1, 3.0));
b.addMonomial(new Monomial(2, -1.0));
b.addMonomial(new Monomial(3, 4.0));
Polynomial sum = a.add(b);
System.out.println(sum); // 4.0 X^3 + 1.0 X^2 + 3.0 X + 5.0
Part 4 — Using Comparable<Polynomial> (advanced)
Make Polynomial implement Comparable<Polynomial>, ordering polynomials by their degree (i.e. the highest degree with a non-zero coefficient).
Then create a List<Polynomial>, add several polynomials of different degrees, and sort the list with Collections.sort().
Hints
Initialising the ArrayList in the constructor:
private ArrayList<Monomial> terms = new ArrayList<>();
// fill with maxDegree+1 monomials of coefficient 0:
for (int i = 0; i <= maxDegree; i++) {
terms.add(new Monomial(i, 0.0));
}
Accessing a monomial by degree:
Since the list is indexed by degree, terms.get(n) gives the monomial of degree n directly — no search needed.
Formatting the output:
Use a StringBuilder and iterate from maxDegree down to 0, appending non-zero monomials.
The derivative method:
public Polynomial derivative() {
if (maxDegree == 0) return new Polynomial(0); // constant → zero
Polynomial result = new Polynomial(maxDegree - 1);
for (int n = 1; n <= maxDegree; n++) {
double newCoeff = n * terms.get(n).getCoefficient();
result.addMonomial(new Monomial(n - 1, newCoeff));
}
return result;
}
Deliverable
No formal submission — this is a bonus exercise. If you complete it and want feedback, share your zip with the subject line "TD Bonus Polynôme" via the course Moodle.