-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
477 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
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.h08; | ||
|
||
import it.unimi.di.prog2.h08.impl.EmptyException; | ||
|
||
/** | ||
* {@code IntSet}s are mutable, unbounded sets of integers. | ||
* | ||
* <p>A typical IntSet is \( S = \{x_1, \ldots, x_n \} \). | ||
*/ | ||
public class IntSet { | ||
|
||
// Constructors | ||
|
||
/** | ||
* Initializes this set to be empty. | ||
* | ||
* <p>Builds the set \( S = \varnothing \). | ||
*/ | ||
public IntSet() {} | ||
|
||
// Methods | ||
|
||
/** | ||
* Adds the given element to this set. | ||
* | ||
* <p>This method modifies the object, that is: \( S' = S \cup \{ x \} \). | ||
* | ||
* @param x the element to be added. | ||
*/ | ||
public void insert(int x) {} | ||
|
||
/** | ||
* Removes the given element from this set. | ||
* | ||
* <p>This method modifies the object, that is: \( S' = S \setminus \{ x \} \). | ||
* | ||
* @param x the element to be removed. | ||
*/ | ||
public void remove(int x) {} | ||
|
||
/** | ||
* Tells if the given element is in this set. | ||
* | ||
* <p>Answers the question \( x\in S \). | ||
* | ||
* @param x the element to look for. | ||
* @return whether the given element belongs to this set, or not. | ||
*/ | ||
public boolean isIn(int x) { | ||
return false; | ||
} | ||
|
||
/** | ||
* Returns the cardinality of this set. | ||
* | ||
* <p>Responds with \( |S| \). | ||
* | ||
* @return the size of this set. | ||
*/ | ||
public int size() { | ||
return 0; | ||
} | ||
|
||
/** | ||
* Returns an element from this set. | ||
* | ||
* @return an arbitrary element from this set. | ||
* @throws EmptyException if this set is empty. | ||
*/ | ||
public int choose() throws EmptyException { | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
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.h08; | ||
|
||
import java.util.Objects; | ||
|
||
/** A collection of methods for {@link IntSet}s. */ | ||
public class IntSets { | ||
|
||
// See EJ 2.4 | ||
/** . */ | ||
private IntSets() {} | ||
|
||
/** | ||
* Builds a set from an array of elements. | ||
* | ||
* @param a and array of integer elements. | ||
* @return the set containing an entry for every distinct element of {@code a}. | ||
* @throws NullPointerException if {@code a} is {@code null}. | ||
*/ | ||
public static IntSet getElements(int[] a) throws NullPointerException { | ||
Objects.requireNonNull(a, "L'array non può essere null"); | ||
IntSet s = new IntSet(); | ||
for (int i = 0; i < a.length; i++) s.insert(a[i]); | ||
return s; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
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.h08; | ||
|
||
/** A class to test some methods of {@link IntSet}. */ | ||
public class IntSetsClient { | ||
|
||
/** . */ | ||
private IntSetsClient() {} | ||
|
||
/** | ||
* Builds a set from an array of elements. | ||
* | ||
* @param args the elements of the set. | ||
*/ | ||
public static void main(String[] args) { | ||
int[] a = new int[args.length]; | ||
int i = 0; | ||
for (String s : args) a[i++] = Integer.parseInt(s); | ||
IntSet S = IntSets.getElements(a); | ||
System.out.println(S); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
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.h08; | ||
|
||
import it.unimi.di.prog2.h08.impl.NegativeExponentException; | ||
|
||
/** | ||
* {@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 { | ||
|
||
// Constructors | ||
|
||
/** Initializes this to be the zero polynomial, that is \( p = 0 \). */ | ||
public Poly() {} | ||
|
||
/** | ||
* Initializes this to be the polynomial \(p = cx^n\). | ||
* | ||
* @param c the coefficient. | ||
* @param n the degree. | ||
* @throws NegativeExponentException if {@code n} < 0. | ||
*/ | ||
public Poly(int c, int n) throws NegativeExponentException {} | ||
|
||
/** | ||
* A factory method returning a monomial. (see EJ 2.1) | ||
* | ||
* @param c the coefficient. | ||
* @param n the degree. | ||
* @throws NegativeExponentException if {@code n} < 0. | ||
* @return the monomial, if {@code n} >= 0. | ||
*/ | ||
public static Poly monomialWithCoeffAndDegree(int c, int n) { | ||
return null; | ||
} | ||
|
||
// Methods | ||
|
||
/** | ||
* 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 0; | ||
} | ||
|
||
/** | ||
* 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) { | ||
return 0; | ||
} | ||
|
||
/** | ||
* 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 { | ||
return null; | ||
} | ||
|
||
/** | ||
* 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 { | ||
return null; | ||
} | ||
|
||
/** | ||
* 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 { | ||
return null; | ||
} | ||
|
||
/** | ||
* Returns the negate polynomial. | ||
* | ||
* <p>If \( p \) is this polynomial, returns \( -p \). | ||
* | ||
* @return this polynomial multiplied by \( -1 \). | ||
*/ | ||
public Poly minus() { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
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.h08; | ||
|
||
/** A collection of methods for {@link Poly}s. */ | ||
public class Polys { | ||
|
||
// See EJ 2.4 | ||
/** . */ | ||
private Polys() {} | ||
|
||
/** | ||
* Returns the derivative of the given polynomial. | ||
* | ||
* @param p the polynomial to differentiate. | ||
* @return the derivative of {@code p}. | ||
* @throws NullPointerException if {@code p} is {@code null}. | ||
*/ | ||
public static Poly diff(Poly p) throws NullPointerException { | ||
Poly q = new Poly(); | ||
for (int i = 1; i <= p.degree(); i++) q = q.add(new Poly(p.coeff(i) * i, i - 1)); | ||
return q; | ||
} | ||
} |
Oops, something went wrong.