Studente ArrayList Comparable (finale)

Aggiute versioni definitive Studente e Test per ArrayList Comparable +
pdf
This commit is contained in:
Giacomo R. 2023-01-19 21:30:29 +01:00
parent e9eb9fa3f4
commit 4a060e119c
6 changed files with 66 additions and 25 deletions

View File

@ -1,25 +0,0 @@
import java.util.Scanner;
import java.util.Random;
public class E67 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random generator = new Random();
int i, j;
System.out.print("Inserisci una parola: ");
String word = in.next();
for(int k = 0; k < word.length(); k++) {
String first, middle, last;
i = generator.nextInt(word.length());
j = generator.nextInt(word.length() - i) + i;
first = word.substring(0, i);
middle = word.substring(i, j);
last = word.substring(j, word.length());
word = first + word.chatAt(j) + middle + word.charAt(i) + last;
}
System.out.println(first+middle+last);
}
}

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