Vous êtes sur la page 1sur 10

Université de Sousse Programmation Orientée Objet JAVA

ISITCOM 2020-2021 Raoudha Ben Djemaa

Sujet TD2

Exercice 1 :

public class Date


{
//1.création des attributs
private int j; // jour
private int m; // mois
private int a; // an

//2. Création des constructeurs


Date()
{
j = 1;
m = 1;
a = 2004;
}

Date(int j, int m, int a)


{
this.j = j;
this.m = m;
this.a = a;
}
//3. Création des méthodes métiers

void initialise(int jour, int mois, int an)


{
j = jour;
m = mois;
a = an;
}

void afficher()
{
System.out.println(j + "/" + m + "/" + a);
}

String getDate()
{
1/30
Université de Sousse Programmation Orientée Objet JAVA
ISITCOM 2020-2021 Raoudha Ben Djemaa

return (j + "/" + m + "/" + a);


}

public static void main(String args[])


{
//creation des objets par les constructeurs
Date d1 = new Date();
Date d2 = new Date(1, 2, 2004);

//Invoquer les methods par les objets


d1.afficher();
d2.afficher();
d1.initialise(1, 2, 2004);
d1.afficher();
}
}//fin de la classe Date

Résultat d’affichage :

1/1/2004
1/2/2004
1/2/2004

Etape 2 :

class Date
{

private int j; // jour


private int m; // mois
private int a; // an
Date()
{
j = 1;
m = 1;
a = 2004;
}
Date(int j, int m, int a)
{
this.j = j;
this.m = m;
this.a = a;
}
2/30
Université de Sousse Programmation Orientée Objet JAVA
ISITCOM 2020-2021 Raoudha Ben Djemaa

void initialise(int jour, int mois, int an)


{
j = jour;
m = mois;
a = an;
}
void afficher()
{
System.out.println(j + "/" + m + "/" + a);
}
String getDate()
{
return (j + "/" + m + "/" + a);
}
}

public class Personne


{

private String nom, prenom, adresse, telephone;


private Date naissance;

public Personne(
String n,
String p,
String adr,
String tel,
int j, Date d
int m,
int a)
{
nom = n;
prenom = p;
adresse = adr;
telephone = tel;
naissance = new Date(j, m, a);
naissance = d ;
}

3/30
Université de Sousse Programmation Orientée Objet JAVA
ISITCOM 2020-2021 Raoudha Ben Djemaa

public void afficher()


{
System.out.println(
"Nom :"+ nom+ " \n"
+"Prénom :"+ prenom+ " \n"
+ "habite :"
+ adresse + " \n"
+ "date de naissance :"
+ naissance.getDate()
+ " \n"
+ "Tel:"+telephone+" \n");

Naissance.afficher();
}

public String getNom()


{
return nom;
}
public String getPrenom()
{
return prenom;
}
public String getAdresse()
{
return adresse;
}
public String getTelephone()
{
return telephone;
}
public Date getNaissance()
{
return naissance;
}

public static void main(String[] args)


{
Personne auteur1 =
new Personne("CELERE", "Jacques", "Lyon", "04032945", 1, 2, 1970);

Personne auteur2 =
4/30
Université de Sousse Programmation Orientée Objet JAVA
ISITCOM 2020-2021 Raoudha Ben Djemaa

new Personne("Bon", "Jean", "Paris", "01302040", 1, 4, 1964);

auteur1.afficher();
auteur2.afficher();
}
}

Résultat d’affichage :

Nom :CELERE
habite :Lyon
date de naissance :1/2/1970
Tel :04032945

Nom :Bon
habite :Paris
date de naissance :1/4/1964
Tel :01302040

5/30
Université de Sousse Programmation Orientée Objet JAVA
ISITCOM 2020-2021 Raoudha Ben Djemaa

Etape 3 :

class Date
{
private int j; // jour
private int m; // mois
private int a; // an
Date()
{
j = 1;
m = 1;
a = 2004;
}
Date(int j, int m, int a)
{
this.j = j;
this.m = m;
this.a = a;
}
void initialise(int jour, int mois, int an)
{
j = jour;
m = mois;
a = an;
}
void afficher()
{
System.out.println(j + "/" + m + "/" + a);
}
String getDate()
{
return (j + "/" + m + "/" + a);
}
}

class Personne
{
private String nom, prenom, adresse, telephone;
private Date naissance;
public Personne(
String n,
String p,
String adr,
6/30
Université de Sousse Programmation Orientée Objet JAVA
ISITCOM 2020-2021 Raoudha Ben Djemaa

String tel,
int a,
int m,
int j)
{
nom = n;
prenom = p;
adresse = adr;
telephone = tel;
naissance = new Date(a, m, j);
}
public void afficher()
{
String texte = "Nom" + nom;
System.out.println(
"Nom :"
+ nom
+ " \n"
+ "habite :"
+ adresse
+ " \n"
+ "date de naissance :"
+ naissance.getDate()
+ " \n"
+ "Tel :"
+ telephone
+ " \n");
}
public String getNom()
{
return nom;
}
public String getPrenom()
{
return prenom;
}
public String getAdresse()
{
return adresse;
}
public String getTelephone()
{
return telephone;
7/30
Université de Sousse Programmation Orientée Objet JAVA
ISITCOM 2020-2021 Raoudha Ben Djemaa

}
public Date getNaissance()
{
return naissance;
}
}

class Compte
{
Private int num;
private int solde;

Personne titulaire;

Compte(int n,int s, Personne p)

{ num = n;
solde = s;
titulaire = p;
}
void crediter(int m)
{
solde = solde + m;
}

boolean debiter(int m)
//signature de la méthode
{
if (m > 0 && m <= solde)
{
solde = solde - m;
return true;
}
return false;
}

int getNum()
{
return num; }
8/30
Université de Sousse Programmation Orientée Objet JAVA
ISITCOM 2020-2021 Raoudha Ben Djemaa

void afficher()
{
System.out.println("Compte Nø : " + num);
System.out.println("Solde : " + solde);
titulaire.afficher();
}

class TestBanque
{

public static void main(String args[])


{
System.out.println("Creation de la personne p1");
Personne p1 =
new Personne("CELERE", "Jacques", "Lyon",
"04032945", 1, 2, 1970);

System.out.println("Creation de la personne p2");


Personne p2 =
new Personne("Bon", "Jean", "Paris", "01302040", 1, 4, 1964);

System.out.println("Creation du compte c1 pour Monsieur CELERE");


compte c1 = new compte(10, 1000, p1);

System.out.println("Creation du compte c2 pour Monsieur bon");


compte c2 = new compte(11, 1000, p2);

System.out.println("Affichage des comptes");


c1.afficher();
c2.afficher();

System.out.println("Operation de credit de 2500 euros sur c1");


c1.crediter(2500);
c1.afficher();

9/30
Université de Sousse Programmation Orientée Objet JAVA
ISITCOM 2020-2021 Raoudha Ben Djemaa

System.out.println("Operation de debit de 3000 euros sur c2");


if (c2.debiter(3000))
c2.afficher();
else
System.out.println("Operation impossible : solde insuffisant");

}
}//fin de la classe compte

Résultat d’affichage :

Creation de la personne p1
Creation de la personne p2
Creation du compte c1 pour Monsieur Bon
Creation du compte c2 pour Monsieur Celere
Afichage des comptes
Compte Nø : 1000
Solde : 1000

Compte Nø : 1000
Solde : 1000
Operation de credit de 2500 francs sur c1
Compte Nø : 1000
Solde : 3500
Operation de debit de 3000 francs sur c2
Operation impossible : solde insuffisant

10/30

Vous aimerez peut-être aussi