Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pl.sdacademy.zoo.animals.AbstractAnimal;
- import pl.sdacademy.zoo.animals.birds.Duck;
- import pl.sdacademy.zoo.animals.birds.Eagle;
- import pl.sdacademy.zoo.animals.birds.Woodpecker;
- import pl.sdacademy.zoo.animals.mammals.Cat;
- import pl.sdacademy.zoo.animals.mammals.Dog;
- import pl.sdacademy.zoo.animals.mammals.Rabbit;
- public class Main {
- public static void main(String[] args) {
- Eagle eagle = new Eagle(3, "big", "gray"); // class Eagle extends AbstractBirds extends AbstractAnimals implements Flyable
- System.out.println(eagle.toString());
- Duck duck = new Duck(5, "medium", "brown"); // class Duck extends AbstractBirds extends AbstractAnimals implements Flyable
- System.out.println(duck.toString());
- duck.fly();
- duck.makeSound();
- Woodpecker woodpecker = new Woodpecker(1, "small", "black & white"); // class Woodpecker...
- System.out.println(woodpecker.toString());
- Cat cat = new Cat(8, "long", "red"); // class Cat extends AbstractMammals extends AbstractAnimals
- System.out.println(cat.toString());
- Dog dog = new Dog(4, "short", "white");
- System.out.println(dog.toString());
- Rabbit rabbit = new Rabbit(6, "very short", "gray");
- System.out.println(rabbit.toString());
- System.out.println("ZADANIE 2"); // dwuelementowa tablica z klasy abstrakcyjnej AbstractAnimal; dla każdego elementu wywołać
- // metody z klasy bazowej i zsumować wiek obu zwierzątek
- AbstractAnimal[] animals = new AbstractAnimal[2]; // utworzenie tablicy typu AbstractAnimal o nazwie animals
- animals[0] = dog;
- animals[1] = duck;
- int suma = 0; //deklaracja zmiennej przechowującej sumę wieku
- for(AbstractAnimal a : animals) { // dla każdego elementu "a" w tablicy "AbstractAnimal"
- a.eat(); // wywołanie metody eat() dla każdego elementu
- a.makeSound(); // wywołanie metody makeSound() dla każdego elementu tablicy
- suma += a.getAge(); // sumowanie wieku wszystkich zwierząt z tablicy
- }
- System.out.println("Suma wieku: " + suma); // wydrukowanie sumy wieku
- System.out.println("ZADANIE 3"); // dwuelementowa tablica, zaimplementowanie metod fly() i fallDown z interface Flyable.
- Flyable[] birds = new Flyable[2];
- birds[0] = duck;
- birds[1] = eagle;
- for(Flyable f : birds) {
- f.fly();
- f.fallDown();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement