Vous êtes sur la page 1sur 7

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)


{
this.nom=nom;
this.prenom=prenom;
this.rue=rue;
this.ville=ville;
nbPersonnes ++;
}

public String toString()


{
return (nom+" "+prenom+" " + rue+" "+ ville);
}

public void modifierPersonne(String rue, String ville)


{
this.rue=rue;
this.ville=ville;
ecrirePersonne ();
}

static void nbPersonne()


{
System.out.println("\n Nombre d’employés : " + nbPersonnes +
"\n Nombre de secrétaires : " +
secretaire.nbSecretaires() +
"\n Nombred’enseignants : " +
Enseignant.nbEnseignants() +
"\n Nombre d’étudiants : " +
Etudiant.nbEtudiants());
}

abstract void ecrirePersonne ();


public static void main(String[] args) {
secretaire soc = new secretaire ("Sara", "soufi","Rue de
melahi", "rabat", "gt2534");
soc.ecrirePersonne();

Enseignant en = new Enseignant ("Ahmed", "Sbihi", "Rue de al


farah", "tetouan", "1 ere annee renouvlable");
en.ecrirePersonne();

Etudiant et2 = new Etudiant ("ahmed", "koko","Rue asilal",


"tanger", "3 eme annee genie civil");
et2.ecrirePersonne();

System.out.println ("\n l'affichage apres la modification:");

soc.modifierPersonne ("Rue de checha", "fes");


en.modifierPersonne ("Rue de afilal", "casablanca");

personne.nbPersonne();

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);
this.numeroBureau = numeroBureau;
nbSecretaires++;
}

public String toString ()


{
return super.toString() + "\n Numero de bureau : " +
numeroBureau;
}

void ecrirePersonne ()
{
System.out.println ("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);
this.diplomeEnCours=diplomeEnCours;
nbEtudiants++;
}

public String toString() {

return super.toString() +"le diplome "+diplomeEnCours;


}

void ecrirePersonne ()
{
System.out.println ("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);
this.specialite = specialite;
nbEnseignants++;
}

public String toString ()


{
return super.toString() + "\n spécialité : " + specialite;
}

void ecrirePersonne ()
{
System.out.println ("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);
this.largeur=largeur;
this.hauteur=hauteur;
}
public void Rectangle(Rectangle Rec)
{
x = Rec.x;
y = Rec.y;
largeur = Rec.largeur;
hauteur = Rec.hauteur;
}

public double getHauteur(){


return hauteur;
}

public double getLargeur()


{
return largeur;
}
public void setObjetGraphique(int x, int y , double largeur, double
hauteur)
{
super(x,y);
this.largeur = largeur;
this.hauteur = hauteur;

}
public void setTaille(double largeur, double hauteur)
{
this.largeur = largeur;
this.hauteur = hauteur;
}

public void affiche()


{
System.out.println( "rectangle : "+getX()+" "+getY()+"
"+getHauteur()+" "+getLargeur());

}}

public class Bouton extends Rectangle{

String text ;

public void Bouton()


{
super.Rectangle();
text=null;
}

Bouton(int x , int y, double largeur, double hauteur, String text)


{
super(x,y,largeur,hauteur);
this.text= text;

public void Bouton(Bouton bot)


{
x=bot.x;
y=bot.y;
hauteur=bot.hauteur;
largeur=bot.largeur;
text=bot.text;

}
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);
this.rayon=rayon;
}

public Cercle(Cercle Crcl){

x=Crcl.x;
y=Crcl.y;
rayon=Crcl.rayon;
}

public void setRayon(double rayon)


{
this.rayon=rayon;
}
public double getRayon()
{
return rayon;
}

void affiche() {

System.out.println("ce cercle a du rayon " +getRayon());


}}

Partie 2 : (Interfaces)
Exercice 3 (Gestion de stock d'un magasin)

Vous aimerez peut-être aussi