// DANS LE FICHIER Cercle.java :
/** classe Cercle
* (exemple complet avec les instructions)
*/
public class Cercle
{
// Attributs :
private int aRayon;
private boolean aPlein;
private String aNom;
/** constructeur naturel */
public Cercle( final int pRayon, final boolean pPlein, final String pNom )
{
this.aRayon = pRayon; // acces a un attribut + affectation
this.aPlein = pPlein;
this.aNom = pNom;
} // Cercle(...)
/** constructeur sans parametre */
public Cercle()
{
this.aRayon = 10;
this.aPlein = false;
this.aNom = "anonyme";
// ces 3 lignes n'en feront plus qu'une lorsqu'on saura appeler l'autre constructeur
} // Cercle()
/** fonction qui calcule et retourne le diametre */
public int getDiameter()
{
int vDiametre; // variable locale non initialisee
vDiametre = 2 * this.aRayon; // affectation
return vDiametre;
} // getDiameter()
/** procedure d'affichage */
public void affiche()
{
String vChaine = "cercle"; // variable locale initialisee
vChaine = vChaine + " " + this.aNom + " : "; // concatenation de String
vChaine = vChaine + this.getDiameter(); // appel de methode
System.out.print( vChaine ); // affichage
} // affiche()
} // Cercle
// DANS LE FICHIER Essai.java :
/** classe de test de la classe Cercle */
public class Essai
{
// pas d'attributs
/** procedure de test */
public void lance()
{
Cercle vC1 = new Cercle( 20, true, "cercle1" ); // creation nouveau cercle
Cercle vC2 = new Cercle(); // creation nouveau cercle avec valeurs par defaut
int vD1 = vC1.aRayon * 2; // interdit car aRayon est prive
int vD1 = vC1.getDiameter(); // OK car getDiameter est publique
vC2.affiche(); // OK
} // lance()
} // Essai