Skip to content

Commit c729bea

Browse files
committed
Uploaded examples and exercises in Module 2
1 parent 705f981 commit c729bea

File tree

49 files changed

+956
-10
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+956
-10
lines changed

module_01/src/esercizi/area_perimetro_rettangolo/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Area e Perimetro 🛴
1+
# Area e Perimetro 🛴
22

33
Scrivere un programma Java che acquisisca da tastiera i seguenti dati:
44

module_01/src/esercizi/dati_anagrafici/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Dati Anagrafici 🛴
1+
# Dati Anagrafici 🛴
22

33
Scrivere un programma Java che permetta all’utente di inserire da tastiera il proprio nome e cognome.
44

module_01/src/esercizi/incrementi/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Incrementi 🛴
1+
# Incrementi 🛴
22

33
Scrivere un programma Java che:
44

module_01/src/esercizi/inverti_stringhe/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Inverti Stringhe 🛵
1+
# Inverti Stringhe 🛵
22

33
Scrivere un programma Java che, data una stringa, stampi la stessa stringa invertendone il contenuto a partire dal primo spazio, ad esempio data la stringa _"come stai"_ stampi a video _"stai come"_.
44

module_01/src/esercizi/numero_opposto/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Numero Opposto 🛴
1+
# Numero Opposto 🛴
22

33
Scrivere un programma Java in grado di prendere in ingresso da tastiera un intero _x_ e stampare a video il valore _–x_.
44

module_01/src/esercizi/operatori_aritmetici/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Operatori Aritmetici 🛴
1+
# Operatori Aritmetici 🛴
22

33
Scrivere un programma Java che permetta all’utente di inserire due numeri interi e successivamente stampi a video il risultato delle operazioni di:
44

module_01/src/esercizi/rimpiazza_intrusi/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Rimpiazza gli Intrusi 🛵
1+
# Rimpiazza gli Intrusi 🛵
22

33
Date tre stringhe _a_, _b_ e _c_, stampare _c_ dopo aver rimpiazzato, al suo interno, ogni occorrenza compresa tra due spazi di _a_ con _b_. Ad esempio, date in input le stringhe "_a_", "_b_", "_a abc d_", stampare a video "_a bbc d_".
44

module_01/src/esercizi/scambio_valori/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Scambio di Valori 🛴
1+
# Scambio di Valori 🛴
22

33
Scrivere un programma Java che acquisisca da tastiera due valori interi e li memorizzi nelle variabili _varA_ e _varB_.
44

module_01/src/esercizi/squadra_del_cuore/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Squadra del Cuore 🛴
1+
# Squadra del Cuore 🛴
22

33
Scrivere un programma Java che permetta all’utente di inserire da tastiera i seguenti dati:
44

module_01/src/esercizi/tipi_di_dato/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Tipi di Dato 🛴
1+
# Tipi di Dato 🛴
22

33
Scrivere un programma Java che:
44

module_02/src/esempi/Lezione4.java

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
package esempi;
2+
3+
import java.util.Arrays;
4+
5+
public class Lezione4 {
6+
public static void main(String[] args) {
7+
// Costrutto if-else
8+
int a = 4;
9+
int b = 3;
10+
int max;
11+
12+
if (a > b) {
13+
max = a;
14+
} else {
15+
max = b;
16+
}
17+
System.out.println("Il massimo tra " + a + " e " + b + " è: "+ max);
18+
19+
// Costrutto if-else senza parentesi graffe
20+
if (a > b)
21+
max = a;
22+
else
23+
max = b;
24+
System.out.println("Il massimo tra " + a + " e " + b + " è: "+ max);
25+
26+
// Operatore ternario
27+
max = (a > b) ? a : b;
28+
System.out.println("Il massimo tra " + a + " e " + b + " è: "+ max);
29+
30+
// Costrutto if-elseif-else
31+
int c = 10;
32+
if (c > 8) {
33+
System.out.println("c > 8");
34+
} else if (c > 5) {
35+
System.out.println("c > 5");
36+
} else {
37+
System.out.println("c <= 5");
38+
}
39+
40+
// Costrutto switch
41+
int d = 3;
42+
switch (d) {
43+
case 1:
44+
System.out.println("Risultato switch: d = 1");
45+
break;
46+
case 2:
47+
System.out.println("Risultato switch: d = 2");
48+
break;
49+
case 3:
50+
System.out.println("Risultato switch: d = 3");
51+
break;
52+
case 4:
53+
System.out.println("Risultato switch: d = 4");
54+
break;
55+
case 5:
56+
System.out.println("Risultato switch: d = 5");
57+
break;
58+
case 6:
59+
case 7:
60+
case 8:
61+
System.out.println("Risultato switch: d compreso tra 6 e 8");
62+
break;
63+
default:
64+
System.out.println("Risultato switch: d < 1 oppure d > 8");
65+
break;
66+
}
67+
68+
// Ciclo while(condizione)
69+
System.out.println("Esecuzione ciclo while:");
70+
int f1 = 5;
71+
while (f1 > 0) {
72+
System.out.println(f1);
73+
f1--;
74+
}
75+
76+
// Ciclo for
77+
System.out.println("Esecuzione ciclo for:");
78+
for (int i = 0; i < 10; i++) {
79+
System.out.println("Valore di i corrente: " + i);
80+
}
81+
82+
// Piccola parentesi: operatori aritmetici abbreviati
83+
int j = 4;
84+
j += 2; // Equivalente a j = j + 2;
85+
j -= 2; // Equivalente a j = j - 2;
86+
j *= 2; // Equivalente a j = j * 2;
87+
j /= 2; // Equivalente a j = j / 2;
88+
89+
// Ciclo do-while
90+
System.out.println("Esecuzione ciclo do-while:");
91+
int e = 0;
92+
do {
93+
System.out.println("Valore di e: " + e);
94+
e += 3;
95+
} while (e % 2 == 0);
96+
97+
// continue e break
98+
System.out.println("Esecuzione ciclo for con continue e break:");
99+
for (int i = 0; i < 30; i++) {
100+
if (i < 10) {
101+
continue;
102+
}
103+
if (i > 25) {
104+
break;
105+
}
106+
System.out.println("Valore corrente di i: " + i);
107+
}
108+
109+
// Array
110+
System.out.println("ARRAY");
111+
112+
int[] arrayInteri = new int[5];
113+
arrayInteri[0] = 1;
114+
arrayInteri[1] = 2;
115+
arrayInteri[2] = 3;
116+
arrayInteri[3] = 4;
117+
arrayInteri[4] = 5;
118+
119+
System.out.println("Elemento 0 in array: " + arrayInteri[0]);
120+
System.out.println("Elemento 1 in array: " + arrayInteri[1]);
121+
System.out.println("Elemento 2 in array: " + arrayInteri[2]);
122+
System.out.println("Elemento 3 in array: " + arrayInteri[3]);
123+
System.out.println("Elemento 4 in array: " + arrayInteri[4]);
124+
System.out.println("Lunghezza array: " + arrayInteri.length);
125+
126+
int arrayInteriForLength = 5;
127+
int[] arrayInteriFor = new int[arrayInteriForLength];
128+
129+
// Assegnazione valori all'interno di arrayInteriFor
130+
for (int i = 0; i < arrayInteriForLength; i++) {
131+
arrayInteriFor[i] = i + 1;
132+
}
133+
134+
// Stampa valori all'interno di arrayInteriFor
135+
for (int i = 0; i < arrayInteriForLength; i++) {
136+
System.out.println("Elemento " + i + " in array: " + arrayInteriFor[i]);
137+
}
138+
139+
System.out.println(Arrays.toString(arrayInteriFor));
140+
141+
// Stampa array senza Arrays.toString()
142+
System.out.print("[");
143+
for (int i = 0; i < arrayInteriForLength; i++) {
144+
System.out.print(arrayInteriFor[i]);
145+
if (i != arrayInteriForLength - 1) {
146+
System.out.print(", ");
147+
}
148+
}
149+
System.out.println("]");
150+
151+
// Sintassi alternative per inizializzazione array
152+
int[] arrayInteri2 = new int[]{ 1, 2, 3, 4, 5 };
153+
int[] arrayInteri3 = { 1, 2, 3, 4, 5 };
154+
155+
// Estensione di un array ad array di dimensione maggiore
156+
int[] arrayInteriStart = { 1, 2, 3, 4, 5 };
157+
int[] arrayInteriEnd = new int[10];
158+
System.out.println(Arrays.toString(arrayInteriEnd));
159+
160+
for (int i = 0; i < arrayInteriStart.length; i++) {
161+
arrayInteriEnd[i] = arrayInteriStart[i];
162+
}
163+
System.out.println(Arrays.toString(arrayInteriEnd));
164+
165+
// Array bidimensionali (Matrici)
166+
System.out.println("MATRICE");
167+
int[][] matrice = new int[5][10];
168+
matrice[0][0] = 3;
169+
matrice[0][1] = 4;
170+
System.out.println(Arrays.toString(matrice[0]));
171+
System.out.println(Arrays.toString(matrice[1]));
172+
System.out.println(Arrays.toString(matrice[2]));
173+
System.out.println(Arrays.toString(matrice[3]));
174+
System.out.println(Arrays.toString(matrice[4]));
175+
System.out.println();
176+
177+
for (int i = 0; i < matrice.length; i++) {
178+
for (int z = 0; z < matrice[0].length; z++) {
179+
matrice[i][z] = 5;
180+
}
181+
}
182+
printMatrice(matrice);
183+
}
184+
185+
public static void printMatrice(int[][] matriceDaStampare) {
186+
for (int i = 0; i < matriceDaStampare.length; i++) {
187+
System.out.println(Arrays.toString(matriceDaStampare[i]));
188+
}
189+
}
190+
}

module_02/src/esempi/Lezione5.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package esempi;
2+
3+
import java.util.Random;
4+
5+
public class Lezione5 {
6+
public static void main(String[] args) {
7+
// Metodo ricorsivo
8+
System.out.println("---- METODI RICORSIVI ----");
9+
int n = 5;
10+
System.out.println("Chiamata ricorsiva: fattorialeRicorsivo(" + n + ")");
11+
int nFattoriale = fattorialeRicorsivo(n);
12+
System.out.println("Fattoriale di " + n + ": " + nFattoriale);
13+
14+
// Valori randomici
15+
// Math.random()
16+
System.out.println("---- VALORI RANDOMICI ----");
17+
System.out.println("---- MATH.RANDOM() ----");
18+
double doubleRandom = Math.random();
19+
boolean boolRandom = (doubleRandom > 0.5);
20+
System.out.println("Numero double randomico: " + doubleRandom);
21+
doubleRandom *= 50;
22+
System.out.println("Numero double randomico: " + doubleRandom);
23+
int intRandom = (int) doubleRandom;
24+
System.out.println("Numero int randomico: " + intRandom);
25+
System.out.println("Valore booleano randomico: " + boolRandom);
26+
27+
// Classe Random
28+
System.out.println("---- CLASSE RANDOM ----");
29+
Random rand = new Random();
30+
double doubleRandomNew = rand.nextDouble();
31+
int intRandomNew = rand.nextInt(50);
32+
boolean boolRandomNew = rand.nextBoolean();
33+
System.out.println("Numero double randomico: " + doubleRandomNew);
34+
System.out.println("Numero int randomico: " + intRandomNew);
35+
System.out.println("Valore booleano randomico: " + boolRandomNew);
36+
37+
// Valori randomici in intervallo [min, max]
38+
int min = 34;
39+
int max = 41;
40+
int randomValueInRange = min + rand.nextInt(max - min + 1);
41+
System.out.println("Temperatura corporea randomica: " + randomValueInRange);
42+
43+
// Valori randomici con distribuzione gaussiana
44+
int gaussianRandomInt = (int) (37 + rand.nextGaussian());
45+
System.out.println("Temperatura corporea con distribuzione Gaussiana: " + gaussianRandomInt);
46+
47+
// Seed di Random
48+
Random randBySeed = new Random(42);
49+
int randomValue1 = randBySeed.nextInt();
50+
int randomValue2 = randBySeed.nextInt();
51+
System.out.println("Valore randomico 1 da Random partendo da seed 42: " + randomValue1);
52+
System.out.println("Valore randomico 2 da Random partendo da seed 42: " + randomValue2);
53+
}
54+
55+
public static int fattorialeRicorsivo(int n) {
56+
// Caso base
57+
if (n == 0) {
58+
System.out.println("Caso base: return 1");
59+
return 1;
60+
}
61+
// Chiamata ricorsiva
62+
System.out.println("Chiamata ricorsiva: fattorialeRicorsivo(" + (n - 1) + ")");
63+
return n * fattorialeRicorsivo(n - 1);
64+
}
65+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package esercizi.anagrams;
2+
3+
import java.util.Scanner;
4+
5+
public class Anagrams {
6+
public static void main(String[] args) {
7+
Scanner myScanner = new Scanner(System.in);
8+
9+
System.out.println("Inserire la prima stringa:");
10+
String a = myScanner.nextLine();
11+
String aLowerCase = a.toLowerCase();
12+
System.out.println("Inserire la seconda stringa:");
13+
String b = myScanner.nextLine();
14+
String bLowerCase = b.toLowerCase();
15+
16+
boolean outputAnagrams = areAnagrams(aLowerCase, bLowerCase);
17+
if (outputAnagrams) {
18+
System.out.println("Le due parole sono anagrammi!");
19+
} else {
20+
System.out.println("Le due parole NON sono anagrammi!");
21+
}
22+
}
23+
24+
public static boolean areAnagrams(String a, String b) {
25+
if (a.length() != b.length()) {
26+
return false;
27+
}
28+
int lettersFound = 0;
29+
for (int i = 0; i < a.length(); i++) {
30+
if (b.indexOf(a.charAt(i)) != -1) {
31+
lettersFound++;
32+
}
33+
}
34+
return lettersFound == a.length();
35+
}
36+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Anagrammi 🛵
2+
3+
Due stringhe _a_ e _b_ sono definite anagrammi se contengono gli stessi caratteri anche se in diverse posizioni. Ad esempio, gli anagrammi di CAT sono CAT, ACT, TAC, TCA, ATC e CTA.
4+
5+
Scrivere un programma che prevede un metodo che, date due stringhe _a_ e _b_ in input, restituisce true se sono anagrammi (case-insensitive) e false altrimenti.
6+
7+

0 commit comments

Comments
 (0)