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..d85890d --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Cat.java @@ -0,0 +1,32 @@ +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) { + this.name = name; + Random rand = new Random(); + this.runDistance = rand.nextInt(10); + this.jumpHeight = rand.nextInt(10); + } + + 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..b6ad224 --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Human.java @@ -0,0 +1,32 @@ +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) { + this.name = name; + Random rand = new Random(); + this.runDistance = rand.nextInt(10); + this.jumpHeight = rand.nextInt(10); + } + + 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..4244b7f --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/JustDoIt.java @@ -0,0 +1,47 @@ +package ru.gb.Dmitrieva.HomeWorkApp.Lesson_1; + +import java.util.Random; + +public class JustDoIt { + public static void main(String[] args) { + + Barrier[] barriers = new Barrier[6]; + int i; + for(i = 0; i < barriers.length; ++i) { + Random rand = new Random(); + boolean isRoad = rand.nextBoolean(); + if (isRoad) { + barriers[i] = new Road(i + 1 + " is Road"); + } else { + barriers[i] = new Wall(i + 1 + " is Wall"); + } + } + + for(i = 0; i < generateParticipants().length; ++i) { + boolean result = true; + + for(int j = 0; j < barriers.length; ++j) { + result = barriers[j].moving(generateParticipants()[i]); + if (!result) { + break; + } + } + + if (result) { + System.out.println("Success!!"); + } else { + System.out.println("Fail and retires from the race!!"); + } + } + + } + + 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 new file mode 100644 index 0000000..5473eb0 --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Road.java @@ -0,0 +1,23 @@ +package ru.gb.Dmitrieva.HomeWorkApp.Lesson_1; + +import java.util.Random; + +public class Road extends Barrier { + private int length; + + public Road(String name) { + super(name); + Random rand = new Random(); + this.length = rand.nextInt(10); + } + + public int getLength() { + return this.length; + } + + 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 new file mode 100644 index 0000000..7a986ff --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Robot.java @@ -0,0 +1,32 @@ +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) { + this.name = name; + Random rand = new Random(); + this.runDistance = rand.nextInt(10); + this.jumpHeight = rand.nextInt(10); + } + + 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..aaa1af9 --- /dev/null +++ b/src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_1/Wall.java @@ -0,0 +1,23 @@ +package ru.gb.Dmitrieva.HomeWorkApp.Lesson_1; + +import java.util.Random; + +public class Wall extends Barrier { + private int heigth; + + public Wall(String name) { + super(name); + Random rand = new Random(); + this.heigth = rand.nextInt(10); + } + + public int getHeigth() { + return this.heigth; + } + + 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(); + } +} \ No newline at end of file