From 640268cf438a80aa79e7aa35d181af2df33f6497 Mon Sep 17 00:00:00 2001 From: OleksandrDarchyk Date: Mon, 11 Nov 2024 20:57:23 +0100 Subject: [PATCH 01/12] hw-1-jv-oop-advanced --- src/main/java/core/basesyntax/Circle.java | 20 +++++++++++++ .../java/core/basesyntax/ColorSupplier.java | 12 ++++++++ src/main/java/core/basesyntax/Drawable.java | 5 ++++ src/main/java/core/basesyntax/Figure.java | 13 +++++++++ .../java/core/basesyntax/FigureSupplier.java | 28 +++++++++++++++++++ src/main/java/core/basesyntax/HelloWorld.java | 2 +- .../core/basesyntax/IsoscelesTrapezoid.java | 25 +++++++++++++++++ src/main/java/core/basesyntax/Main.java | 24 ++++++++++++++++ src/main/java/core/basesyntax/Measurable.java | 5 ++++ src/main/java/core/basesyntax/Rectangle.java | 23 +++++++++++++++ .../java/core/basesyntax/RightTriangle.java | 23 +++++++++++++++ src/main/java/core/basesyntax/Square.java | 21 ++++++++++++++ 12 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 src/main/java/core/basesyntax/Circle.java create mode 100644 src/main/java/core/basesyntax/ColorSupplier.java create mode 100644 src/main/java/core/basesyntax/Drawable.java create mode 100644 src/main/java/core/basesyntax/Figure.java create mode 100644 src/main/java/core/basesyntax/FigureSupplier.java create mode 100644 src/main/java/core/basesyntax/IsoscelesTrapezoid.java create mode 100644 src/main/java/core/basesyntax/Main.java create mode 100644 src/main/java/core/basesyntax/Measurable.java create mode 100644 src/main/java/core/basesyntax/Rectangle.java create mode 100644 src/main/java/core/basesyntax/RightTriangle.java create mode 100644 src/main/java/core/basesyntax/Square.java diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java new file mode 100644 index 0000000000..4677c1da53 --- /dev/null +++ b/src/main/java/core/basesyntax/Circle.java @@ -0,0 +1,20 @@ +package core.basesyntax; + +public class Circle extends Figure { + private double radius; + public Circle(String color , double radius) { + super(color); + this.radius = radius; + } + + @Override + public void draw() { + super.draw(); + System.out.println("radius: " + radius + " units"); + } + + @Override + public double getArea() { + return Math.PI * radius * radius; + } +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java new file mode 100644 index 0000000000..b024475098 --- /dev/null +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -0,0 +1,12 @@ +package core.basesyntax; + +import java.util.Random; + +public class ColorSupplier { + private static final String[] COLORS = {"red", "green", "blue", "yellow", "black", "white"}; + private Random random = new Random(); + + public String getRandomColor() { + return COLORS[random.nextInt(COLORS.length)]; + } +} diff --git a/src/main/java/core/basesyntax/Drawable.java b/src/main/java/core/basesyntax/Drawable.java new file mode 100644 index 0000000000..d045270178 --- /dev/null +++ b/src/main/java/core/basesyntax/Drawable.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface Drawable { + void draw(); +} diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java new file mode 100644 index 0000000000..e4096b2d01 --- /dev/null +++ b/src/main/java/core/basesyntax/Figure.java @@ -0,0 +1,13 @@ +package core.basesyntax; + +public abstract class Figure implements Drawable,Measurable{ + protected String color; + public Figure(String color){ + this.color = color; + } + + @Override + public void draw() { + System.out.println("Figure: " + this.getClass().getSimpleName() + ", area: " + getArea() + " sq. units, color: " + color); + } +} diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java new file mode 100644 index 0000000000..d80b66e13d --- /dev/null +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -0,0 +1,28 @@ +package core.basesyntax; + +import java.util.Random; + +class FigureSupplier { + private Random random = new Random(); + private ColorSupplier colorSupplier = new ColorSupplier(); + + public Figure getRandomFigure() { + String color = colorSupplier.getRandomColor(); + switch (random.nextInt(5)) { + case 0: + return new Square(color, random.nextInt(10) + 1); + case 1: + return new Rectangle(color, random.nextInt(10) + 1, random.nextInt(10) + 1); + case 2: + return new RightTriangle(color, random.nextInt(10) + 1, random.nextInt(10) + 1); + case 3: + return new Circle(color, random.nextInt(10) + 1); + default: + return new IsoscelesTrapezoid(color, random.nextInt(10) + 1, random.nextInt(10) + 1, random.nextInt(10) + 1); + } + } + + public Figure getDefaultFigure() { + return new Circle("white", 10); + } +} diff --git a/src/main/java/core/basesyntax/HelloWorld.java b/src/main/java/core/basesyntax/HelloWorld.java index 97db782bf7..b3dd246ee0 100644 --- a/src/main/java/core/basesyntax/HelloWorld.java +++ b/src/main/java/core/basesyntax/HelloWorld.java @@ -3,6 +3,6 @@ /** * Feel free to remove this class and create your own. */ -public class HelloWorld { +public class HelloWorld { } diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java new file mode 100644 index 0000000000..1b299c3a8e --- /dev/null +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -0,0 +1,25 @@ +package core.basesyntax; + +public class IsoscelesTrapezoid extends Figure { + private double base1; + private double base2; + private double height; + + public IsoscelesTrapezoid(String color, double base1, double base2, double height) { + super(color); + this.base1 = base1; + this.base2 = base2; + this.height = height; + } + + @Override + public void draw() { + super.draw(); + System.out.println("base1: " + base1 + " units, base2: " + base2 + " units, height: " + height + " units"); + } + + @Override + public double getArea() { + return 0.5 * (base1 + base2) * height; + } +} diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java new file mode 100644 index 0000000000..5e87360de6 --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,24 @@ +package core.basesyntax; + +public class Main { + public static void main(String[] args) { + FigureSupplier figureSupplier = new FigureSupplier(); + Figure[] figures = new Figure[6]; + + // Generate random figures for the first half + for (int i = 0; i < figures.length / 2; i++) { + figures[i] = figureSupplier.getRandomFigure(); + } + + // Generate default figures for the second half + for (int i = figures.length / 2; i < figures.length; i++) { + figures[i] = figureSupplier.getDefaultFigure(); + } + + // Display all figures + for (Figure figure : figures) { + figure.draw(); + System.out.println(); + } + } +} diff --git a/src/main/java/core/basesyntax/Measurable.java b/src/main/java/core/basesyntax/Measurable.java new file mode 100644 index 0000000000..89b0fe3452 --- /dev/null +++ b/src/main/java/core/basesyntax/Measurable.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface Measurable { + double getArea(); +} diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java new file mode 100644 index 0000000000..6c92109a4e --- /dev/null +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -0,0 +1,23 @@ +package core.basesyntax; + +public class Rectangle extends Figure { + private double width; + private double height; + + public Rectangle(String color ,double width,double height) { + super(color); + this.height= height; + this.width = width; + } + + @Override + public void draw() { + super.draw(); + System.out.println("width: " + width + " units, height: " + height + " units"); + } + + @Override + public double getArea() { + return width * height; + } +} diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java new file mode 100644 index 0000000000..642f0952b4 --- /dev/null +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -0,0 +1,23 @@ +package core.basesyntax; + +public class RightTriangle extends Figure { + private double firstLeg; + private double secondLeg; + + public RightTriangle(String color, double firstLeg, double secondLeg) { + super(color); + this.firstLeg = firstLeg; + this.secondLeg = secondLeg; + } + + @Override + public void draw() { + super.draw(); + System.out.println("firstLeg: " + firstLeg + " units, secondLeg: " + secondLeg + " units"); + } + + @Override + public double getArea() { + return 0.5 * firstLeg * secondLeg; + } +} diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java new file mode 100644 index 0000000000..ba1db8da62 --- /dev/null +++ b/src/main/java/core/basesyntax/Square.java @@ -0,0 +1,21 @@ +package core.basesyntax; + +public class Square extends Figure { + private double side; + + public Square(String color,double side) { + super(color); + this.side=side; + } + + @Override + public void draw() { + super.draw(); + System.out.println("side: " + side + " units"); + } + + @Override + public double getArea() { + return side*side; + } +} From fb52199c5af71860d17498c0a3ef6c6dfebc3ee5 Mon Sep 17 00:00:00 2001 From: OleksandrDarchyk Date: Mon, 11 Nov 2024 21:36:19 +0100 Subject: [PATCH 02/12] hw-1-jv-oop-advanced --- src/main/java/core/basesyntax/Circle.java | 13 +++++++------ src/main/java/core/basesyntax/Figure.java | 10 +++++++--- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java index 4677c1da53..f1746d65fe 100644 --- a/src/main/java/core/basesyntax/Circle.java +++ b/src/main/java/core/basesyntax/Circle.java @@ -2,19 +2,20 @@ public class Circle extends Figure { private double radius; - public Circle(String color , double radius) { + + public Circle(String color, double radius) { super(color); this.radius = radius; } @Override - public void draw() { - super.draw(); - System.out.println("radius: " + radius + " units"); + public double getArea() { + return Math.PI * radius * radius; } @Override - public double getArea() { - return Math.PI * radius * radius; + public void draw() { + super.draw(); + System.out.println("radius: " + radius + " units"); } } diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java index e4096b2d01..3663c86842 100644 --- a/src/main/java/core/basesyntax/Figure.java +++ b/src/main/java/core/basesyntax/Figure.java @@ -1,8 +1,12 @@ package core.basesyntax; -public abstract class Figure implements Drawable,Measurable{ +import core.basesyntax.Drawable; +import core.basesyntax.Measurable; + +public abstract class Figure implements Drawable, Measurable { protected String color; - public Figure(String color){ + + public Figure(String color) { this.color = color; } @@ -10,4 +14,4 @@ public Figure(String color){ public void draw() { System.out.println("Figure: " + this.getClass().getSimpleName() + ", area: " + getArea() + " sq. units, color: " + color); } -} +} \ No newline at end of file From 77f68403cc9f3a4ed8c030ed2945be33e0c994c8 Mon Sep 17 00:00:00 2001 From: OleksandrDarchyk Date: Mon, 11 Nov 2024 21:59:18 +0100 Subject: [PATCH 03/12] hw-1-jv-oop-advanced --- src/main/java/core/basesyntax/Circle.java | 1 - .../java/core/basesyntax/FigureSupplier.java | 39 +++++++++++++------ src/main/java/core/basesyntax/Rectangle.java | 1 - .../java/core/basesyntax/RightTriangle.java | 1 - src/main/java/core/basesyntax/Square.java | 3 +- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java index f1746d65fe..9c2bd55b77 100644 --- a/src/main/java/core/basesyntax/Circle.java +++ b/src/main/java/core/basesyntax/Circle.java @@ -15,7 +15,6 @@ public double getArea() { @Override public void draw() { - super.draw(); System.out.println("radius: " + radius + " units"); } } diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index d80b66e13d..489be8066a 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -3,26 +3,41 @@ import java.util.Random; class FigureSupplier { + private static final int FIGURE_TYPES_COUNT = 5; private Random random = new Random(); private ColorSupplier colorSupplier = new ColorSupplier(); public Figure getRandomFigure() { String color = colorSupplier.getRandomColor(); - switch (random.nextInt(5)) { - case 0: - return new Square(color, random.nextInt(10) + 1); - case 1: - return new Rectangle(color, random.nextInt(10) + 1, random.nextInt(10) + 1); - case 2: - return new RightTriangle(color, random.nextInt(10) + 1, random.nextInt(10) + 1); - case 3: - return new Circle(color, random.nextInt(10) + 1); - default: - return new IsoscelesTrapezoid(color, random.nextInt(10) + 1, random.nextInt(10) + 1, random.nextInt(10) + 1); + switch (random.nextInt(FIGURE_TYPES_COUNT)) { + case 0: { + int side = random.nextInt(10) + 1; + return new Square(color, side); + } + case 1: { + int width = random.nextInt(10) + 1; + int height = random.nextInt(10) + 1; + return new Rectangle(color, width, height); + } + case 2: { + int base = random.nextInt(10) + 1; + int height = random.nextInt(10) + 1; + return new RightTriangle(color, base, height); + } + case 3: { + int radius = random.nextInt(10) + 1; + return new Circle(color, radius); + } + default: { + int base1 = random.nextInt(10) + 1; + int base2 = random.nextInt(10) + 1; + int height = random.nextInt(10) + 1; + return new IsoscelesTrapezoid(color, base1, base2, height); + } } } public Figure getDefaultFigure() { return new Circle("white", 10); } -} +} \ No newline at end of file diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index 6c92109a4e..bfb78f2b56 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -12,7 +12,6 @@ public Rectangle(String color ,double width,double height) { @Override public void draw() { - super.draw(); System.out.println("width: " + width + " units, height: " + height + " units"); } diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index 642f0952b4..f55afa6680 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -12,7 +12,6 @@ public RightTriangle(String color, double firstLeg, double secondLeg) { @Override public void draw() { - super.draw(); System.out.println("firstLeg: " + firstLeg + " units, secondLeg: " + secondLeg + " units"); } diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java index ba1db8da62..2c928d19eb 100644 --- a/src/main/java/core/basesyntax/Square.java +++ b/src/main/java/core/basesyntax/Square.java @@ -5,12 +5,11 @@ public class Square extends Figure { public Square(String color,double side) { super(color); - this.side=side; + this.side = side; } @Override public void draw() { - super.draw(); System.out.println("side: " + side + " units"); } From 21d4614786ce317dcd092a0438711b9cd454c59b Mon Sep 17 00:00:00 2001 From: OleksandrDarchyk Date: Mon, 11 Nov 2024 22:08:55 +0100 Subject: [PATCH 04/12] hw-2-oop-advanced --- src/main/java/core/basesyntax/Rectangle.java | 1 + src/main/java/core/basesyntax/RightTriangle.java | 1 + src/main/java/core/basesyntax/Square.java | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index bfb78f2b56..6c92109a4e 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -12,6 +12,7 @@ public Rectangle(String color ,double width,double height) { @Override public void draw() { + super.draw(); System.out.println("width: " + width + " units, height: " + height + " units"); } diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index f55afa6680..642f0952b4 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -12,6 +12,7 @@ public RightTriangle(String color, double firstLeg, double secondLeg) { @Override public void draw() { + super.draw(); System.out.println("firstLeg: " + firstLeg + " units, secondLeg: " + secondLeg + " units"); } diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java index 2c928d19eb..86bafc4e78 100644 --- a/src/main/java/core/basesyntax/Square.java +++ b/src/main/java/core/basesyntax/Square.java @@ -3,7 +3,7 @@ public class Square extends Figure { private double side; - public Square(String color,double side) { + public Square(String color, double side) { super(color); this.side = side; } From 50ed5af40aa1b9d1ff85f980a736b5701adb581d Mon Sep 17 00:00:00 2001 From: OleksandrDarchyk Date: Mon, 11 Nov 2024 22:13:46 +0100 Subject: [PATCH 05/12] hw-1-jv-oop-advanced - updated Rectangle, RightTriangle, and Square classes --- src/main/java/core/basesyntax/Rectangle.java | 1 - src/main/java/core/basesyntax/RightTriangle.java | 1 - 2 files changed, 2 deletions(-) diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index 6c92109a4e..bfb78f2b56 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -12,7 +12,6 @@ public Rectangle(String color ,double width,double height) { @Override public void draw() { - super.draw(); System.out.println("width: " + width + " units, height: " + height + " units"); } diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index 642f0952b4..f55afa6680 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -12,7 +12,6 @@ public RightTriangle(String color, double firstLeg, double secondLeg) { @Override public void draw() { - super.draw(); System.out.println("firstLeg: " + firstLeg + " units, secondLeg: " + secondLeg + " units"); } From 7869b7dfe7b44aa42120d542cef67cace03669a1 Mon Sep 17 00:00:00 2001 From: OleksandrDarchyk Date: Mon, 11 Nov 2024 22:16:09 +0100 Subject: [PATCH 06/12] hw-1-jv-oop-advanced - updated Rectangle, RightTriangle, and Square classes --- src/main/java/core/basesyntax/Rectangle.java | 1 + src/main/java/core/basesyntax/RightTriangle.java | 1 + 2 files changed, 2 insertions(+) diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index bfb78f2b56..6c92109a4e 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -12,6 +12,7 @@ public Rectangle(String color ,double width,double height) { @Override public void draw() { + super.draw(); System.out.println("width: " + width + " units, height: " + height + " units"); } diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index f55afa6680..642f0952b4 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -12,6 +12,7 @@ public RightTriangle(String color, double firstLeg, double secondLeg) { @Override public void draw() { + super.draw(); System.out.println("firstLeg: " + firstLeg + " units, secondLeg: " + secondLeg + " units"); } From 696a001015889283d8e85c7255f3620f584d4f5f Mon Sep 17 00:00:00 2001 From: OleksandrDarchyk Date: Mon, 11 Nov 2024 22:20:25 +0100 Subject: [PATCH 07/12] hw-1-jv-oop-advanced - updated Rectangle, RightTriangle, and Square classes --- src/main/java/core/basesyntax/HelloWorld.java | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 src/main/java/core/basesyntax/HelloWorld.java diff --git a/src/main/java/core/basesyntax/HelloWorld.java b/src/main/java/core/basesyntax/HelloWorld.java deleted file mode 100644 index b3dd246ee0..0000000000 --- a/src/main/java/core/basesyntax/HelloWorld.java +++ /dev/null @@ -1,8 +0,0 @@ -package core.basesyntax; - -/** - * Feel free to remove this class and create your own. - */ -public class HelloWorld { - -} From c9b189697037ba5577326271be0fe7c05f5326ae Mon Sep 17 00:00:00 2001 From: OleksandrDarchyk Date: Mon, 11 Nov 2024 22:31:46 +0100 Subject: [PATCH 08/12] hw-1-jv-oop-advanced - updated Rectangle, RightTriangle, and Square classes --- src/main/java/core/basesyntax/Rectangle.java | 1 - src/main/java/core/basesyntax/RightTriangle.java | 1 - src/main/java/core/basesyntax/Square.java | 3 ++- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index 6c92109a4e..bfb78f2b56 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -12,7 +12,6 @@ public Rectangle(String color ,double width,double height) { @Override public void draw() { - super.draw(); System.out.println("width: " + width + " units, height: " + height + " units"); } diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index 642f0952b4..f55afa6680 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -12,7 +12,6 @@ public RightTriangle(String color, double firstLeg, double secondLeg) { @Override public void draw() { - super.draw(); System.out.println("firstLeg: " + firstLeg + " units, secondLeg: " + secondLeg + " units"); } diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java index 86bafc4e78..b4a2849cbf 100644 --- a/src/main/java/core/basesyntax/Square.java +++ b/src/main/java/core/basesyntax/Square.java @@ -10,11 +10,12 @@ public Square(String color, double side) { @Override public void draw() { + super.draw(); System.out.println("side: " + side + " units"); } @Override public double getArea() { - return side*side; + return side * side; } } From 880f82bd352243dc4538eb5006440cac1dc20e4d Mon Sep 17 00:00:00 2001 From: OleksandrDarchyk Date: Mon, 11 Nov 2024 22:41:43 +0100 Subject: [PATCH 09/12] =?UTF-8?q?=D0=9E=D0=BD=D0=BE=D0=B2=D0=B8=D0=B2=20Re?= =?UTF-8?q?ctangle,=20RightTriangle=20=D1=82=D0=B0=20Square?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/core/basesyntax/Rectangle.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index bfb78f2b56..b4422b23f7 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -4,10 +4,10 @@ public class Rectangle extends Figure { private double width; private double height; - public Rectangle(String color ,double width,double height) { + public Rectangle(String color, double width, double height) { super(color); - this.height= height; this.width = width; + this.height = height; } @Override From f40894a321693e0c30a23e4e18c5f07df31bfeaa Mon Sep 17 00:00:00 2001 From: Oleksandr19891989 Date: Thu, 14 Nov 2024 19:46:26 +0100 Subject: [PATCH 10/12] mvn clean package --- src/main/java/core/basesyntax/Figure.java | 8 +++----- src/main/java/core/basesyntax/FigureSupplier.java | 2 +- src/main/java/core/basesyntax/IsoscelesTrapezoid.java | 3 ++- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java index 3663c86842..c8e8cd15f1 100644 --- a/src/main/java/core/basesyntax/Figure.java +++ b/src/main/java/core/basesyntax/Figure.java @@ -1,8 +1,5 @@ package core.basesyntax; -import core.basesyntax.Drawable; -import core.basesyntax.Measurable; - public abstract class Figure implements Drawable, Measurable { protected String color; @@ -12,6 +9,7 @@ public Figure(String color) { @Override public void draw() { - System.out.println("Figure: " + this.getClass().getSimpleName() + ", area: " + getArea() + " sq. units, color: " + color); + System.out.println("Figure: " + this.getClass().getSimpleName() + + ", area: " + getArea() + " sq. units, color: " + color); } -} \ No newline at end of file +} diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index 489be8066a..22e3b0abfe 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -40,4 +40,4 @@ public Figure getRandomFigure() { public Figure getDefaultFigure() { return new Circle("white", 10); } -} \ No newline at end of file +} diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index 1b299c3a8e..b89c8e2f0f 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -15,7 +15,8 @@ public IsoscelesTrapezoid(String color, double base1, double base2, double heigh @Override public void draw() { super.draw(); - System.out.println("base1: " + base1 + " units, base2: " + base2 + " units, height: " + height + " units"); + System.out.println("base1: " + base1 + " units, base2: " + + base2 + " units, height: " + height + " units"); } @Override From 7b63e17ea0222a3afc1ea5275fcbfa7bb3876af3 Mon Sep 17 00:00:00 2001 From: Oleksandr19891989 Date: Fri, 15 Nov 2024 21:02:33 +0100 Subject: [PATCH 11/12] chenges --- .../{Measurable.java => AreaCalcualtor.java} | 2 +- src/main/java/core/basesyntax/Circle.java | 4 +-- src/main/java/core/basesyntax/Color.java | 5 ++++ .../java/core/basesyntax/ColorSupplier.java | 8 +++--- src/main/java/core/basesyntax/Figure.java | 2 +- .../java/core/basesyntax/FigureSupplier.java | 26 +++++++++++-------- .../core/basesyntax/IsoscelesTrapezoid.java | 4 +-- src/main/java/core/basesyntax/Main.java | 20 +++++++------- src/main/java/core/basesyntax/Rectangle.java | 4 +-- .../java/core/basesyntax/RightTriangle.java | 4 +-- src/main/java/core/basesyntax/Square.java | 4 +-- 11 files changed, 45 insertions(+), 38 deletions(-) rename src/main/java/core/basesyntax/{Measurable.java => AreaCalcualtor.java} (59%) create mode 100644 src/main/java/core/basesyntax/Color.java diff --git a/src/main/java/core/basesyntax/Measurable.java b/src/main/java/core/basesyntax/AreaCalcualtor.java similarity index 59% rename from src/main/java/core/basesyntax/Measurable.java rename to src/main/java/core/basesyntax/AreaCalcualtor.java index 89b0fe3452..5a7872c17b 100644 --- a/src/main/java/core/basesyntax/Measurable.java +++ b/src/main/java/core/basesyntax/AreaCalcualtor.java @@ -1,5 +1,5 @@ package core.basesyntax; -public interface Measurable { +public interface AreaCalcualtor { double getArea(); } diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java index 9c2bd55b77..79679bf219 100644 --- a/src/main/java/core/basesyntax/Circle.java +++ b/src/main/java/core/basesyntax/Circle.java @@ -3,8 +3,8 @@ public class Circle extends Figure { private double radius; - public Circle(String color, double radius) { - super(color); + public Circle(Color color, double radius) { + super(String.valueOf(color)); this.radius = radius; } diff --git a/src/main/java/core/basesyntax/Color.java b/src/main/java/core/basesyntax/Color.java new file mode 100644 index 0000000000..98555e0497 --- /dev/null +++ b/src/main/java/core/basesyntax/Color.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public enum Color { + RED, GREEN, BLUE, YELLOW, BLACK, WHITE; +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java index b024475098..f76a742a77 100644 --- a/src/main/java/core/basesyntax/ColorSupplier.java +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -3,10 +3,10 @@ import java.util.Random; public class ColorSupplier { - private static final String[] COLORS = {"red", "green", "blue", "yellow", "black", "white"}; - private Random random = new Random(); + private static final Random RANDOM = new Random(); - public String getRandomColor() { - return COLORS[random.nextInt(COLORS.length)]; + public Color getRandomColor() { + Color[] colors = Color.values(); + return colors[RANDOM.nextInt(colors.length)]; } } diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java index c8e8cd15f1..203fd37634 100644 --- a/src/main/java/core/basesyntax/Figure.java +++ b/src/main/java/core/basesyntax/Figure.java @@ -1,6 +1,6 @@ package core.basesyntax; -public abstract class Figure implements Drawable, Measurable { +public abstract class Figure implements Drawable, AreaCalcualtor { protected String color; public Figure(String color) { diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index 22e3b0abfe..d4477b3425 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -4,40 +4,44 @@ class FigureSupplier { private static final int FIGURE_TYPES_COUNT = 5; + private static final int MAX_DIMENSION = 10; + private static final int MIN_DIMENSION = 1; + private static final int DEFAULT_RADIUS = 10; + private Random random = new Random(); private ColorSupplier colorSupplier = new ColorSupplier(); public Figure getRandomFigure() { - String color = colorSupplier.getRandomColor(); + Color color = colorSupplier.getRandomColor(); switch (random.nextInt(FIGURE_TYPES_COUNT)) { case 0: { - int side = random.nextInt(10) + 1; + int side = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; return new Square(color, side); } case 1: { - int width = random.nextInt(10) + 1; - int height = random.nextInt(10) + 1; + int width = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; + int height = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; return new Rectangle(color, width, height); } case 2: { - int base = random.nextInt(10) + 1; - int height = random.nextInt(10) + 1; + int base = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; + int height = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; return new RightTriangle(color, base, height); } case 3: { - int radius = random.nextInt(10) + 1; + int radius = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; return new Circle(color, radius); } default: { - int base1 = random.nextInt(10) + 1; - int base2 = random.nextInt(10) + 1; - int height = random.nextInt(10) + 1; + int base1 = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; + int base2 = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; + int height = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; return new IsoscelesTrapezoid(color, base1, base2, height); } } } public Figure getDefaultFigure() { - return new Circle("white", 10); + return new Circle(Color.WHITE, DEFAULT_RADIUS); } } diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index b89c8e2f0f..d65e6134ba 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -5,8 +5,8 @@ public class IsoscelesTrapezoid extends Figure { private double base2; private double height; - public IsoscelesTrapezoid(String color, double base1, double base2, double height) { - super(color); + public IsoscelesTrapezoid(Color color, double base1, double base2, double height) { + super(String.valueOf(color)); this.base1 = base1; this.base2 = base2; this.height = height; diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java index 5e87360de6..f49d665c00 100644 --- a/src/main/java/core/basesyntax/Main.java +++ b/src/main/java/core/basesyntax/Main.java @@ -3,22 +3,20 @@ public class Main { public static void main(String[] args) { FigureSupplier figureSupplier = new FigureSupplier(); - Figure[] figures = new Figure[6]; + final int arraySize = (int) (Math.random() * 10) + 1; + Figure[] figures = new Figure[arraySize]; - // Generate random figures for the first half - for (int i = 0; i < figures.length / 2; i++) { - figures[i] = figureSupplier.getRandomFigure(); + for (int i = 0; i < figures.length; i++) { + if (i < figures.length / 2) { + figures[i] = figureSupplier.getRandomFigure(); + } else { + figures[i] = figureSupplier.getDefaultFigure(); + } } - // Generate default figures for the second half - for (int i = figures.length / 2; i < figures.length; i++) { - figures[i] = figureSupplier.getDefaultFigure(); - } - - // Display all figures for (Figure figure : figures) { figure.draw(); - System.out.println(); } } } + diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index b4422b23f7..cd6a244cb0 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -4,8 +4,8 @@ public class Rectangle extends Figure { private double width; private double height; - public Rectangle(String color, double width, double height) { - super(color); + public Rectangle(Color color, double width, double height) { + super(String.valueOf(color)); this.width = width; this.height = height; } diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index f55afa6680..030f716620 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -4,8 +4,8 @@ public class RightTriangle extends Figure { private double firstLeg; private double secondLeg; - public RightTriangle(String color, double firstLeg, double secondLeg) { - super(color); + public RightTriangle(Color color, double firstLeg, double secondLeg) { + super(String.valueOf(color)); this.firstLeg = firstLeg; this.secondLeg = secondLeg; } diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java index b4a2849cbf..8ec2d7ea64 100644 --- a/src/main/java/core/basesyntax/Square.java +++ b/src/main/java/core/basesyntax/Square.java @@ -3,8 +3,8 @@ public class Square extends Figure { private double side; - public Square(String color, double side) { - super(color); + public Square(Color color, double side) { + super(String.valueOf(color)); this.side = side; } From c09aec348191f8a2a953a478b7b6c7e7e426d955 Mon Sep 17 00:00:00 2001 From: Oleksandr19891989 Date: Tue, 19 Nov 2024 11:35:15 +0100 Subject: [PATCH 12/12] chenges --- src/main/java/core/basesyntax/Circle.java | 2 +- .../java/core/basesyntax/ColorSupplier.java | 4 +-- .../java/core/basesyntax/FigureSupplier.java | 26 +++++++++---------- .../core/basesyntax/IsoscelesTrapezoid.java | 18 ++++++------- src/main/java/core/basesyntax/Rectangle.java | 2 +- .../java/core/basesyntax/RightTriangle.java | 2 +- src/main/java/core/basesyntax/Square.java | 2 +- 7 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java index 79679bf219..8f7dc1bd88 100644 --- a/src/main/java/core/basesyntax/Circle.java +++ b/src/main/java/core/basesyntax/Circle.java @@ -4,7 +4,7 @@ public class Circle extends Figure { private double radius; public Circle(Color color, double radius) { - super(String.valueOf(color)); + super(color.name()); this.radius = radius; } diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java index f76a742a77..46eb2c7769 100644 --- a/src/main/java/core/basesyntax/ColorSupplier.java +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -5,8 +5,8 @@ public class ColorSupplier { private static final Random RANDOM = new Random(); - public Color getRandomColor() { + public String getRandomColor() { Color[] colors = Color.values(); - return colors[RANDOM.nextInt(colors.length)]; + return colors[RANDOM.nextInt(colors.length)].toString(); } } diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index d4477b3425..263c6bd8b4 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -12,31 +12,31 @@ class FigureSupplier { private ColorSupplier colorSupplier = new ColorSupplier(); public Figure getRandomFigure() { - Color color = colorSupplier.getRandomColor(); + String color = colorSupplier.getRandomColor(); switch (random.nextInt(FIGURE_TYPES_COUNT)) { case 0: { int side = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; - return new Square(color, side); + return new Square(Color.valueOf(color), side); } case 1: { - int width = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; - int height = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; - return new Rectangle(color, width, height); + int firstLeg = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; + int secondLeg = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; + return new Rectangle(Color.valueOf(color), firstLeg, secondLeg); } case 2: { - int base = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; - int height = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; - return new RightTriangle(color, base, height); + int firstLeg = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; + int secondLeg = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; + return new RightTriangle(Color.valueOf(color), firstLeg, secondLeg); } case 3: { int radius = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; - return new Circle(color, radius); + return new Circle(Color.valueOf(color), radius); } default: { - int base1 = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; - int base2 = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; - int height = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; - return new IsoscelesTrapezoid(color, base1, base2, height); + int topBase = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; + int bottomBase = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; + int secondLeg = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; + return new IsoscelesTrapezoid(Color.valueOf(color), topBase, bottomBase, secondLeg); } } } diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index d65e6134ba..db32f87b1c 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -1,26 +1,26 @@ package core.basesyntax; public class IsoscelesTrapezoid extends Figure { - private double base1; - private double base2; + private double topBase; + private double bottomBase; private double height; - public IsoscelesTrapezoid(Color color, double base1, double base2, double height) { - super(String.valueOf(color)); - this.base1 = base1; - this.base2 = base2; + public IsoscelesTrapezoid(Color color, double topBase, double bottomBase, double height) { + super(color.name()); + this.topBase = topBase; + this.bottomBase = bottomBase; this.height = height; } @Override public void draw() { super.draw(); - System.out.println("base1: " + base1 + " units, base2: " - + base2 + " units, height: " + height + " units"); + System.out.println("topBase: " + topBase + " units, bottomBase: " + + bottomBase + " units, height: " + height + " units"); } @Override public double getArea() { - return 0.5 * (base1 + base2) * height; + return 0.5 * (topBase + bottomBase) * height; } } diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index cd6a244cb0..4c5e6eec58 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -5,7 +5,7 @@ public class Rectangle extends Figure { private double height; public Rectangle(Color color, double width, double height) { - super(String.valueOf(color)); + super(color.name()); this.width = width; this.height = height; } diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index 030f716620..951032c3ad 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -5,7 +5,7 @@ public class RightTriangle extends Figure { private double secondLeg; public RightTriangle(Color color, double firstLeg, double secondLeg) { - super(String.valueOf(color)); + super(color.name()); this.firstLeg = firstLeg; this.secondLeg = secondLeg; } diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java index 8ec2d7ea64..53d981eaa7 100644 --- a/src/main/java/core/basesyntax/Square.java +++ b/src/main/java/core/basesyntax/Square.java @@ -4,7 +4,7 @@ public class Square extends Figure { private double side; public Square(Color color, double side) { - super(String.valueOf(color)); + super(color.name()); this.side = side; }