Merge branch 'master' of gitea.it:gicorada/java-scuola

This commit is contained in:
Giacomo R. 2023-01-19 21:31:06 +01:00
commit 22ac8bce33
5 changed files with 66 additions and 0 deletions

24
InsOrd/Studente.java Normal file
View File

@ -0,0 +1,24 @@
public class Studente implements Comparable<Studente>{
private String nome;
private String cognome;
private double media;
private int eta;
public Studente(String nome, String cognome, double media, int eta) {
this.nome = nome;
this.cognome = cognome;
this.media = media;
this.eta = eta;
}
@Override
public int compareTo(Studente o) {
if(this.cognome.equals(o.cognome)) return this.nome.compareTo(o.nome);
return this.cognome.compareTo(o.cognome);
}
@Override
public String toString() {
return nome + " " + cognome + " media=" + media + " eta=" + eta;
}
}

BIN
InsOrd/Studente.pdf Normal file

Binary file not shown.

36
InsOrd/Test.java Normal file
View File

@ -0,0 +1,36 @@
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<Studente> lista = new ArrayList<>();
while(in.hasNextLine()) {
Studente s = new Studente(in.next(), in.next(), in.nextDouble(), in.nextInt());
insert(lista, s);
}
for (Studente s: lista) {
System.out.println(s.toString());
}
}
private static void insert(ArrayList<Studente> l, Studente s) {
if(l.size() == 0) {
l.add(s);
} else {
boolean ins = false;
for(int i = 0; i < l.size(); i++) {
if(s.compareTo(l.get(i)) < 0) {
l.add(i, s);
ins = true;
break;
}
}
if(!ins) {
l.add(s);
}
}
}
}

BIN
InsOrd/Test.pdf Normal file

Binary file not shown.

6
InsOrd/input.txt Normal file
View File

@ -0,0 +1,6 @@
AAA BBB 1 4
BBB AAA 2 2
CCC DDD 3 6
ABC AAA 4 1
BZZ AAA 5 3
BBB CCC 6 5