-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
My zoo #1
base: master
Are you sure you want to change the base?
My zoo #1
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,12 @@ | ||
# jv-homework-template | ||
jv-zoo | ||
Цель: прокачать навыки в ООП и работе с Generics | ||
|
||
Ваша задача заключается в том чтобы создать свой зоопарк, разместить животных по вольерам и накромить их. Создание инстансов животных и кормление можно проводить в классе App(или Main или любом другом). В данном темплейте есть класс HelloWorld и класс HelloWorldTest. Feel free to remove them) | ||
|
||
Создать зоопарк | ||
В зоопарке должны быть вольер для птиц, для всех животных и аквариум | ||
Создать классы тигр, пингвин, воробей и акула и добавить в соответствующие среды обитания (см. п.2) | ||
Птицы должны уметь летать, рыбы должны уметь плавать. Обратите внимание на пингвина, там не все так просто: он может плавать, но не может летать, при єтом является птицей | ||
Должна быть возможность накормить всех животных | ||
Для решения задачи воспользоваться Дженериками |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* Feel free to remove this class and create your own. | ||
*/ | ||
public class App { | ||
public static void main(String[] args) { | ||
Zoo a1 = new Shark("Masanya_1"); | ||
Zoo a2 = new Pingvin("Kovalski_1"); | ||
Zoo a3 = new Tiger("Tigrula_1"); | ||
Zoo a4 = new Sparrow("Chijik_1"); | ||
|
||
Fish f1 = new Shark("Masanya_2"); | ||
Bird b1 = new Pingvin("Kovalski_2"); | ||
Mammal m1 = new Tiger("Tigera_2"); | ||
Shark mas1 = new Shark("Masanya_2"); | ||
Pingvin p1 = new Pingvin("Kovalski_3"); | ||
Tiger l1 = new Tiger("Tigrula_3"); | ||
Sparrow s1 = new Sparrow("Chijik_3"); | ||
Swimable sw1 = new Pingvin("Kovalski_4"); | ||
Flyableable s2 = new Sparrow("Chijik_4"); | ||
ArrayList<Zoo> array1 = new ArrayList<>();//All animal | ||
array1.add(a1); | ||
array1.add(a2); | ||
array1.add(a3); | ||
array1.add(a4); | ||
ArrayList<Fish> array2 = new ArrayList<>();//Acvarium | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yura1977 Я очікую що тут буде класс Cage з дженериками, WildCard і т.д |
||
array2.add(f1); | ||
array2.add(mas1); | ||
ArrayList<Bird> array3 = new ArrayList<>();//Birdcage | ||
array3.add(s1); | ||
array3.add(b1); | ||
array3.add(p1); | ||
|
||
// ancage.addAll(ancage); | ||
//Zoo[] array1 = {a1, a2, a3,a4, f1, b1, m1, mas1, p1, l1}; | ||
//Flyableable[] array2 = { s1,s2, b1, m1, p1, l1}; | ||
|
||
for (Zoo a : array1) { | ||
if (a instanceof Shark) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yura1977 Це взагалі фігня) |
||
Shark m = (Shark) a; | ||
System.out.println(m.name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yura1977 p.name? А інкапсуляція? А Getters and Setters? |
||
m.eat(); | ||
m.swim(); | ||
} else if (a instanceof Pingvin) { | ||
Pingvin p = (Pingvin) a; | ||
System.out.println(p.name); | ||
p.eat(); | ||
p.swimable(); | ||
} else if (a instanceof Tiger) { | ||
Tiger t = (Tiger) a; | ||
System.out.println(t.name); | ||
t.eat(); | ||
t.run(); | ||
}else if (a instanceof Sparrow){ | ||
Sparrow s = (Sparrow)a; | ||
System.out.println(s.name); | ||
s.eat(); | ||
s.flyable(); | ||
} | ||
System.out.println("-----------------------"); | ||
} | ||
for (Bird b : array3) { | ||
if (b instanceof Pingvin) { | ||
Pingvin p = (Pingvin) b; | ||
System.out.println(p.name); | ||
p.eat(); | ||
p.swimable(); | ||
} else if (b instanceof Sparrow) { | ||
Sparrow s = (Sparrow) b; | ||
System.out.println(s.name); | ||
s.eat(); | ||
s.flyable(); | ||
} | ||
System.out.println("-----------------------"); | ||
} | ||
} | ||
} | ||
abstract class Zoo { | ||
String name; | ||
Zoo(String name){ | ||
this.name = name; | ||
} | ||
|
||
abstract void eat(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yura1977 В зоопарку є метод їсти??? Серйозно? |
||
|
||
} | ||
|
||
abstract class Fish extends Zoo { | ||
//protected String name; | ||
public Fish(String name){ | ||
super (name); | ||
this.name = name; | ||
} | ||
|
||
abstract void swim(); | ||
} | ||
|
||
abstract class Bird extends Zoo implements Flyableable { | ||
//private String name; | ||
public Bird(String name){ | ||
super(name); | ||
this.name = name; | ||
} | ||
//abstract void fly(); | ||
@Override | ||
public void flyable (){ | ||
System.out.println(name + "fly"); | ||
} | ||
} | ||
|
||
abstract class Mammal extends Zoo implements Flyableable { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yura1977 Mammal це самець? Якщо так . - то він має має імплементити Flyableable? |
||
//String name; | ||
Mammal (String name){ | ||
super(name); | ||
this.name =name; | ||
} | ||
abstract void run(); | ||
|
||
} | ||
interface Swimable{ | ||
void swimable(); | ||
} | ||
interface Flyableable{ | ||
default void flyable(){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yura1977 А ти впевнений що цей метод має бути дефолтним? |
||
System.out.println("Somebody flyable"); | ||
} | ||
} | ||
|
||
class Shark extends Fish { | ||
//protected String name; | ||
Shark(String name){ | ||
super(name); | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
protected void eat() { | ||
System.out.println("Shark is predator and eat fish"); | ||
} | ||
@Override | ||
public void swim (){ | ||
System.out.println("Shark is a great swimer"); | ||
} | ||
} | ||
class Sparrow extends Bird{ | ||
Sparrow(String name){ | ||
super(name); | ||
this.name = name; | ||
} | ||
@Override | ||
protected void eat() { | ||
System.out.println("Sparrow eat grain"); | ||
} | ||
@Override | ||
public void flyable (){ | ||
System.out.println(name + "Sparrow fly very well"); | ||
} | ||
} | ||
class Pingvin extends Bird implements Swimable { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yura1977 А що відбудеться коли викличемо метод fly() у пінгвіна? |
||
//private String name; | ||
Pingvin(String name){ | ||
super(name); | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
protected void eat() { | ||
System.out.println("Pinguin lubit est ribu!"); | ||
} | ||
|
||
@Override | ||
public void swimable() { | ||
System.out.println("Pinguin can swim but not fly"); | ||
} | ||
} | ||
|
||
class Tiger extends Mammal { | ||
//private String name; | ||
Tiger(String name){ | ||
super(name); | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
protected void eat() { | ||
System.out.println("Tiger like meet"); | ||
} | ||
|
||
@Override | ||
void run() { | ||
System.out.println("Tiger - can run!"); | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Yura1977 називай змінні більш зрозуміло