Imaginsystems


Tecniche di Programmazione - Codici Sorgenti - News Informatiche
Archivio Posts
Anno 2014

Anno 2013

Anno 2012
Statistiche
  • Views Home Page: 71.612
  • Views Posts: 542.408
  • Views Gallerie: 0
  • n° Posts: 210
  • n° Commenti: 224

JAVA - Esercizio n.2 di Programmazione Orientata agli Oggetti (POO)

JAVA -   Esercizio n.2 di Programmazione Orientata agli Oggetti (POO) 




Esercizio per il corso di Programmazione Orientata agli Oggetti (POO) , Uni Roma Tre .
L'obbiettivo dell'esercizio implementare Comparable.

CODICE JAVA : (File : Studente.java )
import java.lang.Comparable;

public class Studente implements Comparable<Studente>{

	private String nome;
	private String cognome;
	private int matricola;
	
	public Studente(String nome,String cognome ,int matricola){
		this.nome = nome;
		this.cognome = cognome;
		this.matricola = matricola;
	}
	

	public String getNome(){
		return this.nome;
	}
	
	public String getCognome(){
		return this.cognome;
	}

	public int getMatricola(){
		return this.matricola;
	}

	public int compareTo(Studente s){
		return this.matricola - s.getMatricola();
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((cognome == null) ? 0 : cognome.hashCode());
		result = prime * result + matricola;
		result = prime * result + ((nome == null) ? 0 : nome.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Studente other = (Studente) obj;
		if (cognome == null) {
			if (other.cognome != null)
				return false;
		} else if (!cognome.equals(other.cognome))
			return false;
		if (matricola != other.matricola)
			return false;
		if (nome == null) {
			if (other.nome != null)
				return false;
		} else if (!nome.equals(other.nome))
			return false;
		return true;
	}
		
	
}
 CODICE JAVA : (File : ComparatorePerStudenteNome.java )
import java.util.*;
	public class ComparatorePerStudenteNome implements Comparator<Studente> {
		
		public int compare(Studente s1, Studente s2){
			
			if (s1 == null) return -1;
			if (s2 == null) return +1;
			
			if ((s1.getNome().length() - s2.getNome().length() ) == 0){ return 0;}
			
			if ((s1.getNome().length() - s2.getNome().length() ) < 0){ return -1;}
			
			if ((s1.getNome().length() - s2.getNome().length() ) > 0){ return +1;}
			
			return 0;
		}
		

	}

CODICE JAVA : (File : ComparatorePerStudenteCognome.java )
import java.util.*;

public class ComparatorePerStudenteCognome implements Comparator<Studente> {
			
			public int compare(Studente s1, Studente s2){
				
				if (s1 == null) return -1;
				if (s2 == null) return +1;
				
				if ((s1.getCognome().length() - s2.getCognome().length() ) == 0){ return 0;}
				
				if ((s1.getCognome().length() - s2.getCognome().length() ) < 0){ return -1;}
				
				if ((s1.getCognome().length() - s2.getCognome().length() ) > 0){ return +1;}
				
				return 0;
			}
			

		}

CODICE JAVA : (File : ComparatorePerStudenteNomeCognome.java )
import java.util.*;

public class ComparatorePerStudenteNomeCognome implements Comparator<Studente> {
	
	public int compare(Studente s1, Studente s2){
		
		if (s1 == null) return -1;
		if (s2 == null) return +1;
		
		if ((s1.getNome().length() - s2.getNome().length() ) == 0){ 
			
			//Cognome
			
			if ((s1.getCognome().length() - s2.getCognome().length() ) == 0){ return 0;}
			
			if ((s1.getCognome().length() - s2.getCognome().length() ) < 0){ return -1;}
			
			if ((s1.getCognome().length() - s2.getCognome().length() ) > 0){ return +1;}
		}
		
		if ((s1.getNome().length() - s2.getNome().length() ) < 0){ return -1;}
		
		if ((s1.getNome().length() - s2.getNome().length() ) > 0){ return +1;}
		
		return 0;
		
	}
	

}
CODICE JAVA : (File : StudenteTest.java )
import static org.junit.Assert.*;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;


public class StudenteTest {

	public List<Studente> l = new LinkedList<Studente>();
	
	public List<Studente> ArrayListOmonimi = new LinkedList<Studente>();
	
	@Before
	public void setUp() throws Exception {
		l.add(new Studente("Alessandro","Cavolo", 599));
		l.add(new Studente("Paolo","Rossi",233));
		l.add(new Studente("Valter","Michele",16));
		l.add(new Studente("Giacomo","Merso",299));
		
		
		ArrayListOmonimi.add(new Studente("Alessandro","Cavolo", 599));
		ArrayListOmonimi.add(new Studente("Alessandro","Rossi",233));
		ArrayListOmonimi.add(new Studente("Davide","De Rubeis",16));
		ArrayListOmonimi.add(new Studente("Davide","De Rubeis",299));
		
		
	}

	@Test
	public void testSort() {
		Collections.sort(l);
		
		assertEquals("Valter",l.get(0).getNome());
		assertEquals("Paolo",l.get(1).getNome());
		assertEquals("Giacomo",l.get(2).getNome());
		assertEquals("Alessandro",l.get(3).getNome());
	
		
	}

	
	@Test
	public void testSortNome(){
		ComparatorePerStudenteNome comparatore = new ComparatorePerStudenteNome();

		Collections.sort(l,comparatore);

		assertEquals("Paolo",l.get(0).getNome());
		assertEquals("Valter",l.get(1).getNome());
		assertEquals("Giacomo",l.get(2).getNome());
		assertEquals("Alessandro",l.get(3).getNome());
		
		assertEquals(0,comparatore.compare(l.get(0),l.get(0)));
		assertEquals(-1,comparatore.compare(l.get(0),l.get(1)));
		assertEquals(1,comparatore.compare(l.get(1),l.get(0)));
		
	
	}
	
	@Test
	public void testSortCognome(){
		ComparatorePerStudenteCognome comparatore = new ComparatorePerStudenteCognome();

		Collections.sort(l,comparatore);

		assertEquals("Rossi",l.get(0).getCognome());
		assertEquals("Merso",l.get(1).getCognome());
		assertEquals("Cavolo",l.get(2).getCognome());
		assertEquals("Michele",l.get(3).getCognome());
		
		assertEquals(0,comparatore.compare(l.get(0),l.get(0)));
		assertEquals(-1,comparatore.compare(l.get(0),l.get(2)));
		assertEquals(1,comparatore.compare(l.get(3),l.get(0)));
		
	
	}
	
		@Test
		public void testSortNomeCognome_TestListSemplice(){
			ComparatorePerStudenteNomeCognome comparatore = new ComparatorePerStudenteNomeCognome();

			Collections.sort(l,comparatore);
			
			assertEquals("Paolo",l.get(0).getNome());
			assertEquals("Valter",l.get(1).getNome());
			assertEquals("Giacomo",l.get(2).getNome());
			assertEquals("Alessandro",l.get(3).getNome());
			
			assertEquals(0,comparatore.compare(l.get(0),l.get(0)));
			assertEquals(-1,comparatore.compare(l.get(0),l.get(1)));
			assertEquals(1,comparatore.compare(l.get(1),l.get(0)));
			
		
		}
		
		//ArrayListOmonimi
       
		@Test
		public void testSortNomeCognome_TestOmonimi(){
			ComparatorePerStudenteNomeCognome comparatore = new ComparatorePerStudenteNomeCognome();

			Collections.sort(ArrayListOmonimi,comparatore);
			
			assertEquals("Davide",ArrayListOmonimi.get(0).getNome());
			assertEquals("Davide",ArrayListOmonimi.get(1).getNome());
			assertEquals("Alessandro",ArrayListOmonimi.get(2).getNome());
			assertEquals("Alessandro",ArrayListOmonimi.get(3).getNome());
			
			assertEquals(0,comparatore.compare(ArrayListOmonimi.get(0),ArrayListOmonimi.get(0)));
			assertEquals(-1,comparatore.compare(ArrayListOmonimi.get(0),ArrayListOmonimi.get(2)));
			assertEquals(1,comparatore.compare(ArrayListOmonimi.get(2),ArrayListOmonimi.get(0)));
			
		
		}

}
By ImaginSystems & Queen Gin
Categoria: JAVA
mercoledì, 08 mag 2013 Ore. 16.24

Messaggi collegati


Ora e Data
Mappa
Blogs Amici
    Copyright © 2002-2007 - Blogs 2.0
    dotNetHell.it | Home Page Blogs
    ASP.NET 2.0 Windows 2003