Add tema d'esame

This commit is contained in:
orange 2017-01-09 10:53:17 +01:00
parent 89479bf7fa
commit b5dce92962
5 changed files with 209 additions and 0 deletions

View File

@ -0,0 +1,66 @@
import java.util.Comparator;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
import java.util.Scanner;
import java.io.PrintStream;
public class AlberoBinarioDiRicerca{
public AlberoBinarioDiRicerca(Scanner sc, Comparator<Persona> cmp){
tree = new TreeSet<Persona>(cmp);
Persona p=Persona.read(sc);
while(p!=null){
tree.add(p);
p=Persona.read(sc);
}
}
private AlberoBinarioDiRicerca(Set<Persona> tree){
this.tree=tree;
}
public AlberoBinarioDiRicerca sort(Comparator<Persona> cmp){
Set<Persona> newTree = new TreeSet<Persona>(cmp);
for(Persona p: tree)
newTree.add(p);
return new AlberoBinarioDiRicerca(newTree);
}
public void print(PrintStream ps){
for(Persona p: tree)
p.print(ps);
}
private Set<Persona> tree;
}
class NomeComparator implements Comparator<Persona>{
public int compare(Persona p1, Persona p2){
return p1.getNome().compareTo(p2.getNome());
}
}
class CognomeComparator implements Comparator<Persona>{
public int compare(Persona p1, Persona p2){
return p1.getCognome().compareTo(p2.getCognome());
}
}
class DataComparator implements Comparator<Persona>{
public int compare(Persona p1, Persona p2){
return p1.getDataDiNascita().compareTo(p2.getDataDiNascita());
}
}
class PesoComparator implements Comparator<Persona>{
public int compare(Persona p1, Persona p2){
return ((Double) p1.getPeso()).compareTo((Double) (p2.getPeso()));
}
}
class AltezzaComparator implements Comparator<Persona>{
public int compare(Persona p1, Persona p2){
return ((Double) p1.getAltezza()).compareTo((Double) (p2.getAltezza()));
}
}

View File

@ -0,0 +1,112 @@
import java.util.Scanner;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException;
public class Persona{
public Persona(String nome, String cognome, Date dataDiNascita, double peso, double altezza){
this.nome=nome;
this.cognome=cognome;
this.dataDiNascita=dataDiNascita;
this.peso=peso;
this.altezza=altezza;
}
public String getNome(){
return nome;
}
public String getCognome(){
return cognome;
}
public Date getDataDiNascita(){
return dataDiNascita;
}
public double getPeso(){
return peso;
}
public double getAltezza(){
return altezza;
}
public void setNome(String nome){
this.nome = nome;
}
public void setDataDiNascita(Date dataDiNascita){
this.dataDiNascita = dataDiNascita;
}
public void setCognome(String cognome){
this.cognome = cognome;
}
public void setPeso(double peso){
this.peso=peso;
}
public void setAltezza(double altezza){
this.altezza=altezza;
}
public static Persona read(Scanner sc){
String nome, cognome;
Date dataDiNascita;
double peso, altezza;
SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
if(!sc.hasNextLine()) return null;
nome=sc.nextLine();
if(!sc.hasNextLine()) return null;
cognome=sc.nextLine();
if(!sc.hasNextLine()) return null;
try{
dataDiNascita=sdf.parse(sc.nextLine());
}
catch(ParseException exception){
System.err.println("Impossibile convertire la stringa in data");
System.err.println("Data impostata a epoch");
dataDiNascita=new Date(0);
}
if(!sc.hasNextLine()) return null;
try{
peso=Double.parseDouble(sc.nextLine());
}
catch(NumberFormatException exception){
System.err.println("Impossibile convertire la stringa in data");
System.err.println("Peso impostato a 0");
peso=0;
}
if(!sc.hasNextLine()) return null;
try{
altezza=Double.parseDouble(sc.nextLine());
}
catch(NumberFormatException exception){
System.err.println("Impossibile convertire la stringa in data");
System.err.println("altezza impostata a 0");
altezza=0;
}
return new Persona(nome, cognome, dataDiNascita, peso, altezza);
}
public void print(PrintStream ps){
SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
ps.println(nome);
ps.println(cognome);
ps.println(sdf.format(dataDiNascita));
ps.println(peso);
ps.println(altezza);
}
private String nome;
private String cognome;
private Date dataDiNascita;
private double peso;
private double altezza;
}

Binary file not shown.

View File

@ -0,0 +1,11 @@
import java.io.File;
import java.util.Scanner;
import java.util.Collections;
public class Test{
public static void main(String [] args)throws Exception{
File fl = new File("persone.dat");
AlberoBinarioDiRicerca tree = new AlberoBinarioDiRicerca(new Scanner(fl), new PesoComparator());
tree.sort(new NomeComparator()).print(System.out);
}
}

View File

@ -0,0 +1,20 @@
Pinco Panco
cnome
12/02/1998
65.9
177.8
Raffaele
Mignone
18/07/1996
71.5
175.5
Pluto
Disney
15/09/2009
34.6
134.7
Vincenzo
Manganiello
04/09/2005
40.7
146.9