Cours Java
TP2
Correction

Alain Plantec

1 Gestion des notes d'un étudiant avec un ArrayList


import java.util.*;
import Promotion;

public class Etudiant {
    private String nom, prenom;
    private int age;
    private ArrayList notes;
    private Promotion promotion;
    
    public Etudiant(String _nom, String _prenom, int _age) {
	nom = _nom;
	prenom = _prenom;
	age = _age;
	notes = new ArrayList();
    }
    public String getNom() {
	return nom;
    }
    public String getPrenom() {
	return prenom;
    }
    public int getAge() {
	return age;
    }
    public Promotion getPromotion() {
	return (promotion);
    }
    public void setPromotion(Promotion p) {
	promotion = p;
    }
    public boolean equals(Object o) {
	if (!(o instanceof Etudiant)) {
	    return false;
	} else {
	    Etudiant p = (Etudiant) o;
	    return nom.equals(p.nom) && prenom.equals(p.prenom);
	}
    }
    public int [] getNotes() {
	int [] n = new int [notes.size()];
	for (int i = 0; i < notes.size(); i++) {
	    Integer uneNote = (Integer) notes.get(i);
	    n[i] = uneNote.intValue();
	}
	return n;
    }
    public float moyenne() {
	int [] n = getNotes();
	float cum = (float) 0.0;
	for (int i = 0; i < n.length; i++) {
	    cum += n[i];
	}
	return  cum / (float) n.length;
    }
    public void setAge(int _age) {
	age = _age;
    }
    public void setNom(String _nom) {
	nom = _nom;
    }
    public void setPrenom(String _prenom) {
	prenom = _prenom;
    }
    public void ajouterNote(int _note) {
	notes.add(new Integer(_note));
    }
    public String toString() {
	StringBuffer sb = new StringBuffer();
	sb.append("Etudiant (" + nom + ", " + prenom + ", " + age + ", notes[");
	for (int i = 0; i < notes.size(); i++) {
	    sb.append(notes.get(i));
	    if (i < notes.size() - 1) {
		sb.append(",");
	    }
	}
	sb.append("])");
	return sb.toString();
    }
}

2 La classe Promotion (avec un HashMap)


import java.util.*;
import Etudiant;

public class Promotion {
    private String nom;
    private HashMap etudiants;
    
    public Promotion(String _nom) {
	nom = _nom;
	etudiants = new HashMap();
    }
    public String getNom() {
	return nom;
    }
    public void ajouterEtudiant(Etudiant e) {
	if (!etudiants.containsKey(e.getNom())) {
	    etudiants.put(e.getNom(), e);
	    e.setPromotion(this);
	}
    }
    public void retirerEtudiant(Etudiant e) {
	etudiants.remove(e.getNom());
	e.setPromotion(null);
    }
    public boolean equals(Object o) {
	if (!(o instanceof Promotion)) {
	    return false;
	} else {
	    Promotion p = (Promotion) o;
	    return nom.equals(p.nom);
	}
    }

    public String toString() {
	StringBuffer sb = new StringBuffer();
	sb.append("Promotion (" + nom + ", etudiants[");
	Set keys = etudiants.keySet();
	for (Iterator i = keys.iterator(); i.hasNext();) {
	    sb.append(etudiants.get(i.next()));
	    if (i.hasNext()) sb.append(" ");
	}
	sb.append("]: moyenne=" + moyenne() + ")");
	return sb.toString();
    }

    public Etudiant chercherEtudiant(Etudiant etudiant) {
	return (Etudiant) etudiants.get(etudiant.getNom());
    }

    public float moyenne() {
	float cum = (float) 0.0;
	Set keys = etudiants.keySet();
	for (Iterator i = keys.iterator(); i.hasNext();) {
	    Etudiant e = (Etudiant) etudiants.get(i.next());
	    cum += e.moyenne();
	}
	return  cum / (float) etudiants.size();
    }
}

3 La classe Scolarite (avec un HashMap)


import java.util.*;
import Promotion;

public class Scolarite {
    private String nom;
    private int annee;
    private HashMap promotions;
    
    public Scolarite(String _nom, int an) {
	nom = _nom;
	promotions = new HashMap();
	annee = an;
    }
    public String getNom() {
	return nom;
    }
    public Map getPromotions() {
	return promotions;
    }
    public void ajouterPromotion(Promotion e) {
	promotions.put(e.getNom(), e);
    }
    public void retirerPromotion(Promotion e) {
	promotions.remove(e.getNom());
    }
    public boolean equals(Object o) {
	if (!(o instanceof Scolarite)) {
	    return false;
	} else {
	    Scolarite p = (Scolarite) o;
	    return nom.equals(p.nom);
	}
    }
    public String toString() {
	StringBuffer sb = new StringBuffer();
	sb.append("Scolarite (" + nom + ", " + annee + ",promotions[");
	Set keys = promotions.keySet();
	for (Iterator i = keys.iterator(); i.hasNext();) {
	    sb.append(promotions.get(i.next()));
	    if (i.hasNext()) sb.append(" ");
	}
	sb.append("])");
	return sb.toString();
    }

    public Etudiant chercherEtudiant(String nom) {
	Etudiant bufe = new Etudiant(nom, "", 0);
	Set keys = promotions.keySet();
	for (Iterator i = keys.iterator(); i.hasNext();) {
	    Promotion p = (Promotion) promotions.get(i.next());
	    Etudiant found = p.chercherEtudiant(bufe);
	    if (found != null) {
		return found;
	    }
	}
	return null;
    }
    public Promotion chercherPromotionEtudiant(Etudiant etudiant) {
	return (Promotion) etudiant.getPromotion();
    }
}





Plantec Alain
2002-02-21