From 74d04499b2cb80b589f6212c8ad94d2922774262 Mon Sep 17 00:00:00 2001 From: Ekarry <81385639+Ekarry@users.noreply.github.com> Date: Sat, 15 May 2021 00:00:09 +0300 Subject: [PATCH 1/4] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D1=88=D1=83=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=B8=D1=82=D1=8C=20=D0=94=D0=97?= =?UTF-8?q?1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HomeWorkApp/Lesson_1/Actions.java | 11 ++++ .../HomeWorkApp/Lesson_1/Barrier.java | 15 ++++++ .../Dmitrieva/HomeWorkApp/Lesson_1/Cat.java | 29 +++++++++++ .../Dmitrieva/HomeWorkApp/Lesson_1/Human.java | 29 +++++++++++ .../HomeWorkApp/Lesson_1/JustDoIt.java | 52 +++++++++++++++++++ .../Dmitrieva/HomeWorkApp/Lesson_1/Road.java | 20 +++++++ .../Dmitrieva/HomeWorkApp/Lesson_1/Robot.java | 29 +++++++++++ .../Dmitrieva/HomeWorkApp/Lesson_1/Wall.java | 20 +++++++ 8 files changed, 205 insertions(+) create mode 100644 src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Actions.java create mode 100644 src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Barrier.java create mode 100644 src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Cat.java create mode 100644 src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Human.java create mode 100644 src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/JustDoIt.java create mode 100644 src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Road.java create mode 100644 src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Robot.java create mode 100644 src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Wall.java diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Actions.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Actions.java new file mode 100644 index 0000000..1ef3f1d --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Actions.java @@ -0,0 +1,11 @@ +package ru.gb.Dmitrieva.HomeWorkApp.Lesson_1; + +public interface Actions { + void run(); + + void jump(); + + int getRunDistance(); + + int getJumpHeight(); +} diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Barrier.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Barrier.java new file mode 100644 index 0000000..ecc685e --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Barrier.java @@ -0,0 +1,15 @@ +package ru.gb.Dmitrieva.HomeWorkApp.Lesson_1; + +public abstract class Barrier { + private String name; + + public Barrier(String name) { + this.name = name; + } + + protected abstract boolean moving(Actions var1); + + public String getName() { + return this.name; + } +} diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Cat.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Cat.java new file mode 100644 index 0000000..618e771 --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Cat.java @@ -0,0 +1,29 @@ +package ru.gb.Dmitrieva.HomeWorkApp.Lesson_1; + +public class Cat implements Actions { + private String name; + private int runDistance; + private int jumpHeight; + + public Cat(String name, int distance, int jumpHeight) { + this.name = name; + this.runDistance = distance; + this.jumpHeight = jumpHeight; + } + + public void run() { + System.out.printf("Cat %s is running %d m\n", this.name, this.getRunDistance()); + } + + public void jump() { + System.out.printf("Cat %s is jumping %d m\n", this.name, this.getJumpHeight()); + } + + public int getRunDistance() { + return this.runDistance; + } + + public int getJumpHeight() { + return this.jumpHeight; + } +} diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Human.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Human.java new file mode 100644 index 0000000..22747fe --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Human.java @@ -0,0 +1,29 @@ +package ru.gb.Dmitrieva.HomeWorkApp.Lesson_1; + +public class Human implements Actions { + private String name; + private int runDistance; + private int jumpHeight; + + public Human(String name, int distance, int jumpHeight) { + this.name = name; + this.runDistance = distance; + this.jumpHeight = jumpHeight; + } + + public void run() { + System.out.printf("Human %s is running %d m\n", this.name, this.getRunDistance()); + } + + public void jump() { + System.out.printf("Human %s is jumping %d m\n", this.name, this.getJumpHeight()); + } + + public int getRunDistance() { + return this.runDistance; + } + + public int getJumpHeight() { + return this.jumpHeight; + } +} diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/JustDoIt.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/JustDoIt.java new file mode 100644 index 0000000..19fadb6 --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/JustDoIt.java @@ -0,0 +1,52 @@ +package ru.gb.Dmitrieva.HomeWorkApp.Lesson_1; + +import java.util.Random; + +public class JustDoIt { + public JustDoIt() { + } + + public static void main(String[] args) { + Random rand = new Random(); + Lesson1.Actions[] actions = new Lesson1.Actions[3]; + int distance = rand.nextInt(10); + int heigth = rand.nextInt(10); + actions[0] = new Lesson1.Human("Peter", distance, heigth); + distance = rand.nextInt(10); + heigth = rand.nextInt(10); + actions[1] = new Lesson1.Robot("00XX001Z111", distance, heigth); + distance = rand.nextInt(10); + heigth = rand.nextInt(10); + actions[2] = new Lesson1.Cat("Luna", distance, heigth); + Barrier[] barriers = new Barrier[6]; + + int i; + for(i = 0; i < barriers.length; ++i) { + distance = rand.nextInt(10); + boolean isRoad = rand.nextBoolean(); + if (isRoad) { + barriers[i] = new Lesson1.Road(i + 1 + " is Road", distance); + } else { + barriers[i] = new Lesson1.Wall(i + 1 + " is Wall", distance); + } + } + + for(i = 0; i < actions.length; ++i) { + boolean result = true; + + for(int j = 0; j < barriers.length; ++j) { + result = barriers[j].moving(actions[i]); + if (!result) { + break; + } + } + + if (result) { + System.out.println("Success!!"); + } else { + System.out.println("Fail and retires from the race!!"); + } + } + + } +} diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Road.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Road.java new file mode 100644 index 0000000..4c30353 --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Road.java @@ -0,0 +1,20 @@ +package ru.gb.Dmitrieva.HomeWorkApp.Lesson_1; + +public class Road extends Barrier { + private int length; + + public Road(String name, int length) { + super(name); + this.length = length; + } + + public int getLength() { + return this.length; + } + + protected boolean moving(Lesson1.Actions actions) { + System.out.printf("The barrier number %s and it's length: %d m\n", super.getName(), this.length); + actions.run(); + return this.getLength() <= actions.getRunDistance(); + } +} diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Robot.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Robot.java new file mode 100644 index 0000000..5234acd --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Robot.java @@ -0,0 +1,29 @@ +package ru.gb.Dmitrieva.HomeWorkApp.Lesson_1; + +public class Robot implements Actions { + private String name; + private int runDistance; + private int jumpHeight; + + public Robot(String name, int distance, int jumpHeight) { + this.name = name; + this.runDistance = distance; + this.jumpHeight = jumpHeight; + } + + public void run() { + System.out.printf("Robot %s is running %d m\n", this.name, this.getRunDistance()); + } + + public void jump() { + System.out.printf("Robot %s is jumping %d m\n", this.name, this.getJumpHeight()); + } + + public int getRunDistance() { + return this.runDistance; + } + + public int getJumpHeight() { + return this.jumpHeight; + } +} \ No newline at end of file diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Wall.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Wall.java new file mode 100644 index 0000000..038f301 --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Wall.java @@ -0,0 +1,20 @@ +package ru.gb.Dmitrieva.HomeWorkApp.Lesson_1; + +public class Wall extends Barrier { + private int heigth; + + public Wall(String name, int heigth) { + super(name); + this.heigth = heigth; + } + + public int getHeigth() { + return this.heigth; + } + + protected boolean moving(Lesson1.Actions actions) { + System.out.printf("The barrier number %s and it's height: %d m\n", super.getName(), this.heigth); + actions.jump(); + return this.getHeigth() <= actions.getJumpHeight(); + } +} \ No newline at end of file From 5e69aa631cc69a06f0f5303fdacce1eac8d15c05 Mon Sep 17 00:00:00 2001 From: Ekarry <81385639+Ekarry@users.noreply.github.com> Date: Tue, 18 May 2021 22:36:17 +0300 Subject: [PATCH 2/4] =?UTF-8?q?=D0=9E=D1=82=D0=BA=D0=BE=D1=80=D1=80=D0=B5?= =?UTF-8?q?=D0=BA=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BB=D0=B0=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D0=B7=D0=B0=D0=BC=D0=B5=D1=87=D0=B0=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=D0=BC=20-=20=D0=BD=D0=B5=20=D1=81=D0=BC=D0=BE=D0=B3=D0=BB?= =?UTF-8?q?=D0=B0=20=D1=82=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=BF=D0=BE=20?= =?UTF-8?q?=D0=B1=D0=B0=D1=80=D1=8C=D0=B5=D1=80=D0=B0=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dmitrieva/HomeWorkApp/Lesson_1/Cat.java | 9 +++-- .../Dmitrieva/HomeWorkApp/Lesson_1/Human.java | 9 +++-- .../HomeWorkApp/Lesson_1/JustDoIt.java | 35 ++++++++----------- .../Dmitrieva/HomeWorkApp/Lesson_1/Road.java | 9 +++-- .../Dmitrieva/HomeWorkApp/Lesson_1/Robot.java | 9 +++-- .../Dmitrieva/HomeWorkApp/Lesson_1/Wall.java | 9 +++-- 6 files changed, 45 insertions(+), 35 deletions(-) diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Cat.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Cat.java index 618e771..d85890d 100644 --- a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Cat.java +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Cat.java @@ -1,14 +1,17 @@ package ru.gb.Dmitrieva.HomeWorkApp.Lesson_1; +import java.util.Random; + public class Cat implements Actions { private String name; private int runDistance; private int jumpHeight; - public Cat(String name, int distance, int jumpHeight) { + public Cat(String name) { this.name = name; - this.runDistance = distance; - this.jumpHeight = jumpHeight; + Random rand = new Random(); + this.runDistance = rand.nextInt(10); + this.jumpHeight = rand.nextInt(10); } public void run() { diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Human.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Human.java index 22747fe..b6ad224 100644 --- a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Human.java +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Human.java @@ -1,14 +1,17 @@ package ru.gb.Dmitrieva.HomeWorkApp.Lesson_1; +import java.util.Random; + public class Human implements Actions { private String name; private int runDistance; private int jumpHeight; - public Human(String name, int distance, int jumpHeight) { + public Human(String name) { this.name = name; - this.runDistance = distance; - this.jumpHeight = jumpHeight; + Random rand = new Random(); + this.runDistance = rand.nextInt(10); + this.jumpHeight = rand.nextInt(10); } public void run() { diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/JustDoIt.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/JustDoIt.java index 19fadb6..4244b7f 100644 --- a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/JustDoIt.java +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/JustDoIt.java @@ -3,39 +3,25 @@ import java.util.Random; public class JustDoIt { - public JustDoIt() { - } - public static void main(String[] args) { - Random rand = new Random(); - Lesson1.Actions[] actions = new Lesson1.Actions[3]; - int distance = rand.nextInt(10); - int heigth = rand.nextInt(10); - actions[0] = new Lesson1.Human("Peter", distance, heigth); - distance = rand.nextInt(10); - heigth = rand.nextInt(10); - actions[1] = new Lesson1.Robot("00XX001Z111", distance, heigth); - distance = rand.nextInt(10); - heigth = rand.nextInt(10); - actions[2] = new Lesson1.Cat("Luna", distance, heigth); - Barrier[] barriers = new Barrier[6]; + Barrier[] barriers = new Barrier[6]; int i; for(i = 0; i < barriers.length; ++i) { - distance = rand.nextInt(10); + Random rand = new Random(); boolean isRoad = rand.nextBoolean(); if (isRoad) { - barriers[i] = new Lesson1.Road(i + 1 + " is Road", distance); + barriers[i] = new Road(i + 1 + " is Road"); } else { - barriers[i] = new Lesson1.Wall(i + 1 + " is Wall", distance); + barriers[i] = new Wall(i + 1 + " is Wall"); } } - for(i = 0; i < actions.length; ++i) { + for(i = 0; i < generateParticipants().length; ++i) { boolean result = true; for(int j = 0; j < barriers.length; ++j) { - result = barriers[j].moving(actions[i]); + result = barriers[j].moving(generateParticipants()[i]); if (!result) { break; } @@ -49,4 +35,13 @@ public static void main(String[] args) { } } + + private static Actions[] generateParticipants() { + Actions [] actions = new Actions []{ + new Human("Peter"), + new Robot("00XX001Z111"), + new Cat("Luna") + }; + return actions; + } } diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Road.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Road.java index 4c30353..5473eb0 100644 --- a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Road.java +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Road.java @@ -1,18 +1,21 @@ package ru.gb.Dmitrieva.HomeWorkApp.Lesson_1; +import java.util.Random; + public class Road extends Barrier { private int length; - public Road(String name, int length) { + public Road(String name) { super(name); - this.length = length; + Random rand = new Random(); + this.length = rand.nextInt(10); } public int getLength() { return this.length; } - protected boolean moving(Lesson1.Actions actions) { + protected boolean moving(Actions actions) { System.out.printf("The barrier number %s and it's length: %d m\n", super.getName(), this.length); actions.run(); return this.getLength() <= actions.getRunDistance(); diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Robot.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Robot.java index 5234acd..7a986ff 100644 --- a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Robot.java +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Robot.java @@ -1,14 +1,17 @@ package ru.gb.Dmitrieva.HomeWorkApp.Lesson_1; +import java.util.Random; + public class Robot implements Actions { private String name; private int runDistance; private int jumpHeight; - public Robot(String name, int distance, int jumpHeight) { + public Robot(String name) { this.name = name; - this.runDistance = distance; - this.jumpHeight = jumpHeight; + Random rand = new Random(); + this.runDistance = rand.nextInt(10); + this.jumpHeight = rand.nextInt(10); } public void run() { diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Wall.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Wall.java index 038f301..aaa1af9 100644 --- a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Wall.java +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Wall.java @@ -1,18 +1,21 @@ package ru.gb.Dmitrieva.HomeWorkApp.Lesson_1; +import java.util.Random; + public class Wall extends Barrier { private int heigth; - public Wall(String name, int heigth) { + public Wall(String name) { super(name); - this.heigth = heigth; + Random rand = new Random(); + this.heigth = rand.nextInt(10); } public int getHeigth() { return this.heigth; } - protected boolean moving(Lesson1.Actions actions) { + protected boolean moving(Actions actions) { System.out.printf("The barrier number %s and it's height: %d m\n", super.getName(), this.heigth); actions.jump(); return this.getHeigth() <= actions.getJumpHeight(); From 721deae6d3af4dc3715897e0ce7dadfd78bf957e Mon Sep 17 00:00:00 2001 From: Ekarry <81385639+Ekarry@users.noreply.github.com> Date: Fri, 21 May 2021 22:59:21 +0300 Subject: [PATCH 3/4] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D1=88=D1=83=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=B8=D1=82=D1=8C=20=D0=94=D0=97?= =?UTF-8?q?2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HomeWorkApp/Lesson_2/ArrayCheck.java | 53 +++++++++++++++++++ .../Lesson_2/MyArrayDataException.java | 7 +++ .../Lesson_2/MyArraySizeException.java | 7 +++ 3 files changed, 67 insertions(+) create mode 100644 src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_2/ArrayCheck.java create mode 100644 src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_2/MyArrayDataException.java create mode 100644 src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_2/MyArraySizeException.java diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_2/ArrayCheck.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_2/ArrayCheck.java new file mode 100644 index 0000000..bc148bc --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_2/ArrayCheck.java @@ -0,0 +1,53 @@ +package ru.gb.Dmitrieva.HomeWorkApp.Lesson_2; + + +public class ArrayCheck { + public static void main(String[] args) { + String[][] strDataWrong = { + {"1", "2", "3", "4"}, + {"2", "2", "7", "3"}, + {"1", "seven", "2", "2"}, + {"2", "2", "2", "2"} + + }; + getArraySum(strDataWrong); + + String[][] strSizeWrong = { + {"1", "2", "3", "4"}, + {"2", "2", "7", "3"}, + {"1", "5", "2", "2"}, + {"2", "2", "2", "2"}, + {"2", "2", "2", "2"} + + }; + getArraySum(strSizeWrong); + + } + + private static void getArraySum(String[][] str) { + if (str.length != 4) { + throw new MyArraySizeException(); + } + for (int i = 0; i < str.length; i++) { + int sum = 0; + { + for (int j = 0; j < str[i].length; j++) { + inputDate(str[i][j], i, j); + sum += Integer.parseInt(str[i][j]); + } + } + System.out.println("Результат: "); + System.out.println(sum); + } + + + } + + private static void inputDate(String str, int x, int y) { + for (int i = 1; i < str.length(); i++) { + if (str.charAt(i) < '0' || str.charAt(i) > '9') { + throw new MyArrayDataException(x+1, y+1); + } + } + } +} diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_2/MyArrayDataException.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_2/MyArrayDataException.java new file mode 100644 index 0000000..5c96a72 --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_2/MyArrayDataException.java @@ -0,0 +1,7 @@ +package ru.gb.Dmitrieva.HomeWorkApp.Lesson_2; + +public class MyArrayDataException extends NumberFormatException { + public MyArrayDataException(int i, int j){ + super("Не удалось преобразовать элемент массива к числу. В ячейке (" + i + "," + j + ") не цифра."); + } +} diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_2/MyArraySizeException.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_2/MyArraySizeException.java new file mode 100644 index 0000000..35fe665 --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_2/MyArraySizeException.java @@ -0,0 +1,7 @@ +package ru.gb.Dmitrieva.HomeWorkApp.Lesson_2; + +public class MyArraySizeException extends ArrayIndexOutOfBoundsException { + public MyArraySizeException(){ + super("Массив не формата 4х4"); + } +} From 7083288ecee051a580abc499cabb1fd5ba1bd955 Mon Sep 17 00:00:00 2001 From: Ekarry <81385639+Ekarry@users.noreply.github.com> Date: Tue, 25 May 2021 20:56:27 +0300 Subject: [PATCH 4/4] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D1=88=D1=83=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=B8=D1=82=D1=8C=20=D0=94=D0=97?= =?UTF-8?q?3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dmitrieva/HomeWorkApp/Lesson_3/Apple.java | 7 +++ .../Dmitrieva/HomeWorkApp/Lesson_3/Box.java | 30 ++++++++++ .../Dmitrieva/HomeWorkApp/Lesson_3/Fruit.java | 9 +++ .../Dmitrieva/HomeWorkApp/Lesson_3/Main.java | 59 +++++++++++++++++++ .../HomeWorkApp/Lesson_3/Orange.java | 8 +++ 5 files changed, 113 insertions(+) create mode 100644 src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_3/Apple.java create mode 100644 src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_3/Box.java create mode 100644 src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_3/Fruit.java create mode 100644 src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_3/Main.java create mode 100644 src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_3/Orange.java diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_3/Apple.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_3/Apple.java new file mode 100644 index 0000000..173962d --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_3/Apple.java @@ -0,0 +1,7 @@ +package ru.gb.Dmitrieva.HomeWorkApp.Lesson_3; + +public class Apple extends Fruit{ + public Apple() { + weight = 1.0; + } +} diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_3/Box.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_3/Box.java new file mode 100644 index 0000000..2243159 --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_3/Box.java @@ -0,0 +1,30 @@ +package ru.gb.Dmitrieva.HomeWorkApp.Lesson_3; + +import java.util.ArrayList; + +public class Box { + private ArrayList fruitList = new ArrayList<>(); + + public void addFruit(T fruit){ + this.fruitList.add(fruit); + if (fruitList.isEmpty()); + } + + public double getWeight(){ + int count = fruitList.size(); + if (count==0) + return 0; + return fruitList.get(0).getWeight()*count; + } + + public boolean compare(Box box){ + return this.getWeight()==box.getWeight(); + } + + public void pourOverTo(Box destBox){ + for (T fruit : fruitList){ + destBox.addFruit(fruit); + } + fruitList.clear(); + } +} diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_3/Fruit.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_3/Fruit.java new file mode 100644 index 0000000..6fd80af --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_3/Fruit.java @@ -0,0 +1,9 @@ +package ru.gb.Dmitrieva.HomeWorkApp.Lesson_3; + +public abstract class Fruit { + double weight; + + public double getWeight(){ + return weight; + }; +} diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_3/Main.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_3/Main.java new file mode 100644 index 0000000..b9a636a --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_3/Main.java @@ -0,0 +1,59 @@ +package ru.gb.Dmitrieva.HomeWorkApp.Lesson_3; + +import java.util.ArrayList; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + //Создаем массив + System.out.println("Магия уровня 1"); + Integer[] arr = new Integer[]{1, 2, 3, 4, 5}; + String[] arr2 = new String[]{"a", "b", "c", "d", "e"}; + System.out.println(Arrays.toString(arr)); + swap(arr, 0, 1); + System.out.println(Arrays.toString(arr)); + + System.out.println(Arrays.toString(arr2)); + swap(arr2, 0, 4); + System.out.println(Arrays.toString(arr2)); + + //Операции с массивом + System.out.println("\nМагия уровня 2"); + ArrayList arrayList = toArrayList(arr2); + System.out.println(arrayList); + + //Оперции с коробками + System.out.println("\nМагия уровня 3"); + Box appleBox1 = new Box<>(); + Box appleBox2 = new Box<>(); + Box orangeBox1 = new Box<>(); + for (int i = 0; i < 10; i++) { + appleBox1.addFruit(new Apple()); + orangeBox1.addFruit(new Orange()); + } + for (int i = 0; i < 5; i++) { + appleBox2.addFruit(new Apple()); + } + System.out.println("Масса первой коробки с яблоками: " + appleBox1.getWeight()); + System.out.println("Масса второй коробки с яблоками: " + appleBox2.getWeight()); + System.out.println("Масса коробки с апельсинами: " + orangeBox1.getWeight()); + System.out.println("Результат сравнения 1й коробки с яблоками и коробки с апельсинами: "+appleBox1.compare(orangeBox1)); + + System.out.println("\nПересыпаем из второй коробки в первую...\n"); + appleBox2.pourOverTo(appleBox1); + System.out.println("Масса первой коробки с яблоками: " + appleBox1.getWeight()); + System.out.println("Масса второй коробки с яблоками: " + appleBox2.getWeight()); + System.out.println("Масса коробки с апельсинами: " + orangeBox1.getWeight()); + System.out.println("Результат сравнения 1й коробки с яблоками и коробки с апельсинами: "+appleBox1.compare(orangeBox1)); + } + + static void swap(Object[] arr, int num1, int num2) { + Object buf = arr[num1]; + arr[num1] = arr[num2]; + arr[num2] = buf; + } + + static ArrayList toArrayList(T[] arr) { + return new ArrayList(Arrays.asList(arr)); + } +} diff --git a/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_3/Orange.java b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_3/Orange.java new file mode 100644 index 0000000..ed86518 --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_3/Orange.java @@ -0,0 +1,8 @@ +package ru.gb.Dmitrieva.HomeWorkApp.Lesson_3; + +public class Orange extends Fruit +{ + public Orange() { + weight = 1.5; + } +}