Skip to content

Commit

Permalink
Added solutions and fixed the clients
Browse files Browse the repository at this point in the history
  • Loading branch information
mapio committed Oct 31, 2024
1 parent 1f9543e commit 85a43c7
Show file tree
Hide file tree
Showing 16 changed files with 568 additions and 51 deletions.
15 changes: 7 additions & 8 deletions src/main/java/it/unimi/di/prog2/e09/PolyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,26 @@ private PolyClient() {}
/**
* Tests some methods of {@link Poly}.
*
* <p>Starting from term \( t_0 = x + 1 \) reads a list of \( t_i \) of terms from the standard
* input, given as a (coefficient, degree) pairs, and computes the polynomial \( t_0 \cdot t_1
* \cdot t_2 \cdots \), emitting in the standard output the pairs "coefficient degree" for every
* term in the result (in increasing order of degree).
* <p>Starting from term \( t_0 = 0 \) reads a list of \( t_i \) of terms from the standard input,
* given as a (coefficient, degree) pairs, and computes the polynomial \( t_0 + t_1 + t_2 \cdots
* \), emitting in the standard output the pairs "coefficient degree" for every term in the result
* (in increasing order of degree).
*
* @param args not used.
*/

/*- Uncomment the main method once you have implemented the add method in Poly class
public static void main(String[] args) {
Poly result = null;
Poly xp1 = new Poly(1, 1).add(new Poly(-1, 0));
Poly result = new Poly();
try (Scanner s = new Scanner(System.in)) {
while (s.hasNextInt()) {
Poly term = new Poly(s.nextInt(), s.nextInt());
if (result == null) result = term;
else result = result.mul(xp1).add(term);
result = result.add(term);
}
for (int d = 0; d <= result.degree(); d++) System.out.println(result.coeff(d) + " " + d);
}
}
*/
}
13 changes: 6 additions & 7 deletions src/main/java/it/unimi/di/prog2/e09/SparsePolyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,25 @@ private SparsePolyClient() {}
* Tests some methods of {@link SparsePoly}.
*
* <p>Starting from term \( t_0 = x + 1 \) reads a list of \( t_i \) of terms from the standard
* input, given as a (coefficient, degree) pairs, and computes the polynomial \( t_0 \cdot t_1
* \cdot t_2 \cdots \), emitting in the standard output the pairs "coefficient degree" for every
* term in the result (in increasing order of degree).
* input, given as a (coefficient, degree) pairs, and computes the polynomials \( p_{i+1} = p_i
* \codt t_i + t_i \) emitting in the standard output the pairs "coefficient degree" for every
* term (in increasing order of degree) of the last computed polynomial.
*
* @param args not used.
*/

/*- Uncomment the main method once you have implemented the SparsePoly class
public static void main(String[] args) {
SparsePoly result = null;
SparsePoly xp1 = new SparsePoly(1, 1).add(new SparsePoly(-1, 0));
SparsePoly result = new SparsePoly(1, 1).add(new SparsePoly(-1, 0));
try (Scanner s = new Scanner(System.in)) {
while (s.hasNextInt()) {
SparsePoly term = new SparsePoly(s.nextInt(), s.nextInt());
if (result == null) result = term;
else result = result.mul(xp1).add(term);
result = result.mul(term).add(term);
}
for (int d = 0; d <= result.degree(); d++) System.out.println(result.coeff(d) + " " + d);
}
}
*/
}
4 changes: 2 additions & 2 deletions src/main/java/it/unimi/di/prog2/h09/Poly.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public Poly add(Poly q) throws NullPointerException {
* @throws NullPointerException if {@code q} is {@code null}.
*/
public Poly mul(Poly q) throws NullPointerException {
if (q == null) throw new NullPointerException();
if (q == null) throw new NullPointerException("The polynomial must not be null.");
if ((q.degree == 0 && q.coefficient[0] == 0) || (degree == 0 && coefficient[0] == 0))
return new Poly();
Poly result = new Poly(degree + q.degree);
Expand All @@ -150,7 +150,7 @@ public Poly mul(Poly q) throws NullPointerException {
* @throws NullPointerException if {@code q} is {@code null}.
*/
public Poly sub(Poly q) throws NullPointerException {
if (q == null) throw new NullPointerException();
if (q == null) throw new NullPointerException("The polynomial must not be null.");
return add(q.minus());
}

Expand Down
165 changes: 165 additions & 0 deletions src/main/java/it/unimi/di/prog2/s09/Poly.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
Copyright 2024 Massimo Santini
This file is part of "Programmazione 2 @ UniMI" teaching material.
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This material is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this file. If not, see <https://www.gnu.org/licenses/>.
*/

package it.unimi.di.prog2.s09;

import it.unimi.di.prog2.h08.impl.NegativeExponentException;
import java.util.Objects;

/**
* {@code Poly}s are immutable polynomials with integer coefficients.
*
* <p>A typical {@code Poly} is \( p = c_0 + c_1 x + c_2 x^2 + \cdots + c_n x^n \).
*/
public class Poly { // we don't extend Cloneable, see EJ 3.13

/** The array of coefficients, the {@code coeff[i]} is the coefficient of \( x^i \). */
private final int[] coefficient;

/** Initializes this to be the zero polynomial, that is \( p = 0 \). */
public Poly() {
coefficient = new int[1];
}

/**
* Initializes this to be the polynomial \(p = cx^n\).
*
* @param c the coefficient.
* @param n the degree.
* @throws NegativeExponentException if {@code n} &lt; 0.
*/
public Poly(int c, int n) throws NegativeExponentException {
if (n < 0)
throw new NegativeExponentException("Can't create a monomial with negative exponent");
coefficient = new int[n + 1];
coefficient[n] = c;
}

/**
* Initializes a polynomial of given degree (with all coefficients equal to 0).
*
* @param n the degree, must be non-negative.
*/
private Poly(int n) {
coefficient = new int[n + 1];
}

/**
* Returns the degree of this polynomial.
*
* @return the largest exponent with a non-zero coefficient; returns 0 if this is the zero {@code
* Poly}.
*/
public int degree() {
return coefficient.length - 1;
}

/**
* Returns the coefficient of the term of given exponent.
*
* @param d the exponent of the term to consider.
* @return the coefficient of the considered term.
*/
public int coeff(int d) {
if (d < 0 || d >= coefficient.length) return 0;
else return coefficient[d];
}

/**
* Performs polynomial addition.
*
* <p>If \( p \) is this polynomial, returns \( p + q \).
*
* @param q the polynomial to add to this one.
* @return the sum among this and the given polynomial.
* @throws NullPointerException if {@code q} is {@code null}.
*/
public Poly add(Poly q) throws NullPointerException {
Objects.requireNonNull(q, "The polynomial must not be null.");
final Poly larger, smaller;
if (degree() > q.degree()) {
larger = this;
smaller = q;
} else {
larger = q;
smaller = this;
}
int resultDegree = larger.degree();
if (degree() == q.degree()) {
for (int k = degree(); k > 0; k--)
if (coefficient[k] + q.coefficient[k] != 0) break;
else resultDegree--;
}
Poly result = new Poly(resultDegree); // get a new Poly
int i;
for (i = 0; i <= smaller.degree() && i <= resultDegree; i++)
result.coefficient[i] = smaller.coefficient[i] + larger.coefficient[i];
for (int j = i; j <= resultDegree; j++) result.coefficient[j] = larger.coefficient[j];
return result;
}

/**
* Performs polynomial multiplication.
*
* <p>If \( p \) is this polynomial, returns \( p q \).
*
* @param q the polynomial to multiply by this one.
* @return the product among this and the given polynomial.
* @throws NullPointerException if {@code q} is {@code null}.
*/
public Poly mul(Poly q) throws NullPointerException {
Objects.requireNonNull(q, "The polynomial must not be null.");
if ((q.degree() == 0 && q.coefficient[0] == 0) || (degree() == 0 && coefficient[0] == 0))
return new Poly();
Poly r = new Poly(degree() + q.degree());
for (int i = 0; i <= degree(); i++)
for (int j = 0; j <= q.degree(); j++)
r.coefficient[i + j] = r.coefficient[i + j] + coefficient[i] * q.coefficient[j];
return r;
}

/**
* Performs polynomial subtraction.
*
* <p>If \( p \) is this polynomial, returns \( p - q \).
*
* @param q the polynomial to subtract from this one.
* @return the subtraction among this and the given polynomial.
* @throws NullPointerException if {@code q} is {@code null}.
*/
public Poly sub(Poly q) throws NullPointerException {
Objects.requireNonNull(q, "The polynomial must not be null.");
return add(q.minus());
}

/**
* Returns the negate polynomial.
*
* <p>If \( p \) is this polynomial, returns \( -p \).
*
* @return this polynomial multiplied by \( -1 \).
*/
public Poly minus() {
Poly r = new Poly(degree());
for (int i = 0; i <= degree(); i++) r.coefficient[i] = -coefficient[i];
return r;
}
}
52 changes: 52 additions & 0 deletions src/main/java/it/unimi/di/prog2/s09/PolyClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright 2024 Massimo Santini
This file is part of "Programmazione 2 @ UniMI" teaching material.
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This material is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this file. If not, see <https://www.gnu.org/licenses/>.
*/

package it.unimi.di.prog2.s09;

import java.util.Scanner;

/** A class to test some methods of {@link Poly}. */
public class PolyClient {

/** . */
private PolyClient() {}

/**
* Tests some methods of {@link Poly}.
*
* <p>Starting from term \( t_0 = 0 \) reads a list of \( t_i \) of terms from the standard input,
* given as a (coefficient, degree) pairs, and computes the polynomial \( t_0 + t_1 + t_2 \cdots
* \), emitting in the standard output the pairs "coefficient degree" for every term in the result
* (in increasing order of degree).
*
* @param args not used.
*/
public static void main(String[] args) {
Poly result = new Poly();
try (Scanner s = new Scanner(System.in)) {
while (s.hasNextInt()) {
Poly term = new Poly(s.nextInt(), s.nextInt());
result = result.add(term);
}
for (int d = 0; d <= result.degree(); d++) System.out.println(result.coeff(d) + " " + d);
}
}
}
Loading

0 comments on commit 85a43c7

Please sign in to comment.