codice:
import java.util.Random;
public class Student implements Comparable {
private static int matricolaCount=0;
private int matricola;
private float mediaVoti;
private int nroEsamiSostenuti;
private int nroLodi;
Student(float mediaVoti, int nroEsamiSostenuti, int nroLodi){
this.matricola=matricolaCount++;
this.mediaVoti=mediaVoti;
this.nroEsamiSostenuti=nroEsamiSostenuti;
this.nroLodi=nroLodi;
}
/*
* Genera un Studente casuale
*/
Student(){
this.matricola=matricolaCount++;
this.mediaVoti=(float)(new Random().nextDouble()*30.0);
this.nroEsamiSostenuti=new Random().nextInt(20)+1;
this.nroLodi=new Random().nextInt(this.nroEsamiSostenuti)+1;
}
public String toString(){
return "MATRICOLA: " + matricola + " MEDIA VOTI: "+mediaVoti + " NUMERO ESAMI SOSTENUTI: " + nroEsamiSostenuti + " NUMERO LODI: " + nroLodi;
}
float getMediaVoti(){
return mediaVoti;
}
int getNroEsamiSostenuti(){
return nroEsamiSostenuti;
}
int getNroLodi(){
return nroLodi;
}
public int compareTo(Object o) {
// Confronta usando come relazione d'ordine
// o1 < o2 iff o1.mediaVoti <= o2.mediaVoti
if(((Student)o).getNroLodi()<this.getNroLodi())
return +1;
else
if(((Student)o).getNroLodi()>this.getNroLodi())
return -1;
return 0;
}
static void bubblesort(Student[] A) {
// TODO Auto-generated method stub
for (int i = 0; i < A.length; i++) {
boolean scambiAvvenuti = false;
for (int j = 1; j < A.length -1;j++) {
if(A[j].compareTo(A[j-1]) < 0) {
Student temp = A[j-1];
A[j-1] = A[j];
A[j] = temp;
}
}
if (!scambiAvvenuti) break;
}
}
public static void main(String args[]){
Student s[]=new Student[4];
s[0]=new Student((float)28.5,10,3);
s[1]=new Student((float)24.5,12,5);
s[2]=new Student((float)26.5,15,1);
s[3]=new Student((float)22.5,14,4);
int tempI=0;
for(int i=1;i<s.length;i++)
{
if(s[i].compareTo(s[tempI])>0)
tempI=i;
}
System.out.println(s[tempI]);
Student s_10[] = new Student[10];
for (int i = 0; i < s_10.length; i++) {
s_10[i] = new Student();
}
bubblesort(s_10);
for(int i=0;i<s_10.length;i++)
{
System.out.println(s_10[i]);
}
}