Vous êtes sur la page 1sur 4

Correction Exercice 3 

La méthode toString() est utilisée par la machine Java pour représenter un objet sous forme d'une chaîne de
caractères.

Produit P = new Produit (1,"délice", "Danone",2500) ;


System.out.println("Produit : " + P) ;
Est équivalent à

System.out.println("Produit : " + P.toString()) ;

Dans la pratique, le code va nous afficher  P@b82e3f203. Les chaînes de caractères retournées par la méthode toString() de la classe Object ont toutes
@
cette forme : le nom de la classe, suivie du caractère  , et une adresse en hexadécimal, qui est l'adresse mémoire où l'objet considéré est enregistré.

Il est possible de changer ce comportement, et d'afficher une chaîne de caractères par un mécanisme qui s'appelle la "surcharge" (Override) //à étudier avec le chapitre héritage

Surcharge de  toString() dans la classe Produit

public String toString() {


return "id :" + id + " prix :" + prix + "marque :" + marque
+ "libelle:" + libelle ;
}
System.out.println("Produit : " + P.toString()) ;

Les lignes suivantes ajoutent des éléments supplémentaires. Le résultat de l'appel à cette méthode  toString() devra ressembler à ceci :

id : 1
prix : 2.500
marque : délice
libelle : Danone
1. Ajouter un attribut « date» de type Date et ajouter les dates aux produits existants.

import java.util.Date;

Class Produit

import java.util.Date;
public class Produit {
//declaration des attributs
private int id;
private String libelle, marque;
private float prix;
private Date date;

//constructeur vide
public Produit() {}
//Constructeur à deux attributs
public Produit(int id, String marque) {
this.id = id;
this.marque = marque;
}
//Constructeur à trois attributs
public Produit(int id, String libelle, String marque) {
this.id = id;
this.libelle = libelle;
this.marque = marque;
}
//Constructeur à quatre attributs
public Produit(int id, String libelle, String marque, float prix) {
this.id = id;
this.libelle = libelle;
this.marque = marque;
this.prix = prix;
}
//Constructeur à cinq attributs
public Produit(int id, String libelle, String marque, float prix, Date date)
{
this.id = id;
this.libelle = libelle;
this.marque = marque;
this.date = date;
this.prix = prix;
}
//définition de la méthode afficher
public void afficher() {
System.out.println("id :" + id + "\nprix :" + prix + " \nlibelle :"
+ libelle + "\nmarque" + marque + "date :" + date);
}

//définition de la méthode toString


public String toString() {
return "id :" + id + " prix :" + prix + "marque :" + marque
+ " libelle :" + libelle + "Date :" + date;
}
//Définition des getter et setter
public void setPrix(float prix) {
this.prix = prix;
}

public float getPrix() {


return prix;
}

public void setId(int id) {


this.id = id;
}

public int getId() {


return id;
}

public void setMarque(String marque) {


this.marque = marque;
}

public String getMarque() {


return marque;
}

public void setLibelle(String libelle) {


this.libelle = libelle;
}
public String getLibelle() {
return libelle;
}

public Date getDate() {


return date;
}

public void setDate(Date date) {


this.date = date;
}

}
Class TestProduit

import java.util.Date;
public class TestProduit {
public static void main(String[] args) {
//instanciation du produit p1 et test des méthodes set et get
Produit p1 = new Produit();
p1.setId(1);//si id est déclaré public p1.id=1 ;
p1.setLibelle("lait");
p1.setPrix(1000);
System.out.println(" prix :" + p1.getPrix());//p1.prix ; si prix est
déclaré public
System.out.println("id :" + p1.getId());
System.out.println("libelle :" + p1.getLibelle());
//instanciation du produit p2 et test des méthodes set et get
Produit p2 = new Produit(1021, "Lait", "Delice");
p2.setPrix(1000);
System.out.println("Marque :" + p2.getMarque());
System.out.println("id :" + p2.getId());
System.out.println("libelle :" + p2.getLibelle());
//instanciation du produit p3
Produit p3 = new Produit(10, "Délice");
p3.setPrix(12);
//instanciation du produit p4
Produit p4 = new Produit(12, "Tomate", "sicam", 102);

//test de la méthode afficher


System.out.println("test de la méthode afficher");
p1.afficher();
p2.afficher();
p3.afficher();
p4.afficher();

//instanciation du Produit p avec un attribut date


Date d=new Date();
Produit p = new Produit(22, "biscuit", "saida", 250,d);
p.afficher();

/* try{
SimpleDateFormat simpleDateFormat =new SimpleDateFormat("dd/MM/yyyy");
Date d1;
Date d2;
String DateCrea = "22/11/2019";
String DateExp = "30/06/2020";
d1=simpleDateFormat.parse(DateCrea);

d2= simpleDateFormat.parse(DateExp);
System.out.println(d1);
System.out.println(d2);
Produit p5 = new Produit(10, "pull", "zara", 1000, d1);
Produit p6 = new Produit(10, "pull", "zara",
1000, d2);
p5.afficher();
p6.afficher();
}catch (ParseException e){e.printStackTrace();}
*/

//test méthde to String


System.out.println("test méthde to String pour p");
System.out.println(p.toString());
}
}

Vous aimerez peut-être aussi