Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. public class Chien {
  2. private String nom;
  3. private String race;
  4.  
  5. public Chien(String nom,String race)
  6. {
  7. this.nom=nom;
  8. this.race=race;
  9. }
  10.  
  11. public String getNom()
  12. {
  13. return this.nom;
  14. }
  15. public String getRace()
  16. {
  17. return this.race;
  18. }
  19. }
  20.  
  21.  
  22.  
  23.  
  24.  
  25. public class Chat {
  26. private String nom;
  27.  
  28. public Chat(String nom)
  29. {
  30. this.nom=nom;
  31. }
  32.  
  33. public String getNom()
  34. {
  35. return this.nom;
  36. }
  37. }
  38.  
  39.  
  40.  
  41.  
  42. import java.lang.*;
  43. public class Personne {
  44.    
  45.    private String nom;
  46.    private static int nbAnimaux=0;
  47.    private Object Animal []=new Object [10];
  48.  
  49.    public Personne(String nom)
  50.    {
  51.       this.nom=nom;
  52.    }
  53.  
  54.    public void adopte(Object Animal)
  55.    {
  56.       if (this.nbAnimaux>=10)
  57.       {
  58.          System.out.println("Le nombre d'animaux est trop élevé! Maximum 10 animaux autorisés !");}
  59.      
  60.      
  61.       else{
  62.          this.Animal[nbAnimaux]=Animal;
  63.          nbAnimaux+=1;
  64.       }
  65.    }
  66.    public void sePresente(){
  67.       if(this.Animal.length==1)
  68.       {
  69.          if(this.Animal[0] instanceof Chien)
  70.          {
  71.             System.out.println("Je m'appelle"+" "+this.nom+","+"j'ai 1 animal:");
  72.             System.out.println("Le Chien"+" "+((Chien)this.Animal[0]).getNom()+" "+"de race"+" "+((Chien)this.Animal[0]).getRace());}
  73.          else
  74.          {
  75.             System.out.println("Je m'appelle"+" "+this.nom+","+"j'ai 1 animal:");
  76.             System.out.println("Le Chat"+" "+((Chat)this.Animal[0]).getNom());
  77.          }}
  78.      
  79.       else if (this.Animal.length==0)
  80.       {
  81.          System.out.println("Je m'appelle"+" "+this.nom+","+"je n'ai pas d'animaux:");}
  82.            
  83.       else  {
  84.          int i;
  85.          System.out.println("Je m'appelle"+" "+this.nom+","+"j'ai"+" "+Animal.length+"  animaux:");
  86.          for(i=0;i<Animal.length;i++){
  87.             if(this.Animal[i] instanceof Chien){
  88.                System.out.println("Le Chien"+" "+((Chien)this.Animal[0]).getNom()+" "+"de race"+" "+((Chien)this.Animal[0]).getRace());}
  89.             else{
  90.                System.out.println("Le Chat"+" "+((Chat)this.Animal[0]).getNom());}}}
  91.  }
  92. }
  93.  
  94.  
  95. public class TestPersonne{
  96.    public static void main(String[] args)
  97.    {
  98.       Personne tab[]=new Personne[3];
  99.       tab[0]=new Personne("Celine");
  100.       tab[1]=new Personne("Didier");
  101.       tab[2]=new Personne("Guillaume");
  102.    
  103.       tab[0].adopte(new Chien("Whis","Scottish Terrier"));
  104.       tab[2].adopte(new Chat("Cleo"));
  105.       tab[2].adopte(new Chien("Attila","Bouledog"));
  106.       tab[2].adopte(new Chat("Poussy"));
  107.       for(int i=0;i<3;i++){
  108.          tab[i].sePresente();}
  109.    
  110.    
  111.    }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement