Merge branch 'master' of gitea.it:gicorada/java-scuola
This commit is contained in:
29
6.x/p6.12/Sol Prof/ExpGenerator.java
Normal file
29
6.x/p6.12/Sol Prof/ExpGenerator.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
public class ExpGenerator implements Sequence<Double>{
|
||||||
|
private double x, nuovo, old, potenza, fattoriale;
|
||||||
|
private int n;
|
||||||
|
public static final double EPSILON = 1E-6;
|
||||||
|
|
||||||
|
public ExpGenerator(double x) {
|
||||||
|
this.x = x;
|
||||||
|
nuovo = 1;
|
||||||
|
old = 0;
|
||||||
|
potenza = 1;
|
||||||
|
fattoriale = 1;
|
||||||
|
n = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasNext() {
|
||||||
|
return Math.abs(nuovo-old) >= EPSILON;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double next() {
|
||||||
|
if(!hasNext()) throw new IllegalArgumentException();
|
||||||
|
old = nuovo;
|
||||||
|
n++;
|
||||||
|
potenza *= x;
|
||||||
|
fattoriale *= n;
|
||||||
|
nuovo = old + potenza/fattoriale;
|
||||||
|
return old;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
23
6.x/p6.12/Sol Prof/ExpTester.java
Normal file
23
6.x/p6.12/Sol Prof/ExpTester.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Classe che esegue il test dell'uso della funzione ExpApprossimator
|
||||||
|
@author radaelli11353
|
||||||
|
*/
|
||||||
|
public class ExpTester {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner in = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.print("Inserisci il valore di x");
|
||||||
|
double x = in.nextDouble();
|
||||||
|
|
||||||
|
ExpGenerator gen = new ExpGenerator(x);
|
||||||
|
|
||||||
|
double sum = 1;
|
||||||
|
while(gen.hasNext()) {
|
||||||
|
System.out.println(gen.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(sum);
|
||||||
|
}
|
||||||
|
}
|
4
6.x/p6.12/Sol Prof/Sequence.java
Normal file
4
6.x/p6.12/Sol Prof/Sequence.java
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
public interface Sequence<T> {
|
||||||
|
public boolean hasNext();
|
||||||
|
public T next();
|
||||||
|
}
|
149
7.x/P7.1,2,3,4,5 Purse/Purse senza arraylist/Purse.java
Normal file
149
7.x/P7.1,2,3,4,5 Purse/Purse senza arraylist/Purse.java
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Classe che simula un borsellino
|
||||||
|
* @author radaelli11353
|
||||||
|
*/
|
||||||
|
public class Purse {
|
||||||
|
private String[] coins;
|
||||||
|
private int actualSize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Costruttore
|
||||||
|
* @param dim Quantità massima di monete inseribili nel borsellino
|
||||||
|
*/
|
||||||
|
public Purse(int dim) {
|
||||||
|
coins = new String[dim];
|
||||||
|
actualSize = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metodo che aggiunge una moneta al borsellino
|
||||||
|
* @param coinName Nome della moneta
|
||||||
|
*/
|
||||||
|
public void addCoin(String coinName) {
|
||||||
|
coins[actualSize] = coinName;
|
||||||
|
actualSize++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metodo che inverte gli elementi presenti nel borsellino
|
||||||
|
*/
|
||||||
|
public void reverse() {
|
||||||
|
String[] oldCoins = new String[actualSize];
|
||||||
|
oldCoins = Arrays.copyOf(coins, actualSize);
|
||||||
|
for(int i = 0; i < actualSize; i++) {
|
||||||
|
coins[i] = oldCoins[actualSize - i - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metodo che restituisce il numero di valori presenti nel borsellino
|
||||||
|
* @return Numero di valori presenti nel borsellino
|
||||||
|
*/
|
||||||
|
public int getSize() {
|
||||||
|
return actualSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metodo che restituisce il valore di una moneta alla posizione 'i'
|
||||||
|
* @param i Posizione del valore richiesto
|
||||||
|
* @return Valore (String) alla posizione 'i'
|
||||||
|
*/
|
||||||
|
public String getCoinValue(int i) {
|
||||||
|
if(i >= actualSize || i < 0) throw new IllegalArgumentException("La moneta all'index" + i + "non è presente nel borsellino");
|
||||||
|
return coins[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metodo che imposta la moneta ad una posizione 'i' ad un valore 'value'
|
||||||
|
* @param i Posizione in cui inserire il valore
|
||||||
|
* @param value Valore (String) da inserire nel borsellino
|
||||||
|
*/
|
||||||
|
public void setCoinValue(int i, String value) {
|
||||||
|
if(i >= actualSize || i < 0) throw new IllegalArgumentException("La moneta all'index" + i + "non è presente nel borsellino");
|
||||||
|
coins[i] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metodo che svuota il borsellino
|
||||||
|
*/
|
||||||
|
public void clear() {
|
||||||
|
for(int i = 0; i < actualSize; i++) {
|
||||||
|
coins[i] = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metodo che sposta le monete contenute in un altro borsellino
|
||||||
|
* nel borsellino attuale
|
||||||
|
* @param other Altro borsellino che viene svuotato
|
||||||
|
*/
|
||||||
|
public void transfer(Purse other) {
|
||||||
|
for(int i = 0; i < other.actualSize; i++) {
|
||||||
|
coins[actualSize + i] = other.coins[i];
|
||||||
|
}
|
||||||
|
actualSize += other.actualSize;
|
||||||
|
other.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metodo che confronta (per contenuto e posizione del contenuto) due borsellini
|
||||||
|
* @param other Altro borsellino con cui viene confrontato l'attuale
|
||||||
|
* @return True se i due borsellini contengono le stesse monete nella stessa posizione, false se non soddisfano queste condizioni
|
||||||
|
*/
|
||||||
|
public boolean sameContents(Purse other) {
|
||||||
|
if(this.actualSize == other.actualSize) {
|
||||||
|
for(int i = 0; i < actualSize; i++) {
|
||||||
|
if(coins[i] != other.coins[i]) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metodo che confronta (per contenuto) due borsellini
|
||||||
|
* @param other Altro borsellino con cui viene confrontato l'attuale
|
||||||
|
* @return True se i due borsellini contengono le stesse monete, false se non soddisfa questa condizione
|
||||||
|
*/
|
||||||
|
public boolean sameCoins(Purse other) {
|
||||||
|
if(this.actualSize == other.actualSize) {
|
||||||
|
String[] copy = new String[actualSize];
|
||||||
|
for(int i = 0; i < actualSize; i++) {
|
||||||
|
copy[i] = other.coins[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
for(String coin : this.coins) {
|
||||||
|
for(int i = 0; i < actualSize; i++) {
|
||||||
|
if(coin == other.coins[i]) {
|
||||||
|
copy[i] = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(String coin : copy) {
|
||||||
|
if(coin != "") return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metodo toString che restituisce i valori delle monente nel borsellino
|
||||||
|
* @return Testo che indica le monete contenute nel borsellino
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
String text = "Purse[";
|
||||||
|
|
||||||
|
for(int i = 0; i < actualSize; i++) {
|
||||||
|
if(i != 0) text += ",";
|
||||||
|
text += coins[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
text += "]";
|
||||||
|
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
public class PurseTester {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Purse borsellino = new Purse(50);
|
||||||
|
|
||||||
|
Purse altroBorsellino = new Purse(50);
|
||||||
|
|
||||||
|
borsellino.addCoin("Test1");
|
||||||
|
borsellino.addCoin("Test1");
|
||||||
|
borsellino.addCoin("Test1");
|
||||||
|
borsellino.addCoin("Test");
|
||||||
|
altroBorsellino.addCoin("Test");
|
||||||
|
altroBorsellino.addCoin("Test1");
|
||||||
|
altroBorsellino.addCoin("Test1");
|
||||||
|
altroBorsellino.addCoin("Test1");
|
||||||
|
|
||||||
|
System.out.println(borsellino.sameCoins(altroBorsellino));
|
||||||
|
|
||||||
|
System.out.println(borsellino.toString());
|
||||||
|
System.out.println(altroBorsellino.toString());
|
||||||
|
}
|
||||||
|
}
|
29
7.x/PN7.1 RunGenerator/RunGenerator.java
Normal file
29
7.x/PN7.1 RunGenerator/RunGenerator.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
|
||||||
|
public class RunGenerator {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] values = {1,2,2,2,3,4,4,5,5,5,6,6,7,7,7,7,7,8,9,0};
|
||||||
|
|
||||||
|
boolean inRun = false;
|
||||||
|
|
||||||
|
for(int i = 0; i < values.length; i++) {
|
||||||
|
if(inRun) {
|
||||||
|
if(values[i] != values[i-1]) {
|
||||||
|
System.out.print(")");
|
||||||
|
inRun = false;
|
||||||
|
if(i < values.length - 1 && values[i] == values[i+1]) {
|
||||||
|
System.out.print("(");
|
||||||
|
inRun = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if(i < values.length - 1 && values[i] == values[i+1]) {
|
||||||
|
System.out.print("(");
|
||||||
|
inRun = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.print(values[i]);
|
||||||
|
}
|
||||||
|
if(inRun) System.out.print(")");
|
||||||
|
}
|
||||||
|
}
|
BIN
7.x/PN7.1 RunGenerator/RunGenerator.pdf
Normal file
BIN
7.x/PN7.1 RunGenerator/RunGenerator.pdf
Normal file
Binary file not shown.
35
7.x/PN7.3 Riempimento/Riempimento.java
Normal file
35
7.x/PN7.3 Riempimento/Riempimento.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
|
||||||
|
public class Riempimento {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
boolean[] posti = {false, false, false, false, false, false, false, false, false, false};
|
||||||
|
int occupati = 0;
|
||||||
|
|
||||||
|
while (occupati < posti.length) {
|
||||||
|
int centro = 0;
|
||||||
|
int maxSequence = 0;
|
||||||
|
int actSequence = 0;
|
||||||
|
for (int i = 0; i < posti.length; i++) {
|
||||||
|
if (!posti[i]) {
|
||||||
|
actSequence++;
|
||||||
|
if (actSequence > maxSequence) {
|
||||||
|
maxSequence = actSequence;
|
||||||
|
centro = i - actSequence / 2;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
actSequence = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
posti[centro] = true;
|
||||||
|
occupati++;
|
||||||
|
|
||||||
|
for(int i = 0; i < posti.length; i++) {
|
||||||
|
if(posti[i]) System.out.print("X");
|
||||||
|
else System.out.print("_");
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
BIN
7.x/PN7.3 Riempimento/Riempimento.pdf
Normal file
BIN
7.x/PN7.3 Riempimento/Riempimento.pdf
Normal file
Binary file not shown.
12
Test somma floating point/FPSum.java
Normal file
12
Test somma floating point/FPSum.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
public class FPSum {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
double sum = 0;
|
||||||
|
|
||||||
|
for(int i = 0; i < 1000; i++) {
|
||||||
|
sum += 1/(double)1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Numero teorico: 1");
|
||||||
|
System.out.println("Numero reale: " + sum);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user