Skip to content

Commit

Permalink
Lecture 10
Browse files Browse the repository at this point in the history
  • Loading branch information
mapio committed Nov 6, 2024
1 parent 02c2404 commit 500e81e
Show file tree
Hide file tree
Showing 43 changed files with 1,050 additions and 10 deletions.
67 changes: 67 additions & 0 deletions src/main/java/it/unimi/di/prog2/e10/BoundedIntQueue.java
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 src/main/java/it/unimi/di/prog2/e10/BoundedIntQueueClient.java
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);
}
*/
}
62 changes: 62 additions & 0 deletions src/main/java/it/unimi/di/prog2/e10/RationalNumber.java
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 src/main/java/it/unimi/di/prog2/e10/RationalNumberClient.java
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));
}
*/
}
5 changes: 5 additions & 0 deletions src/main/java/it/unimi/di/prog2/e10/package-info.java
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;
2 changes: 1 addition & 1 deletion src/main/java/it/unimi/di/prog2/h09/PolyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static void main(String[] args) {
Poly term = Poly.monomialWithCoeffAndDegree(s.nextInt(), s.nextInt());
result = result.mul(term);
}
for (int d = 0; d <= result.degree(); d++) System.out.println(result.coeff(d) + " " + d);
}
for (int d = 0; d <= result.degree(); d++) System.out.println(result.coeff(d) + " " + d);
}
}
Loading

0 comments on commit 500e81e

Please sign in to comment.