1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| public class Ville {
String nomVille;
String nomPays*;
int nbreHabitants;
char categorie;
// Méthode String.toString()
public String toString(){
return "\t"+this.nomVille+" est une ville de "+this.nomPays + ", elle comporte : "+ this.nbreHabitants +
" => elle est donc de catégorie : " + this.categorie;
}
// Constructeur par défaut
//************************
public Ville(){
System.out.println("Création d'une ville !");
nomVille = "inconnu";
nomPays = "inconnu";
nbreHabitants = 0;
this.setCategorie()*;
}
// Constructeur avec paramètres:
//*****************************
public Ville(String pNom, int pNbre, String pPays){
throws NombreHabitantException
{
if (pNbre < 0)
throw new NombreHabitantException();
nomVille = pNom;
nomPays = pPays;
nbreHabitants = pNbre;
this.setCategorie();
}
}
//**************ACCESSEURS (GETTEURS)*********
// Retourne le nom de la ville :
public String getNom() {
return nomVille;
}
// Retourne le nom du pays :
public String getNomPays() {
return nomPays;
}
// Retourne le nbre d'habitants :
public int getNbreHabitants() {
return nbreHabitants;
}
//**************MUTATEURS (SETTEURS)*********
// Définit le nom de la ville :
public void setNom(String pNom) {
nomVille = pNom;
}
// Défionit le nom du pays :
public void setNomPays(String pPays) {
nomPays = pPays;
}
// Définit le nombre d'habitants :
public void setNbreHabitants( int nbre) {
nbreHabitants = nbre;
this.setCategorie();
}
// Définit la catégorie de la ville :
public void setCategorie() {
int bornesSuperieures [] ={0,1000,10000,100000,
500000,1000000,5000000,10000000};
char categorie [] = {'?','A','B','C','D','E','F','G','H'};
int i = 0;
while (i<bornesSuperieures.length && this.nbreHabitants > bornesSuperieures[i])
i ++;
this.categorie = categorie[i];
}
} |
Partager