Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // zwróć uwagę, że jest druga zakładka z klasą Animal
- class Main {
- /**
- * Pola statyczne:
- */
- public static final int NUMBER_OF_SPECIES = 6;
- /**
- * Pola:
- */
- public String species; // gatunek
- public boolean isMale; // czy płeć męska?
- public int weight; // waga
- /**
- * Konstruktory
- */
- public Main(String species, boolean isMale, int weight) {
- setSpecies(species);
- setisMale(isMale);
- setWeight(weight);
- }
- /**
- * Metody
- */
- //setery i getery
- //do species
- //seter species
- public void setSpecies(String spec){
- String[] types1= getPossibleTypes(); //types1 to tablica stringów
- //mająca w każdym swoim elemencie [0] [1] itd
- //odpowiednio każdy gatunek jakie może mieć zwierze czyli mammals, birds itd
- //wzięliśmy to żeby móc zaraz sprawdzić czy to co chcemy ustawić jako gatunek
- //w naszym animalsie należy do tego zbioru "dopuszczalnych typów"
- int leng=types1.length; //types1.length to długość tablicy czyli jest 6 możliwych gatunków
- for(int i=0;i<leng;i++){
- if(spec==types1[i]){ //jak to co chcemy ustawić jest w tablicy
- this.species=spec; //to ustawiamy
- return ; //i kończymy
- }
- }
- //a jak nie było to piszemy że błąd, nie ustawiamy tego i kończymy
- System.out.println("Bledny gatunek!");
- return ;
- }
- //geter species
- public String getType(){
- return species;
- }
- //do male
- //geter male
- public boolean getMale(){
- return isMale;
- }
- //setter male
- public void setisMale(boolean value){
- isMale=value;
- }
- //changer male
- public void changeMale(){
- this.isMale = !this.isMale;
- }
- //do weight, waga
- //geter weight
- public int getWeight(){
- return weight;
- }
- //setter male
- public void setWeight(int value)
- {
- if(value>0){
- weight=value;
- }
- else
- {
- System.out.println("Waga nie moze byc ujemna!");
- }
- }
- /**
- * Metody statyczne
- */
- public static String[] getPossibleTypes(){
- String[] types = {"mammals", "birds", "reptiles", "amphibians", "insects", "fish" };
- return types;
- }
- public static void main(String[] args) {
- Animal animal = new Animal("birds", true, 20);
- System.out.println("Is male?: " + animal.isMale);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement