Advertisement
Guest User

J1

a guest
Mar 5th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // zwróć uwagę, że jest druga zakładka z klasą Animal
  2.  
  3. class Main {
  4.  
  5.  /**
  6.      * Pola statyczne:
  7.      */
  8.     public static final int NUMBER_OF_SPECIES = 6;
  9.  
  10.     /**
  11.      * Pola:
  12.      */
  13.     public String species; // gatunek
  14.     public boolean isMale; // czy płeć męska?
  15.     public int weight;     // waga
  16.  
  17.  
  18.     /**
  19.      * Konstruktory
  20.      */
  21.     public Main(String species, boolean isMale, int weight) {
  22.         setSpecies(species);
  23.         setisMale(isMale);
  24.         setWeight(weight);
  25.     }
  26.  
  27.     /**
  28.      * Metody
  29.      */
  30.    
  31.     //setery i getery
  32.    
  33.     //do species
  34.     //seter species
  35.     public void setSpecies(String spec){
  36.         String[] types1= getPossibleTypes();    //types1 to tablica stringów
  37.         //mająca w każdym swoim elemencie [0] [1] itd
  38.         //odpowiednio każdy gatunek jakie może mieć zwierze czyli mammals, birds itd
  39.         //wzięliśmy to żeby móc zaraz sprawdzić czy to co chcemy ustawić jako gatunek
  40.         //w naszym animalsie należy do tego zbioru "dopuszczalnych typów"
  41.         int leng=types1.length; //types1.length to długość tablicy czyli jest 6 możliwych gatunków
  42.         for(int i=0;i<leng;i++){
  43.             if(spec==types1[i]){    //jak to co chcemy ustawić jest w tablicy
  44.                 this.species=spec;  //to ustawiamy
  45.                 return ;    //i kończymy
  46.             }
  47.         }
  48.         //a jak nie było to piszemy że błąd, nie ustawiamy tego i kończymy
  49.         System.out.println("Bledny gatunek!");
  50.         return ;
  51.     }
  52.     //geter species
  53.     public String getType(){
  54.         return species;
  55.     }
  56.  
  57.    
  58.     //do male
  59.     //geter male
  60.     public boolean getMale(){
  61.         return isMale;
  62.     }
  63.    
  64.     //setter male
  65.     public void setisMale(boolean value){
  66.         isMale=value;
  67.     }
  68.     //changer male
  69.     public void changeMale(){
  70.         this.isMale = !this.isMale;
  71.     }
  72.    
  73.     //do weight, waga
  74.     //geter weight
  75.         public int getWeight(){
  76.             return weight;
  77.         }
  78.        
  79.         //setter male
  80.         public void setWeight(int value)
  81.         {
  82.             if(value>0){
  83.                 weight=value;
  84.             }
  85.             else
  86.             {
  87.                 System.out.println("Waga nie moze byc ujemna!");
  88.             }
  89.         }
  90.    
  91.  
  92.     /**
  93.      * Metody statyczne
  94.      */
  95.  
  96.     public static String[] getPossibleTypes(){
  97.         String[] types = {"mammals", "birds", "reptiles", "amphibians", "insects", "fish" };
  98.         return types;
  99.     }
  100.  
  101.  public static void main(String[] args) {
  102.         Animal animal = new Animal("birds", true, 20);
  103.         System.out.println("Is male?: " + animal.isMale);
  104.        
  105.     }
  106.    
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement