TD/TP5-6&7: Classes Abstraites et Interfaces
Partie 1 : (Classes Abstraites) :
Exercice 1 :
package abstractpersonne;
public abstract class personne {
protected String nom;
protected String prenom;
protected String rue;
protected String ville;
protected static int nbPersonnes=0;
public personne(String nom, String prenom, String rue, String ville)
{
[Link]=nom;
[Link]=prenom;
[Link]=rue;
[Link]=ville;
nbPersonnes ++;
}
public String toString()
{
return (nom+" "+prenom+" " + rue+" "+ ville);
}
public void modifierPersonne(String rue, String ville)
{
[Link]=rue;
[Link]=ville;
ecrirePersonne ();
}
static void nbPersonne()
{
[Link]("\n Nombre d’employés : " + nbPersonnes +
"\n Nombre de secrétaires : " +
[Link]() +
"\n Nombred’enseignants : " +
[Link]() +
"\n Nombre d’étudiants : " +
[Link]());
}
abstract void ecrirePersonne ();
public static void main(String[] args) {
secretaire soc = new secretaire ("Sara", "soufi","Rue de
melahi", "rabat", "gt2534");
[Link]();
Enseignant en = new Enseignant ("Ahmed", "Sbihi", "Rue de al
farah", "tetouan", "1 ere annee renouvlable");
[Link]();
Etudiant et2 = new Etudiant ("ahmed", "koko","Rue asilal",
"tanger", "3 eme annee genie civil");
[Link]();
[Link] ("\n l'affichage apres la modification:");
[Link] ("Rue de checha", "fes");
[Link] ("Rue de afilal", "casablanca");
[Link]();
ackage abstractpersonne;
public class secretaire extends personne{
private String numeroBureau;
private static int nbSecretaires = 0;
secretaire (String nom, String prenom, String rue, String ville,
String numeroBureau)
{
super (nom,prenom,rue,ville);
[Link] = numeroBureau;
nbSecretaires++;
}
public String toString ()
{
return [Link]() + "\n Numero de bureau : " +
numeroBureau;
}
void ecrirePersonne ()
{
[Link] ("Secrétaire : " + toString());
}
static int nbSecretaires ()
{
return nbSecretaires;
}
}
package abstractpersonne;
public class Etudiant extends personne {
String diplomeEnCours;
static int nbEtudiants;
public Etudiant(String nom, String prenom, String rue, String ville,
String diplomeEnCours)
{
super (nom,prenom,rue,ville);
[Link]=diplomeEnCours;
nbEtudiants++;
}
public String toString() {
return [Link]() +"le diplome "+diplomeEnCours;
}
void ecrirePersonne ()
{
[Link] ("Etudiant : " + toString());
}
static int nbEtudiants ()
{
return nbEtudiants;
}
}
package abstractpersonne;
public class Enseignant extends personne{
private String specialite;
private static int nbEnseignants = 0;
public Enseignant (String nom, String prenom, String rue, String ville,
String specialite)
{
super (nom, prenom, rue, ville);
[Link] = specialite;
nbEnseignants++;
}
public String toString ()
{
return [Link]() + "\n spécialité : " + specialite;
}
void ecrirePersonne ()
{
[Link] ("Enseignant : " + toString());
}
static int nbEnseignants ()
{
return nbEnseignants;
}}
Exercice2 :
abstract class ObjetGraphique {
protected int x, y;
void ObjetGraphique()
{
x=0;y=0;
}
abstract void affiche ();
public ObjetGraphique(int x, int y)
{
this.x=x;
this.y=y;
}
public ObjetGraphique(ObjetGraphique Obj)
{
x =Obj.x;
y =Obj.y;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public void setPosition(int x, int y) {
this.x=x;
this.y=y;
}
public void deplacer(int dx, int dy)
{
this.x=x+dx ; this.y=y+dy;
}}
public abstract class Rectangle extends ObjetGraphique{
double largeur, hauteur;
public void Rectangle()
{
largeur=0;
hauteur=0;
}
public Rectangle (int x, int y, double largeur, double hauteur)
{
super(x,y);
[Link]=largeur;
[Link]=hauteur;
}
public void Rectangle(Rectangle Rec)
{
x = Rec.x;
y = Rec.y;
largeur = [Link];
hauteur = [Link];
}
public double getHauteur(){
return hauteur;
}
public double getLargeur()
{
return largeur;
}
public void setObjetGraphique(int x, int y , double largeur, double
hauteur)
{
super(x,y);
[Link] = largeur;
[Link] = hauteur;
}
public void setTaille(double largeur, double hauteur)
{
[Link] = largeur;
[Link] = hauteur;
}
public void affiche()
{
[Link]( "rectangle : "+getX()+" "+getY()+"
"+getHauteur()+" "+getLargeur());
}}
public class Bouton extends Rectangle{
String text ;
public void Bouton()
{
[Link]();
text=null;
}
Bouton(int x , int y, double largeur, double hauteur, String text)
{
super(x,y,largeur,hauteur);
[Link]= text;
public void Bouton(Bouton bot)
{
x=bot.x;
y=bot.y;
hauteur=[Link];
largeur=[Link];
text=[Link];
}
public void setText(String text )
{
this .text=text;
}
String getText()
{
return text;
}}
public class Cercle extends ObjetGraphique {
double rayon ;
public void Cercle()
{
rayon=0;
}
public Cercle(int x, int y, double rayon)
{
super(x,y);
[Link]=rayon;
}
public Cercle(Cercle Crcl){
x=Crcl.x;
y=Crcl.y;
rayon=[Link];
}
public void setRayon(double rayon)
{
[Link]=rayon;
}
public double getRayon()
{
return rayon;
}
void affiche() {
[Link]("ce cercle a du rayon " +getRayon());
}}
Partie 2 : (Interfaces)
Exercice 3 (Gestion de stock d'un magasin)