-
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
43 changed files
with
1,050 additions
and
10 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,67 @@ | ||
/* | ||
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.e10; | ||
|
||
/** | ||
* A <em>queue</em> is a mutable data structure that provides access to its elements in | ||
* first-in/first-out order. | ||
* | ||
* <p>A <em>bounded</em> queue has an upper bound, established when a queue is created, on the | ||
* number of elements that can be stored in the queue. | ||
*/ | ||
public class BoundedIntQueue { | ||
|
||
// EXERCISE: complete following the specification (with particular attention | ||
// to the eventual exceptions) and provide an implementation (including the | ||
// equals, hashCode, and toString methods); add methods that are adequate to | ||
// the specification. | ||
|
||
// Given the boundedness constraint, it is not allowed to use any Java | ||
// Collection Framework class. An array can be used to store the elements in a | ||
// circular buffer (see https://www.wikiwand.com/en/articles/Circular_buffer). | ||
|
||
/** | ||
* Creates a new bounded queue with the given capacity. | ||
* | ||
* @param capacity the capacity of the queue. | ||
* @throws IllegalArgumentException if {@code capacity} is negative. | ||
*/ | ||
public BoundedIntQueue(int capacity) {} | ||
|
||
/** | ||
* Adds an element to the queue. | ||
* | ||
* @param x the element to add. | ||
* @throws IllegalStateException if the queue is full. | ||
*/ | ||
public void enqueue(int x) {} | ||
|
||
/** | ||
* Removes the element at the head of the queue. | ||
* | ||
* @return the element at the head of the queue. | ||
* @throws IllegalStateException if the queue is empty. | ||
*/ | ||
public int dequeue() { | ||
return 0; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/it/unimi/di/prog2/e10/BoundedIntQueueClient.java
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,55 @@ | ||
/* | ||
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.e10; | ||
|
||
/** A class to exercise a {@link BoundedIntQueue}. */ | ||
public class BoundedIntQueueClient { | ||
|
||
/** . */ | ||
private BoundedIntQueueClient() {} | ||
|
||
/** | ||
* Tests some methods of {@link BoundedIntQueue}. | ||
* | ||
* <p>Reads a list of integers from the standard input; if a positive number is read, it is added | ||
* to a bounded queue of size 10; if a negative number is read, it is dequeued and emitted on the | ||
* standard output. When the input terminates, the queue is emitted on the standard output. | ||
* | ||
* @param args not used. | ||
*/ | ||
|
||
/* - Uncomment the following after completing the implementation of BoundedIntQueue | ||
public static void main(String[] args) { | ||
BoundedIntQueue queue = new BoundedIntQueue(10); | ||
try (java.util.Scanner s = new java.util.Scanner(System.in)) { | ||
while (s.hasNextInt()) { | ||
int x = s.nextInt(); | ||
if (x > 0) queue.enqueue(x); | ||
else System.out.println(queue.dequeue()); | ||
} | ||
} | ||
System.out.println(queue); | ||
} | ||
*/ | ||
} |
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,62 @@ | ||
/* | ||
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.e10; | ||
|
||
/** | ||
* A rational number is an immutable number that can be expressed as the quotient or fraction \( p/q | ||
* \) of two {@code int}s, a numerator \( p \) and a non-zero denominator \( q \). | ||
*/ | ||
public class RationalNumber { | ||
|
||
// EXERCISE: complete following the specification (with particular attention | ||
// to the eventual exceptions) and provide an implementation (including the | ||
// equals, hashCode, and toString methods); add methods that are adequate to | ||
// the specification. | ||
|
||
/** | ||
* Creates a new rational number. | ||
* | ||
* @param numerator the numerator. | ||
* @param denominator the denominator. | ||
*/ | ||
public RationalNumber(int numerator, int denominator) {} | ||
|
||
/** | ||
* Returns the sum of this rational number and another one. | ||
* | ||
* @param other the other rational number. | ||
* @return the sum of this rational number and {@code other}. | ||
*/ | ||
public RationalNumber add(RationalNumber other) { | ||
return null; | ||
} | ||
|
||
/** | ||
* Returns the product of this rational number and another one. | ||
* | ||
* @param other the other rational number. | ||
* @return the product of this rational number and {@code other}. | ||
*/ | ||
public RationalNumber mul(RationalNumber other) { | ||
return null; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/it/unimi/di/prog2/e10/RationalNumberClient.java
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,59 @@ | ||
/* | ||
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.e10; | ||
|
||
/** A class to exercise a {@link RationalNumber}. */ | ||
public class RationalNumberClient { | ||
|
||
/** . */ | ||
private RationalNumberClient() {} | ||
|
||
/** | ||
* Tests some methods of {@link BoundedIntQueue}. | ||
* | ||
* <p>Reads a list of pairs of integers from the standard input, corresponding to the numerator | ||
* and the denominator of a sequence of rational numbers \( q_i \). Computes the sequence of | ||
* rational numbers given by \( r_0 = 0 \) and \( r_{i+1} = q_i + r_i \cdot q_i \). Then compares | ||
* the last computed rational number with the rational number given by the pair of integers given | ||
* as command line arguments emitting <samp>true</samp> in the standard output if they are equal, | ||
* <samp>false</samp> otherwise. | ||
* | ||
* @param args the numerator and denominator of the resulting fraction. | ||
*/ | ||
|
||
/* - Uncomment the following after completing the implementation of RationalNumber | ||
public static void main(String[] args) { | ||
RationalNumber expected = | ||
new RationalNumber(Integer.parseInt(args[0]), Integer.parseInt(args[1])); | ||
RationalNumber result = new RationalNumber(0, 1); | ||
try (Scanner sc = new Scanner(System.in)) { | ||
while (sc.hasNextInt()) { | ||
RationalNumber q = new RationalNumber(sc.nextInt(), sc.nextInt()); | ||
result = q.add(result.mul(q)); | ||
} | ||
} | ||
System.out.println(result.equals(expected)); | ||
} | ||
*/ | ||
} |
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,5 @@ | ||
/** | ||
* Esercizi relativi alla lezione 10, per maggiori dettagli si veda il <a | ||
* href="https://prog2.di.unimi.it/diario">diario del corso</a>. | ||
*/ | ||
package it.unimi.di.prog2.e10; |
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
Oops, something went wrong.