Advertisement
linus_minus

Untitled

Jan 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.01 KB | None | 0 0
  1.  
  2. public class Sdz1 {
  3.  
  4.     public static void main(String[] args) {
  5.         // TODO Auto-generated method stub
  6.        
  7.        
  8.         Ville v  = new Ville();
  9.        
  10.         Ville v1 = new Ville("Marsehhille", 123456, "France");
  11.         Ville v2 = new Ville("Rio", 4576, "Brésil");
  12.        
  13.         System.out.println("\n\n"+v1.descrisToi());
  14.         System.out.println(v.descrisToi());
  15.         System.out.println(v2.descrisToi()+"\n\n");
  16.         System.out.println(v1.comparer(v2));
  17.        
  18.        
  19.        
  20.        
  21.         System.out.println("===============================================================");
  22.        
  23.         System.out.println("\n v = "+v.getNom()+"ville de "+v.getNombreHabittants()+"Nombre d'habittants"+v.getNomPays());
  24.         System.out.println("\n v1 = "+v1.getNom()+"ville de "+v1.getNombreHabittants()+"Nombre d'habittants"+v1.getNomPays());
  25.         System.out.println("\n v1 = "+v2.getNom()+"ville de "+v2.getNombreHabittants()+"Nombre d'habittants"+v2.getNomPays());
  26.        
  27.    
  28.         //Faire interchanger les deux ville v1 et v2
  29.         Ville temp= new Ville();
  30.         temp = v1;
  31.         v1 = v2;
  32.         v2 = temp;
  33.        
  34.         System.out.println("\n v1 = "+v1.getNom()+"ville de "+v1.getNombreHabittants()+"Nombre d'habittants"+v1.getNomPays());
  35.         System.out.println("\n v1 = "+v2.getNom()+"ville de "+v2.getNombreHabittants()+"Nombre d'habittants"+v2.getNomPays());
  36.        
  37.         /*
  38.          * Interchanre les noms par le biais des mutteurs
  39.          * */
  40.        
  41.         v1.setNom("Hong Kong");
  42.         v2.setNom("Djibouti");
  43.    
  44.         System.out.println("\n v1 = "+v1.getNom()+"ville de "+v1.getNombreHabittants()+"Nombre d'habittants"+v1.getNomPays());
  45.         System.out.println("\n v1 = "+v2.getNom()+"ville de "+v2.getNombreHabittants()+"Nombre d'habittants"+v2.getNomPays());
  46.        
  47.         System.out.print("Vérification si l'héritage foncionne..."+v1.descrisToi()+v1.getMonument()+"=====");
  48.        
  49.        
  50.     }
  51.    
  52.  
  53. }
  54. // Class Ville
  55.  
  56. /*
  57.  *
  58.  * \t : tabulation
  59.    \r : retour chariot
  60.    \n : nouvelle ligne
  61.  *
  62.  */
  63.  
  64. public class Ville {
  65.    
  66.     protected String nomVille;
  67.     protected String nomPays;
  68.     protected int nombHabitants;
  69.     protected char categorie;
  70.    
  71.     //Constructeur
  72.         public Ville() {
  73.            
  74.             System.out.println("Création d'une ville ! ");
  75.             nomVille = "Inconnnu";
  76.             nomPays = "Inconnu";
  77.             nombHabitants = 0;
  78.             this.setCategorie();
  79.            
  80.         }
  81.        
  82.    
  83.     //******* Les accesseurs Gets *********//
  84.    
  85.     public String getNom() {
  86.         return nomVille;
  87.     }
  88.    
  89.     public String getNomPays() {
  90.         return nomPays;
  91.     }
  92.    
  93.     public int getNombreHabittants() {
  94.         return nombHabitants;
  95.     }
  96.    
  97.    
  98.     //****** MUTATEURS sets *******//
  99.    
  100.     //Définir le nom de la ville
  101.     public void setNom(String pNom) {
  102.         nomVille = pNom;
  103.     }
  104.    
  105.     //Définir le pays
  106.     public void setNomPays(String pPays) {
  107.         nomPays = pPays;
  108.     }
  109.    
  110.     //Définir le nombre d'habittants
  111.     public void setNombrebHabittants(int nbre) {
  112.         nombHabitants = nbre;
  113.         this.setCategorie();
  114.     }
  115.    
  116.    
  117.    
  118.     public Ville(String pNom, int pNbr, String pPays) {
  119.        
  120.         System.out.println("Création d'une ville avec des paramètres");
  121.         nomVille = pNom;
  122.         nomPays = pPays;
  123.         nombHabitants =  pNbr;
  124.         this.setCategorie();
  125.        
  126.     }
  127.    
  128.    
  129.    
  130.     //Définir la catégorie de la ville
  131.     private void setCategorie() {
  132.         int bornesSuperieures[] = {0, 1000, 10000, 100000, 500000, 1000000, 5000000, 10000000};
  133.         char categorie[] = {'?', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
  134.        
  135.         int i = 0;
  136.         while (i < bornesSuperieures.length && this.nombHabitants > bornesSuperieures[i])
  137.            
  138.             i++;
  139.        
  140.         this.categorie = categorie[i];
  141.        
  142.     }          
  143.    
  144.     //Retourne la description de la ville
  145.     public String descrisToi() {
  146.         return"\t"+this.nomVille+" est une ville de "+this.nomPays+" elle comporte"+this.nombHabitants+" d'habitants, elle est donc de la catégorie : "+this.categorie;
  147.        
  148.     }
  149.    
  150.     //Retourne une chaine de caractère selon le résultat de la comparaison
  151.     public String comparer(Ville v1) {
  152.         String str = new String();
  153.         if (v1.getNombreHabittants()> this.nombHabitants)
  154.             str = v1.getNom()+"est une ville plus peuplée que "+this.nomVille;
  155.        
  156.         else
  157.              str = this.nomVille+"est une ville plus peuplée que"+v1.getNom();
  158.              return str;
  159.     }
  160.    
  161. }
  162. //Capitale
  163.  
  164. public class Capitale extends Ville {
  165.      
  166.   private String monument;
  167.    
  168.   //Constructeur par défaut
  169.   public Capitale(){
  170.     //Ce mot clé appelle le constructeur de la classe mère
  171.     super();
  172.     monument = "aucun";
  173.   }    
  174.  
  175.      
  176.   //Constructeur d'initialisation de capitale
  177.   public Capitale(String nom, int hab, String pays, String monument){
  178.     super(nom, hab, pays);
  179.     this.monument = monument;
  180.   }    
  181.      
  182.   /**
  183.     * Description d'une capitale
  184.     * @return String retourne la description de l'objet
  185.   */
  186.   public String decrisToi(){
  187.     String str = super.descrisToi() + "\n \t ==>>" + this.monument + "en est un monument ***********lljljmkjkh";
  188.     System.out.println("Invocation de super.descrsiToi");
  189.  
  190.     return str;
  191.     }
  192.  
  193.   /**
  194.     * @return le nom du monument
  195.   */
  196.   public String getMonument() {
  197.     return monument;
  198.   }
  199.  
  200.   //Définit le nom du monument
  201.   public void setMonument(String monument) {
  202.     this.monument = monument;
  203.   }  
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement