Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. public class  Animal
  2. {
  3.     String AnimalSpecies;
  4.     String FoodSource;
  5.     private short AverageLife;
  6.     private char Sex;
  7.     public static int index;
  8.    
  9.     Animal() {
  10.         index++;
  11.         AnimalSpecies = "" ;
  12.         FoodSource = "" ;
  13.         AverageLife = (short)0;
  14.         Sex = 'M';
  15.     }
  16.    
  17.     Animal(String n, String food, short a, char s){
  18.         index++;
  19.         AnimalSpecies= n;
  20.         SetFoodSource (food);
  21.         SetAverageLife(a);
  22.         SetSex(s);
  23.     }
  24.    
  25.     Animal(Animal obj){
  26.         index++;
  27.         AnimalSpecies = obj.AnimalSpecies;
  28.         FoodSource = obj.FoodSource;
  29.         AverageLife = obj.GetAverageLife();
  30.         Sex = obj.GetSex();
  31.     }
  32.  
  33.     short GetAverageLife() {
  34.         return AverageLife;
  35.     }
  36.    
  37.     void SetAverageLife (short a ) {
  38.         if(a>=0 && a<=150)
  39.         AverageLife = a;
  40.         else
  41.         AverageLife = 0;
  42.    
  43.     }
  44.    
  45.     char GetSex() {
  46.         return Sex;
  47.     }
  48.    
  49.     void SetSex(char s) {
  50.         if(s=='M' || s=='m' || s=='F' || s=='f')
  51.             Sex = s;
  52.         else
  53.             Sex = 'M';
  54.     }
  55.    
  56.     String GetFoodSource() {
  57.         return FoodSource;
  58.     }
  59.    
  60.     void SetFoodSource(String food){
  61.         if(food== "carnivorous " || food== "herbivores" ||  food== "omnivorous" )
  62.             FoodSource = food;
  63.         else
  64.             FoodSource="omnivorous" ;
  65.     }
  66.    
  67.     void GetAnimalInfo(){
  68.         System.out.println("Info for animal " + index + ":");
  69.         System.out.println("Species = " + AnimalSpecies);
  70.         System.out.println("FoodSource = " + FoodSource);
  71.         System.out.println("AverageLife = " + GetAverageLife());
  72.         System.out.println("Sex = " + GetSex());
  73.     }
  74. }
  75.  
  76. public class DemoAnimal{
  77.     Animal a1 = new Animal("Tiger", "carnivorous" ,(short)15, 'm');
  78.     Animal a2 = new Animal("Horse", "herbivores" , (short)25, 'm');
  79.     Animal a3 = new Animal("Raven", "omnivorous"  , (short)15, 'm');
  80.    
  81.     a1.GetAnimalInfo();
  82.     a2.GetAnimalInfo();
  83.     a3.GetAnimalInfo();
  84. }
  85.  
  86. public class Main
  87. {
  88.     public static void main(String args[])
  89.     {
  90.         DemoAnimal demoAnimal = new DemoAnimal();
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement