Skip to content

Commit

Permalink
Lecture 15 and exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
mapio committed Nov 22, 2024
1 parent 1e46d57 commit f9affba
Show file tree
Hide file tree
Showing 28 changed files with 729 additions and 8 deletions.
85 changes: 85 additions & 0 deletions src/main/java/it/unimi/di/prog2/e15/IntRangeClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
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.e15;

/** A class to test int ranges. */
public class IntRangeClient {

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

/**
* A method to test the {@link IntRange} class.
*
* <p>This methods reads the parameters of an {@link IntRange} from the lines in the standard
* input in the form {@code command value}, where the command is:
*
* <ul>
* <li>{@code F} to set the from value of the range.
* <li>{@code T} to set the to value of the range.
* <li>{@code S} to set the step of the range.
* </ul>
*
* commands can be repeated, the last value is the one that is considered; the default values for
* from, to and step are respectively {@link Integer#MIN_VALUE}, {@link Integer#MAX_VALUE}, and 1.
* Once the input is exhausted, the method emits in a single line of the standard output:
*
* <ul>
* <li>the number of integers in the range,
* <li>the first integer in the range (if any),
* <li>the last integer in the range (if different from the first).
* </ul>
*
* @param args not used.
*/

/*- Uncomment and complete once you have implemented the range class
public static void main(String[] args) {
// declare the range...
try (Scanner sc = new Scanner(System.in)) {
while (sc.hasNext()) {
char command = sc.next().charAt(0);
int value = sc.nextInt();
switch (command) {
case 'F':
// set the from the range
break;
case 'T':
// set the to the range
break;
case 'S':
// set the step the range
break;
default:
throw new IllegalArgumentException("Unknown command: " + command);
}
}
}
int iterations = 0, first = 0, last = 0;
// complete...
System.out.println(
iterations + (iterations > 0 ? " " + first : "") + (iterations > 1 ? " " + last : ""));
}
*/
}
77 changes: 77 additions & 0 deletions src/main/java/it/unimi/di/prog2/e15/StringIterators.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
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.e15;

import java.util.Iterator;

/** Utility class with some string iterators. */
public class StringIterators {

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

/**
* Filters even-positioned strings.
*
* @param it an iterator of strings.
* @return an iterator that returns the even-positioned strings of {@code it}.
*/
public static Iterator<String> evenIterator(final Iterator<String> it) {
return new Iterator<>() {

// EXERCISE: complete the implementation

@Override
public boolean hasNext() {
throw new UnsupportedOperationException("Unimplemented method 'hasNext'");
}

@Override
public String next() {
throw new UnsupportedOperationException("Unimplemented method 'next'");
}
};
}

/**
* Converts strings to uppercase.
*
* @param it an iterator of strings.
* @return an iterator that returns the strings of {@code it} in uppercase.
*/
public static Iterator<String> uppercase(final Iterator<String> it) {
return new Iterator<>() {

// EXERCISE: complete the implementation

@Override
public boolean hasNext() {
throw new UnsupportedOperationException("Unimplemented method 'hasNext'");
}

@Override
public String next() {
throw new UnsupportedOperationException("Unimplemented method 'next'");
}
};
}
}
44 changes: 44 additions & 0 deletions src/main/java/it/unimi/di/prog2/e15/StringIteratorsClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
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.e15;

/** A class to test {@link StringIterators}. */
public class StringIteratorsClient {

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

/**
* Prints the uppercase version of the even lines of the standard input.
*
* @param args not used.
*/
/*- uncomment the following code once you have implemented the methods in StringIterators
public static void main(String[] args) {
try (Scanner s = new Scanner(System.in)) {
Iterator<String> it = StringIterators.uppercase(StringIterators.evenIterator(s));
while (it.hasNext()) System.out.println(it.next());
}
}
*/
}
5 changes: 5 additions & 0 deletions src/main/java/it/unimi/di/prog2/e15/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Esercizi relativi alla lezione 15, per maggiori dettagli si veda il <a
* href="https://prog2.di.unimi.it/diario">diario del corso</a>.
*/
package it.unimi.di.prog2.e15;
21 changes: 13 additions & 8 deletions src/main/java/it/unimi/di/prog2/h14/IntSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ public class IntSet implements Iterable<Integer> {
*
*/

private boolean repOk() {
for (int i = 0; i < els.size(); i++)
for (int j = 0; j < els.size(); j++)
if (i != j && els.get(i).equals(els.get(j))) return false;
return true;
}

/**
* Initializes this set to be empty.
*
Expand All @@ -72,7 +65,7 @@ public IntSet() {
* @param other the {@code IntSet} to copy from.
*/
public IntSet(IntSet other) {
els = new ArrayList<Integer>(other.els);
els = new ArrayList<>(other.els);
assert repOk();
}

Expand Down Expand Up @@ -177,4 +170,16 @@ public String toString() {
public Iterator<Integer> iterator() {
return new IntGenerator(els);
}

/**
* An implementation of the RI.
*
* @return whether the RI is satisfied.
*/
private boolean repOk() {
for (int i = 0; i < els.size(); i++)
for (int j = 0; j < els.size(); j++)
if (i != j && els.get(i).equals(els.get(j))) return false;
return true;
}
}
1 change: 1 addition & 0 deletions src/main/java/it/unimi/di/prog2/h14/IntSetsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

/** Classe per il test di {@link IntSet} {@link IntGenerator}. */
public class IntSetsClient {
/** . */
private IntSetsClient() {}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/it/unimi/di/prog2/h14/Primes.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
/** Iteratore che genera i numeri primi. */
public class Primes implements Iterator<Integer> {

/** Crea un generatore di numeri primi. */
public Primes() {}

/*-
* AF: il prossimo numero primo è il primo intero maggiore o uguale a
* candidate che non sia divisibile per uno dei primi già restituiti da next,
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/it/unimi/di/prog2/h15/DecimalDigits.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
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.h15;

/** A class representing decimal digits of a long. */
public class DecimalDigits {

/** The number whose digits are represented by this class. */
private final long number;

/**
* Creates a new instance representing the digits of the given number.
*
* @param number the number.
*/
public DecimalDigits(final long number) {
this.number = number;
}

/**
* Returns the digit corresponding to the given power of 10.
*
* @param power the power.
* @return the corresponding digit.
*/
public int digit(final int power) {
if (power < 0) throw new IllegalArgumentException("The power must be positive.");
long digit = number;
for (int i = 0; i < power; i++) digit /= 10;
return (int) (digit % 10);
}
}
73 changes: 73 additions & 0 deletions src/main/java/it/unimi/di/prog2/h15/DecimalDigitsAG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
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.h15;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
* A class representing decimal digits of a long, endowed with a non zero digits iterator, based on
* an <strong>anonymous</strong> class.
*/
public class DecimalDigitsAG {

/** The number whose digits are represented by this class. */
private final long number;

/**
* Creates a new instance representing the digits of the given number.
*
* @param number the number.
*/
public DecimalDigitsAG(final long number) {
this.number = number;
}

/**
* Returns a <em>generator</em> on from the least significant to the most significant non zero
* digits.
*
* @return the generator.
*/
public Iterator<Integer> nonZeroDigits() {
// no need to pass any value, nor to define any named class
return new Iterator<Integer>() {

/** The remaining digits to return (except possibly for the trailing zeroes). */
private long remaining = number;

@Override
public boolean hasNext() {
while (remaining != 0 && remaining % 10 == 0) remaining /= 10;
return remaining != 0;
}

@Override
public Integer next() {
if (!hasNext()) throw new NoSuchElementException();
int digit = (int) (remaining % 10);
remaining /= 10;
return digit;
}
};
}
}
Loading

0 comments on commit f9affba

Please sign in to comment.