-
Notifications
You must be signed in to change notification settings - Fork 30
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
28 changed files
with
729 additions
and
8 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,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 : "")); | ||
} | ||
*/ | ||
} |
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,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
44
src/main/java/it/unimi/di/prog2/e15/StringIteratorsClient.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,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()); | ||
} | ||
} | ||
*/ | ||
} |
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 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; |
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
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
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
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,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); | ||
} | ||
} |
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,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; | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.