Vous êtes sur la page 1sur 5

Correction du DS de maison

Classe Chercheur
public class Chercheur {

protected String nom;


protected String prenom;
protected int numOrdinateur;

Chercheur(String nom,String prenom,int numOrdinateur){


this.nom=nom;
this.prenom=prenom;
this.numOrdinateur=numOrdinateur;
}

public void affiche(){


System.out.println("nom="+this.nom);
System.out.println("prenom="+this.prenom);
System.out.println("numero
d'ordinateur="+this.numOrdinateur);
}

public String toString(){


return "nom="+this.nom+" "+"prenom="+this.prenom+"
"+"numero d'ordinateur="+this.numOrdinateur;
}

/*
// Redéfinition
public boolean equals(Object obj){
return (true);
}*/

// Surcharge
public boolean equals(Chercheur c){
if(this.nom==c.nom && this.prenom==c.prenom &&
this.numOrdinateur==c.numOrdinateur)
return true;
else
return false;
}

}
Classe Bureau
public class Bureau {
private int numBureau;
private int numEtage;
private int NbChercheurs = 0;
private int NB_MAX_CHERCHEURS = 5;
private Chercheur[] tabChercheurs;

public Bureau(int numBureau, int numEtage) {


this.numBureau = numBureau;
this.numEtage = numEtage;
tabChercheurs = new Chercheur[NB_MAX_CHERCHEURS];
}

public void affiche() {


for (int i = 0; i < NbChercheurs; i++) {
System.out.println(tabChercheurs[i]);
//tabChercheurs[i].affiche();
}
}

boolean ajouterChercheur(Chercheur chercheur) {


if (this.NbChercheurs < NB_MAX_CHERCHEURS) {
tabChercheurs[NbChercheurs] = chercheur;
NbChercheurs++;
return true;
} else
return false;
}

public int trouverChercheur(Chercheur chercheur) {


for (int i = 0; i < NbChercheurs; i++) {
if (chercheur.equals(tabChercheurs[i])) {
return i;
}
}
return -1;
}

public void supprimer (){


for (int i = 0; i < NbChercheurs; i++) {
tabChercheurs[i]=tabChercheurs[i+1];
}
NbChercheurs--;
}

public int getNbrChercheur(){


return (NbChercheurs);
}
}
Classe Laboratoire
public class Laboratoire {

private String nomLaboratoire;


private String specialite;
private int NbBureaux = 0;
private int NB_MAX_BUREAUX = 50;
private Bureau[] tabBureaux;

Laboratoire(String nomLaboratoire, String specialite) {


this.nomLaboratoire = nomLaboratoire;
this.specialite = specialite;
tabBureaux = new Bureau[NB_MAX_BUREAUX];
}

boolean ajouterBureau(Bureau bureau) {


if (this.NbBureaux < NB_MAX_BUREAUX) {
tabBureaux[NbBureaux] = bureau;
NbBureaux++;
return true;
} else
return false;
}

//Afficher le nombre total de chercheurs du laboratoire


créé.
public int nbrChercheurTotal(){
int nbrChercheurTotal=0;
for (int i=0; i<NbBureaux; i++)

nbrChercheurTotal+=tabBureaux[i].getNbrChercheur();
return nbrChercheurTotal;
}
}
Classe Principale
public class Principale {

public static void main(String[] args) {


Chercheur ch1 = new Chercheur("Hamdi","Mohamed",1);
Chercheur ch2 = new Chercheur("H","Mohamed",1);
Chercheur ch3 = new Chercheur("Hedi","Hamdi",3);

ch1.affiche();

System.out.println (ch2.toString());

System.out.println (ch3);

System.out.println(ch1.equals(ch2));
System.out.println(ch1.equals(ch1));
System.out.println(ch2.equals(ch3));

EnseignantChercheur ech =new


EnseignantChercheur("Hamdi","Mohamed",1,9);
ech.affiche();
System.out.println(ech);

System.out.println(ech.equals(ch1));

Bureau b1 = new Bureau(1,1);


Bureau b2 = new Bureau(2,2);

b1.ajouterChercheur(ch1);
b1.ajouterChercheur(ch2);
b1.ajouterChercheur(ch3);

b1.affiche();

System.out.println("TROUVE CH2");
System.out.println(b1.trouverChercheur(ch2));

//b1.supprimer();

b1.affiche();

b2.ajouterChercheur(ch3);

b2.affiche();

Laboratoire lab1 = new Laboratoire("lab1","chimie");


lab1.ajouterBureau(b1);
lab1.ajouterBureau(b2);
//Afficher le bureau ayant un nombre supérieur de
//chercheurs.
System.out.println ( b1.getNbrChercheur()>
b2.getNbrChercheur() ? "Bureau b1 a un nombre supérieur de
chercheurs" : "Bureau b2 a un nombre supérieur de chercheurs"
);

//Supprimer le premier chercheur de son bureau.


b1.supprimer();

//Afficher le nombre total de chercheurs du


//laboratoire créé.
System.out.println ("Nombre total de chercheurs du
laboratoire lab1 : "+lab1.nbrChercheurTotal());

Vous aimerez peut-être aussi