diff --git a/.gitignore b/.gitignore index a1c2a23..3004d02 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ # Compiled class file -*.class # Log file *.log diff --git a/FamilyOOP/Animal.java b/FamilyOOP/Animal.java new file mode 100644 index 0000000..b41980a --- /dev/null +++ b/FamilyOOP/Animal.java @@ -0,0 +1,14 @@ +package FamilyOOP; + +/**Это абстрактный класс животные*/ +abstract class Animal { + protected String type; + protected int height; + protected int weight; + protected String wool; + + /**Метод описывает какие звуки издает животное*/ + abstract void voice(); + /**С помощью метода описывается специфичное для каждого вида движение животного*/ + abstract void gogo(); +} \ No newline at end of file diff --git a/FamilyOOP/Cat.java b/FamilyOOP/Cat.java new file mode 100644 index 0000000..bc1a098 --- /dev/null +++ b/FamilyOOP/Cat.java @@ -0,0 +1,56 @@ +package FamilyOOP; + +import java.util.Iterator; + +/**Класс кот, описывает конкретный вид животного*/ +class Cat extends Animal implements Iterator, Comparable { + @Override + void voice() { + System.out.println("мяу"); + } + @Override + void gogo() { + System.out.println("Идти"); + } + + Cat(String type, int weight, String wool){ + this.type = type; + this.weight = weight; + this.wool = wool; + } + Cat(Object n1, Object n2) { + + } + + int index; + + @Override + public boolean hasNext() { + return index++ < 3; + } + + /**Метод, возвращающий по каждому отдельному индексу определенное поле класса.*/ + @Override + public String next() { + switch (index) { + case 1 -> { + return String.format("Порода: %s", type); + } + case 2 -> { + return String.format("Котик весит: %s кг", weight); + } + case 3 -> { + return String.format("Шерсть у котика: %s", wool); + } + } + return null; + + } + /**Метод для сравнения двух классов по полю вес*/ + @Override + public int compareTo(Cat o) { + if (this.weight > o.weight) return 1; + else if (this.weight < o.weight) return -1; + else return 0; + } +} diff --git a/FamilyOOP/Children.java b/FamilyOOP/Children.java new file mode 100644 index 0000000..54c7fce --- /dev/null +++ b/FamilyOOP/Children.java @@ -0,0 +1,44 @@ +package FamilyOOP; + +class Children extends Human implements IPetOwner { + private final String name; + + Children(String name) { + this.name = name; + } + + @Override + public String toString() { + return this.name; + } + + int hunger() { + return (int)(Math.random() * 9); + } + void goEat() { + System.out.println("Ест..."); + int food = (int)(Math.random() * 3); + int hunger = hunger() - food; + int trueLevelFood = 5; + if (hunger == trueLevelFood) + System.out.println("Ребенок больше не голоден"); + else if (hunger < trueLevelFood) { + goEat(); + } + else { + System.out.println("Ребенок переел"); + } + } + @Override + int go(int start) { + int step = (int)(Math.random() * 7); + int finish = start + step; + return finish; + } + + @Override + public void call() { + System.out.println("Агу Агу"); + } +} + diff --git a/FamilyOOP/Family.java b/FamilyOOP/Family.java new file mode 100644 index 0000000..a4c6fb9 --- /dev/null +++ b/FamilyOOP/Family.java @@ -0,0 +1,20 @@ +package FamilyOOP; + +import java.util.ArrayList; +import java.util.List; + +class Family { + List family; + + Family() { + family = new ArrayList<>(); + } + void add(T name) { + family.add(name); + } + + @Override + public String toString() { + return String.format("%s", family); + } +} diff --git a/FamilyOOP/Grandma.java b/FamilyOOP/Grandma.java new file mode 100644 index 0000000..d908e7a --- /dev/null +++ b/FamilyOOP/Grandma.java @@ -0,0 +1,13 @@ +package FamilyOOP; + +class Grandma extends Sister { + String name; + Grandma(String name) { + this.name = name; + } + + @Override + public String toString() { + return String.format("%s", name); + } +} diff --git a/FamilyOOP/Human.java b/FamilyOOP/Human.java new file mode 100644 index 0000000..4f26ac5 --- /dev/null +++ b/FamilyOOP/Human.java @@ -0,0 +1,35 @@ +package FamilyOOP; + +/**Абстрактный класс человек*/ +abstract class Human { + private int age; + private String gender; + private String familyStatus; + private String havingChildren; + private String colorHair; + + public int getAge() { + return age; + } + + public String getFamilyStatus() { + return familyStatus; + } + + public String getHavingChildren() { + return havingChildren; + } + + abstract int go(int start); + + /** + * Метод, показывающий семейный стутус каждого члена семьи, а именно выводит семейное положение, наличие детей и дополнительно - возраст + */ + void showFamilyStatus() { + int age = getAge(); + String status = getFamilyStatus(); + String ch = getHavingChildren(); + System.out.printf("Человек в возрасте: %d лет. Семейное положение: %s. Дети: %s", age, status, ch); + } + +} diff --git a/FamilyOOP/IMother.java b/FamilyOOP/IMother.java new file mode 100644 index 0000000..fef9f21 --- /dev/null +++ b/FamilyOOP/IMother.java @@ -0,0 +1,6 @@ +package FamilyOOP; + +public interface IMother { + void cleanHome(int time); + void bringUpChildren(); +} diff --git a/FamilyOOP/IPetOwner.java b/FamilyOOP/IPetOwner.java new file mode 100644 index 0000000..3e47ef0 --- /dev/null +++ b/FamilyOOP/IPetOwner.java @@ -0,0 +1,5 @@ +package FamilyOOP; + +public interface IPetOwner { + void call(); +} diff --git a/FamilyOOP/Program.java b/FamilyOOP/Program.java new file mode 100644 index 0000000..a3a5105 --- /dev/null +++ b/FamilyOOP/Program.java @@ -0,0 +1,58 @@ +package FamilyOOP; + +import java.util.ArrayList; + +public class Program { + + public static void main(String[] args) { + Children ch1 = new Children("George"); + Children ch2 = new Children("Ben1"); + Children ch3 = new Children("Ben2"); + Children ch4 = new Children("Ben3"); + Grandma grandMa = new Grandma("Dolly"); + + +// System.out.println(ch1.go(2)); +// ch1.call(); +// ch1.goEat(); + + Family family1 = new Family<>(); + family1.add(ch1); + family1.add(ch2); + family1.add(ch3); + family1.add(ch4); + family1.add(grandMa); + System.out.println(family1); + + + + +// Cat cat1 = new Cat("Вислоухий", 2, "темная"); +// Cat cat2 = new Cat("Вислоухий", 6, "темная"); +//// +// Iterator components = cat1; +// while (components.hasNext()) {System.out.println(cat1.next());} + +// List cats = new ArrayList<>(); +// for (int i = 0; i < 3; i++) { +// cats.add(new Cat("id: " + i, "вес: "+ i * 10)); // пока не работает +// } +// +// System.out.println(cats); +//// Collections.sort(cats); +// cats.sort(Comparator.comparingInt(cat -> cat.weight)); +// System.out.println(cats); +// +// Grandma Meg = new Grandma(); +// System.out.println(Meg.go(6)); + + + + + + + + + + } +} \ No newline at end of file diff --git a/FamilyOOP/Sister.java b/FamilyOOP/Sister.java new file mode 100644 index 0000000..5b6a295 --- /dev/null +++ b/FamilyOOP/Sister.java @@ -0,0 +1,38 @@ +package FamilyOOP; + +class Sister extends Human implements IMother, IPetOwner { + private String SisAndBro; + public String getSisAndBro() {return SisAndBro;} + + @Override + int go(int steps) { + int start = 0; + int move = start + steps; + return move; + } + @Override + void showFamilyStatus() { + int age = getAge(); + String status = getFamilyStatus(); + String ch = getHavingChildren(); + String sandb = getSisAndBro(); + System.out.printf("Человек в возрасте: %d лет. Семейное положение: %s. Дети: %s. Братья/Сестры : %s", age, status, ch, sandb); + } + @Override + public void cleanHome(int effort) { + if (effort < 100 && effort > 50) {System.out.println("Идеальная уборка!");} + else if (effort < 50 && effort > 20) {System.out.println("Хорошо постарались");} + else if (effort < 20 && effort > 0) {System.out.println("Плохо!");} + else System.out.println("Какая-то ошибка..."); + } + @Override + public void bringUpChildren() { + System.out.println("Не балуйся!"); + } + + @Override + public void call() { + System.out.println("Иди сюда!"); + } + +} diff --git a/Human.java b/Human.java new file mode 100644 index 0000000..b5ac5ed --- /dev/null +++ b/Human.java @@ -0,0 +1,125 @@ +package Family; + +class Human{ + private int age; + private String gender; + private String familyStatus; + private String havingChildren; + private String colorHair; + + Human(int age, String familyStatus, String havingChildren) { + this.age = age; + this.familyStatus = familyStatus; + this.havingChildren = havingChildren; + } + + Human(int age, String gender) { + this.age = age; + this.gender = gender; + } + + Human(int age, String familyStatus, String havingChildren, String colorHair) { + this(age,familyStatus, havingChildren); + this.colorHair = colorHair; + + } + + public int getAge() {return age;} + public String getGender() {return gender;} + public String getFamilyStatus() {return familyStatus;} + public String getHavingChildren() {return havingChildren;} + public String getColorHair() {return colorHair;} + public void setAge(int age) {this.age = age;} + public void setGender(String gender) {this.gender = gender;} + public void setFamilyStatus(String familyStatus) {this.familyStatus = familyStatus;} + public void setHavingChildren(String havingChildren) {this.havingChildren = havingChildren;} + public void setColorHair(String colorHair) {this.colorHair = colorHair;} + void showFamilyStatus() { + int age = getAge(); + String status = getFamilyStatus(); + String ch = getHavingChildren(); + System.out.printf("Человек в возрасте: %d лет. Семейное положение: %s. Дети: %s", age, status, ch); + } +} + +class Father extends Human { + Father(int age, String familyStatus, String colorEye, String havingChildren, String havingBrothersAndSisters) { + super(age, familyStatus, havingChildren); + setColorEye(colorEye); + setHavingBrothersAndSisters(havingBrothersAndSisters); + } + private String colorEye; + private String havingBrothersAndSisters; + + public void setColorEye(String colorEye) {this.colorEye = colorEye;} + public void setHavingBrothersAndSisters(String havingBrothersAndSisters) {this.havingBrothersAndSisters = havingBrothersAndSisters;} + + public String getColorEye() {return colorEye;} + public String getHavingBrothersAndSisters() {return havingBrothersAndSisters;} +} + +class Mother extends Human { + Mother(int age, String familyStatus, String havingChildren, String colorHair, String education){ + super(age, familyStatus, havingChildren, colorHair); + this.education = education; + } + Mother(int age, String gender) { + super(age, gender); + } + private String education; + + public void setEducation(String education) {this.education = education;} + public String getEducation() {return education;} + + void voice(String name) { + System.out.println(name + " иди есть суп"); + } + +} + +class Children extends Mother { + Children(String name, int age, String gender, String parents) { + super(age, gender); + this.parents = parents; + this.name = name; + } + private String parents; + private String name; + int hunger() { + return (int)(Math.random() * 9); + } + void goEat() { + System.out.println("Ест..."); + int food = (int)(Math.random() * 3); + int hunger = hunger() - food; + int trueLevelFood = 5; + if (hunger == trueLevelFood) + System.out.println("Ребенок больше не голоден"); + else if (hunger < trueLevelFood) { + goEat(); + } + else { + System.out.println("Ребенок переел"); + } + } + public void setParents(String parents) {this.parents = parents;} + public String getParents() {return parents;} + +} + + +class Family { + public static void main(String[] args) { + Father father = new Father(45, "не женат", "голубые глаза", "1 ребенок", "нет сестер"); + Mother mother = new Mother(34, "не замужем", "1 ребенок", "рыжые волосы", "высшее"); + Children children1 = new Children("Степан", 12, "мужской", "Лев и Марина"); + Children children2 = new Children("Дмитрий", 18, "мужской", "Леонид и Марина"); + + mother.setHavingChildren("2 ребенка"); + + mother.voice("Степан,"); + children1.goEat(); + father.showFamilyStatus(); + } + +} \ No newline at end of file