From 3b1bc78c80f1b17ade335592f5b288ef1330e254 Mon Sep 17 00:00:00 2001 From: Marek Koscielewski Date: Thu, 14 Dec 2023 09:25:35 +0100 Subject: [PATCH 01/18] Abstract Class Figure implementation --- src/main/java/core/basesyntax/Figure.java | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/main/java/core/basesyntax/Figure.java diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java new file mode 100644 index 0000000000..f2fbaf70b9 --- /dev/null +++ b/src/main/java/core/basesyntax/Figure.java @@ -0,0 +1,26 @@ +package core.basesyntax; + +public abstract class Figure { + private double area; + private String color; + + public abstract void draw(); + + public double getArea() { + return area; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public abstract void calculateArea(); + + public void setArea(double area) { + this.area = area; + } +} From c2393e486a398773af0356903c79ccfc6b668296 Mon Sep 17 00:00:00 2001 From: Marek Koscielewski Date: Thu, 14 Dec 2023 09:38:38 +0100 Subject: [PATCH 02/18] Class Square implementation --- src/main/java/core/basesyntax/Square.java | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/main/java/core/basesyntax/Square.java diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java new file mode 100644 index 0000000000..8d2c0e20a1 --- /dev/null +++ b/src/main/java/core/basesyntax/Square.java @@ -0,0 +1,26 @@ +package core.basesyntax; + +public class Square extends Figure { + private double side; + + public void setSide(double side) { + this.side = side; + } + + public double getSide() { + return side; + } + + @Override + public void calculateArea() { + this.setArea(side * side); + } + + @Override + public void draw() { + System.out.println("Figure: " + this.getClass() + + ", area: " + this.getArea() + " sq. units," + + " side: " + this.side + " units, " + + "color: " + this.getColor()); + } +} From 1acf6593f43f1ca7bd8dee71550b8f64c77fe5a3 Mon Sep 17 00:00:00 2001 From: Marek Koscielewski Date: Thu, 14 Dec 2023 09:49:54 +0100 Subject: [PATCH 03/18] Class Rectangle implementation --- src/main/java/core/basesyntax/Rectangle.java | 36 ++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/java/core/basesyntax/Rectangle.java diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java new file mode 100644 index 0000000000..60a3b8cd9a --- /dev/null +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -0,0 +1,36 @@ +package core.basesyntax; + +public class Rectangle extends Figure { + private double firstSide; + private double secondSide; + + @Override + public void calculateArea() { + this.setArea(firstSide * secondSide); + } + + @Override + public void draw() { + System.out.println("Figure: " + this.getClass() + + ", area: " + this.getArea() + " sq. units," + + " first side: " + this.firstSide + " units, " + + " second side: " + this.secondSide + " units, " + + "color: " + this.getColor()); + } + + public double getFirstSide() { + return firstSide; + } + + public double getSecondSide() { + return secondSide; + } + + public void setFirstSide(double firstSide) { + this.firstSide = firstSide; + } + + public void setSecondSide(double secondSide) { + this.secondSide = secondSide; + } +} From adf91dfac840d1852c59b921a1defe0480347f37 Mon Sep 17 00:00:00 2001 From: Marek Koscielewski Date: Thu, 14 Dec 2023 09:54:52 +0100 Subject: [PATCH 04/18] Class Circle implementation --- src/main/java/core/basesyntax/Circle.java | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/main/java/core/basesyntax/Circle.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..bd09b627a7 --- /dev/null +++ b/src/main/java/core/basesyntax/Circle.java @@ -0,0 +1,26 @@ +package core.basesyntax; + +public class Circle extends Figure { + private double radius; + + public double getRadius() { + return radius; + } + + public void setRadius(double radius) { + this.radius = radius; + } + + @Override + public void calculateArea() { + this.setArea(radius * radius * Math.PI); + } + + @Override + public void draw() { + System.out.println("Figure: " + this.getClass() + + ", area: " + this.getArea() + " sq. units," + + " radius: " + this.radius + " units, " + + "color: " + this.getColor()); + } +} From d8e046304525a3cf0e2c334326fae5c2f2ee76d2 Mon Sep 17 00:00:00 2001 From: Marek Koscielewski Date: Thu, 14 Dec 2023 10:06:14 +0100 Subject: [PATCH 05/18] Class RightTriangle Implementation --- .../java/core/basesyntax/RightTriangle.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/java/core/basesyntax/RightTriangle.java diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java new file mode 100644 index 0000000000..68709efad0 --- /dev/null +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -0,0 +1,36 @@ +package core.basesyntax; + +public class RightTriangle extends Figure { + private double firstLeg; + private double secondLeg; + + public double getFirstLeg() { + return firstLeg; + } + + public double getSecondLeg() { + return secondLeg; + } + + public void setFirstLeg(double firstLeg) { + this.firstLeg = firstLeg; + } + + public void setSecondLeg(double seconLeg) { + this.secondLeg = seconLeg; + } + + @Override + public void draw() { + System.out.println("Figure: " + this.getClass() + + ", area: " + this.getArea() + " sq. units," + + " first leg: " + this.firstLeg + " units, " + + " second leg: " + this.secondLeg + " units, " + + "color: " + this.getColor()); + } + + @Override + public void calculateArea() { + this.setArea(firstLeg * secondLeg * 0.5); + } +} From 4ee9f0e3e624b247afefebb85691fdb65cdc19cd Mon Sep 17 00:00:00 2001 From: Marek Koscielewski Date: Thu, 14 Dec 2023 10:13:58 +0100 Subject: [PATCH 06/18] Class Isosceles Trapezoid implementation --- .../core/basesyntax/IsoscelesTrapezoid.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/main/java/core/basesyntax/IsoscelesTrapezoid.java diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java new file mode 100644 index 0000000000..4514bdac3d --- /dev/null +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -0,0 +1,46 @@ +package core.basesyntax; + +public class IsoscelesTrapezoid extends Figure { + private double height; + private double firstBase; + private double secondBase; + + public double getHeight() { + return height; + } + + public void setHeight(double height) { + this.height = height; + } + + public double getFirstBase() { + return firstBase; + } + + public double getSecondBase() { + return secondBase; + } + + public void setFirstBase(double firstBase) { + this.firstBase = firstBase; + } + + public void setSecondBase(double secondBase) { + this.secondBase = secondBase; + } + + @Override + public void draw() { + System.out.println("Figure: " + this.getClass() + + ", area: " + this.getArea() + " sq. units," + + " first base: " + this.firstBase + " units, " + + " second base: " + this.secondBase + " units, " + + " height: " + this.height + " units, " + + "color: " + this.getColor()); + } + + @Override + public void calculateArea() { + this.setArea((firstBase * secondBase) * 0.5 * height); + } +} From f20107056692583e0b5d54b7697002791f95b7a2 Mon Sep 17 00:00:00 2001 From: Marek Koscielewski Date: Thu, 14 Dec 2023 10:36:41 +0100 Subject: [PATCH 07/18] ENUM Colors implementation --- src/main/java/core/basesyntax/Colors.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/main/java/core/basesyntax/Colors.java diff --git a/src/main/java/core/basesyntax/Colors.java b/src/main/java/core/basesyntax/Colors.java new file mode 100644 index 0000000000..b3ffad179e --- /dev/null +++ b/src/main/java/core/basesyntax/Colors.java @@ -0,0 +1,15 @@ +package core.basesyntax; + +public enum Colors { + RED, + BLUE, + GREEN, + YELLOW, + ORANGE, + BROWN, + GREY, + BLACK, + WHITE, + PURPLE, + PINK, +} From 8b783ac5284f936322e0e02db500b2c1dd8d0454 Mon Sep 17 00:00:00 2001 From: Marek Koscielewski Date: Thu, 14 Dec 2023 10:45:47 +0100 Subject: [PATCH 08/18] Class ColorSupplier implementation --- src/main/java/core/basesyntax/ColorSupplier.java | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/main/java/core/basesyntax/ColorSupplier.java diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java new file mode 100644 index 0000000000..4b7eddd5c2 --- /dev/null +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -0,0 +1,11 @@ +package core.basesyntax; + +import java.util.Random; + +public class ColorSupplier { + public void getRandomColor() { + Random random = new Random(); + Colors[] colors = Colors.values(); + int randomIndex = random.nextInt(colors.length); + } +} From 0a4e048cd32eb3243c3482776d0b03d8fd99f0a9 Mon Sep 17 00:00:00 2001 From: Marek Koscielewski Date: Thu, 14 Dec 2023 11:06:10 +0100 Subject: [PATCH 09/18] Class FigureSupplier Added and Added Konstuctors for each Class that extends from Figure --- src/main/java/core/basesyntax/Circle.java | 5 +++++ src/main/java/core/basesyntax/Figure.java | 4 ++++ src/main/java/core/basesyntax/FigureSupplier.java | 7 +++++++ src/main/java/core/basesyntax/IsoscelesTrapezoid.java | 7 +++++++ src/main/java/core/basesyntax/Rectangle.java | 6 ++++++ src/main/java/core/basesyntax/RightTriangle.java | 6 ++++++ src/main/java/core/basesyntax/Square.java | 5 +++++ 7 files changed, 40 insertions(+) create mode 100644 src/main/java/core/basesyntax/FigureSupplier.java diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java index bd09b627a7..69cfabab0b 100644 --- a/src/main/java/core/basesyntax/Circle.java +++ b/src/main/java/core/basesyntax/Circle.java @@ -3,6 +3,11 @@ public class Circle extends Figure { private double radius; + public Circle(String color, double radius) { + super(color); + this.radius = radius; + } + public double getRadius() { return radius; } diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java index f2fbaf70b9..d93c2b1ac7 100644 --- a/src/main/java/core/basesyntax/Figure.java +++ b/src/main/java/core/basesyntax/Figure.java @@ -4,6 +4,10 @@ public abstract class Figure { private double area; private String color; + public Figure(String color) { + this.color = color; + } + public abstract void draw(); public double getArea() { diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java new file mode 100644 index 0000000000..1bc8373a21 --- /dev/null +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -0,0 +1,7 @@ +package core.basesyntax; + +public class FigureSupplier { + public Figure getDefaultFigure() { + return new Circle(Colors.WHITE.name(), 10); + } +} diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index 4514bdac3d..e4f0a89ddc 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -5,6 +5,13 @@ public class IsoscelesTrapezoid extends Figure { private double firstBase; private double secondBase; + public IsoscelesTrapezoid(String color, double height, double firstBase, double secondBase) { + super(color); + this.height = height; + this.firstBase = firstBase; + this.secondBase = secondBase; + } + public double getHeight() { return height; } diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index 60a3b8cd9a..52eb507888 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -4,6 +4,12 @@ public class Rectangle extends Figure { private double firstSide; private double secondSide; + public Rectangle(String color, double firstSide, double secondSide) { + super(color); + this.firstSide = firstSide; + this.secondSide = secondSide; + } + @Override public void calculateArea() { this.setArea(firstSide * secondSide); diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index 68709efad0..9e0dcb0497 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -4,6 +4,12 @@ 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; + } + public double getFirstLeg() { return firstLeg; } diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java index 8d2c0e20a1..2abe33d57d 100644 --- a/src/main/java/core/basesyntax/Square.java +++ b/src/main/java/core/basesyntax/Square.java @@ -3,6 +3,11 @@ public class Square extends Figure { private double side; + public Square(String color, double side) { + super(color); + this.side = side; + } + public void setSide(double side) { this.side = side; } From 471fbcc79b31b7373385b36c38d53adc56c41764 Mon Sep 17 00:00:00 2001 From: Marek Koscielewski Date: Thu, 14 Dec 2023 11:49:25 +0100 Subject: [PATCH 10/18] Methods for random and default figures added --- .../basesyntax/{Colors.java => Color.java} | 2 +- .../java/core/basesyntax/ColorSupplier.java | 7 +++--- .../java/core/basesyntax/FigureSupplier.java | 22 ++++++++++++++++++- 3 files changed, 26 insertions(+), 5 deletions(-) rename src/main/java/core/basesyntax/{Colors.java => Color.java} (80%) diff --git a/src/main/java/core/basesyntax/Colors.java b/src/main/java/core/basesyntax/Color.java similarity index 80% rename from src/main/java/core/basesyntax/Colors.java rename to src/main/java/core/basesyntax/Color.java index b3ffad179e..2092734ca2 100644 --- a/src/main/java/core/basesyntax/Colors.java +++ b/src/main/java/core/basesyntax/Color.java @@ -1,6 +1,6 @@ package core.basesyntax; -public enum Colors { +public enum Color { RED, BLUE, GREEN, diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java index 4b7eddd5c2..e257590b29 100644 --- a/src/main/java/core/basesyntax/ColorSupplier.java +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -3,9 +3,10 @@ import java.util.Random; public class ColorSupplier { - public void getRandomColor() { + public static String getRandomColor() { Random random = new Random(); - Colors[] colors = Colors.values(); - int randomIndex = random.nextInt(colors.length); + Color[] color = Color.values(); + int randomIndex = random.nextInt(color.length); + return color[randomIndex].name(); } } diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index 1bc8373a21..94dced0213 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -1,7 +1,27 @@ package core.basesyntax; +import java.util.Random; + public class FigureSupplier { + static final int figuresNumber = 5; + static final double defaultRadius = 10; + public Figure getDefaultFigure() { - return new Circle(Colors.WHITE.name(), 10); + return new Circle(Color.WHITE.name(), defaultRadius); + } + + public Figure getRandomFigure() { + Random random = new Random(); + int randomFigure = random.nextInt(figuresNumber); + return switch (randomFigure) { + case 0 -> new Square(ColorSupplier.getRandomColor(), random.nextDouble()); + case 1 -> new Rectangle(ColorSupplier.getRandomColor(), random.nextDouble(), + random.nextDouble()); + case 2 -> new Circle(ColorSupplier.getRandomColor(), random.nextDouble()); + case 3 -> new RightTriangle(ColorSupplier.getRandomColor(), random.nextDouble(), + random.nextDouble()); + default -> new IsoscelesTrapezoid(ColorSupplier.getRandomColor(), random.nextDouble(), + random.nextDouble(), random.nextDouble()); + }; } } From 21120f4bb51447c9c273fabaceb9ba89eddeb353 Mon Sep 17 00:00:00 2001 From: Marek Koscielewski Date: Thu, 14 Dec 2023 12:33:54 +0100 Subject: [PATCH 11/18] Added Interface Draw and CalculateArea, subtracted coresponding methods from Class Figure --- .../java/core/basesyntax/CalculateArea.java | 5 +++++ src/main/java/core/basesyntax/Draw.java | 6 ++++++ src/main/java/core/basesyntax/Figure.java | 14 +++++--------- .../java/core/basesyntax/FigureSupplier.java | 4 ++-- src/main/java/core/basesyntax/HelloWorld.java | 8 -------- .../core/basesyntax/IsoscelesTrapezoid.java | 1 + src/main/java/core/basesyntax/Main.java | 18 ++++++++++++++++++ 7 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 src/main/java/core/basesyntax/CalculateArea.java create mode 100644 src/main/java/core/basesyntax/Draw.java delete mode 100644 src/main/java/core/basesyntax/HelloWorld.java create mode 100644 src/main/java/core/basesyntax/Main.java diff --git a/src/main/java/core/basesyntax/CalculateArea.java b/src/main/java/core/basesyntax/CalculateArea.java new file mode 100644 index 0000000000..63f4e4fbbd --- /dev/null +++ b/src/main/java/core/basesyntax/CalculateArea.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface CalculateArea { + void calculateArea(); +} diff --git a/src/main/java/core/basesyntax/Draw.java b/src/main/java/core/basesyntax/Draw.java new file mode 100644 index 0000000000..f6bbfc8bda --- /dev/null +++ b/src/main/java/core/basesyntax/Draw.java @@ -0,0 +1,6 @@ +package core.basesyntax; + +public interface Draw { + void draw(); + +} diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java index d93c2b1ac7..6ecb84ee59 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 { +public abstract class Figure implements Draw, CalculateArea { private double area; private String color; @@ -8,12 +8,6 @@ public Figure(String color) { this.color = color; } - public abstract void draw(); - - public double getArea() { - return area; - } - public String getColor() { return color; } @@ -22,9 +16,11 @@ public void setColor(String color) { this.color = color; } - public abstract void calculateArea(); - public void setArea(double area) { this.area = area; } + + public double getArea() { + return area; + } } diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index 94dced0213..8971fc8ec1 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -6,11 +6,11 @@ public class FigureSupplier { static final int figuresNumber = 5; static final double defaultRadius = 10; - public Figure getDefaultFigure() { + public static Figure getDefaultFigure() { return new Circle(Color.WHITE.name(), defaultRadius); } - public Figure getRandomFigure() { + public static Figure getRandomFigure() { Random random = new Random(); int randomFigure = random.nextInt(figuresNumber); return switch (randomFigure) { diff --git a/src/main/java/core/basesyntax/HelloWorld.java b/src/main/java/core/basesyntax/HelloWorld.java deleted file mode 100644 index 97db782bf7..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 { - -} diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index e4f0a89ddc..08df9cf306 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -50,4 +50,5 @@ public void draw() { public void calculateArea() { this.setArea((firstBase * secondBase) * 0.5 * 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..d289f007e7 --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,18 @@ +package core.basesyntax; + +public class Main { + public static void main(String[] args) { + final int size = 6; + Figure[] figureArray = new Figure[size]; + for (int i = 0; i < size; i++) { + if (i < size / 2) { + figureArray[i] = FigureSupplier.getRandomFigure(); + } else { + figureArray[i] = FigureSupplier.getDefaultFigure(); + } + } + for (int j = 0; j < size; j++) { + figureArray[j].draw(); + } + } +} From 6c85381bae4bb8df645dc45273f5048cb2099340 Mon Sep 17 00:00:00 2001 From: Marek Koscielewski Date: Fri, 15 Dec 2023 11:58:40 +0100 Subject: [PATCH 12/18] Changed Interface names --- .../basesyntax/{CalculateArea.java => AreaCalculator.java} | 2 +- src/main/java/core/basesyntax/{Draw.java => Drawable.java} | 3 +-- src/main/java/core/basesyntax/Figure.java | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) rename src/main/java/core/basesyntax/{CalculateArea.java => AreaCalculator.java} (58%) rename src/main/java/core/basesyntax/{Draw.java => Drawable.java} (57%) diff --git a/src/main/java/core/basesyntax/CalculateArea.java b/src/main/java/core/basesyntax/AreaCalculator.java similarity index 58% rename from src/main/java/core/basesyntax/CalculateArea.java rename to src/main/java/core/basesyntax/AreaCalculator.java index 63f4e4fbbd..20235bd6e0 100644 --- a/src/main/java/core/basesyntax/CalculateArea.java +++ b/src/main/java/core/basesyntax/AreaCalculator.java @@ -1,5 +1,5 @@ package core.basesyntax; -public interface CalculateArea { +public interface AreaCalculator { void calculateArea(); } diff --git a/src/main/java/core/basesyntax/Draw.java b/src/main/java/core/basesyntax/Drawable.java similarity index 57% rename from src/main/java/core/basesyntax/Draw.java rename to src/main/java/core/basesyntax/Drawable.java index f6bbfc8bda..13fe470ec9 100644 --- a/src/main/java/core/basesyntax/Draw.java +++ b/src/main/java/core/basesyntax/Drawable.java @@ -1,6 +1,5 @@ package core.basesyntax; -public interface Draw { +public interface Drawable { void draw(); - } diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java index 6ecb84ee59..cd8f3c99a0 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 Draw, CalculateArea { +public abstract class Figure implements Drawable, AreaCalculator { private double area; private String color; From d72386f6c2c6bb13cab5953b547422b5afce0446 Mon Sep 17 00:00:00 2001 From: Marek Koscielewski Date: Fri, 15 Dec 2023 12:04:11 +0100 Subject: [PATCH 13/18] Changed clalculateArea from void to double --- src/main/java/core/basesyntax/AreaCalculator.java | 2 +- src/main/java/core/basesyntax/Circle.java | 4 ++-- src/main/java/core/basesyntax/IsoscelesTrapezoid.java | 4 ++-- src/main/java/core/basesyntax/Rectangle.java | 4 ++-- src/main/java/core/basesyntax/RightTriangle.java | 4 ++-- src/main/java/core/basesyntax/Square.java | 4 ++-- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/core/basesyntax/AreaCalculator.java b/src/main/java/core/basesyntax/AreaCalculator.java index 20235bd6e0..5767af4c39 100644 --- a/src/main/java/core/basesyntax/AreaCalculator.java +++ b/src/main/java/core/basesyntax/AreaCalculator.java @@ -1,5 +1,5 @@ package core.basesyntax; public interface AreaCalculator { - void calculateArea(); + double calculateArea(); } diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java index 69cfabab0b..56fba49daf 100644 --- a/src/main/java/core/basesyntax/Circle.java +++ b/src/main/java/core/basesyntax/Circle.java @@ -17,8 +17,8 @@ public void setRadius(double radius) { } @Override - public void calculateArea() { - this.setArea(radius * radius * Math.PI); + public double calculateArea() { + return (radius * radius * Math.PI); } @Override diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index 08df9cf306..0e59bdc9ea 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -47,8 +47,8 @@ public void draw() { } @Override - public void calculateArea() { - this.setArea((firstBase * secondBase) * 0.5 * height); + public double calculateArea() { + return ((firstBase * secondBase) * 0.5 * height); } } diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index 52eb507888..274eb59bde 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -11,8 +11,8 @@ public Rectangle(String color, double firstSide, double secondSide) { } @Override - public void calculateArea() { - this.setArea(firstSide * secondSide); + public double calculateArea() { + return (firstSide * secondSide); } @Override diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index 9e0dcb0497..6ec717bea7 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -36,7 +36,7 @@ public void draw() { } @Override - public void calculateArea() { - this.setArea(firstLeg * secondLeg * 0.5); + public double calculateArea() { + return (firstLeg * secondLeg * 0.5); } } diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java index 2abe33d57d..c5c055af67 100644 --- a/src/main/java/core/basesyntax/Square.java +++ b/src/main/java/core/basesyntax/Square.java @@ -17,8 +17,8 @@ public double getSide() { } @Override - public void calculateArea() { - this.setArea(side * side); + public double calculateArea() { + return(side * side); } @Override From 3215a844e3457fd707b9385a006f2654bb711187 Mon Sep 17 00:00:00 2001 From: Marek Koscielewski Date: Fri, 15 Dec 2023 12:05:45 +0100 Subject: [PATCH 14/18] Deleted area value from Figure CLass --- src/main/java/core/basesyntax/Figure.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java index cd8f3c99a0..f3a739223a 100644 --- a/src/main/java/core/basesyntax/Figure.java +++ b/src/main/java/core/basesyntax/Figure.java @@ -1,7 +1,6 @@ package core.basesyntax; public abstract class Figure implements Drawable, AreaCalculator { - private double area; private String color; public Figure(String color) { @@ -15,12 +14,4 @@ public String getColor() { public void setColor(String color) { this.color = color; } - - public void setArea(double area) { - this.area = area; - } - - public double getArea() { - return area; - } } From 8be18d52eb5d34890516b04b76c72d6d49513fa7 Mon Sep 17 00:00:00 2001 From: Marek Koscielewski Date: Fri, 15 Dec 2023 12:16:12 +0100 Subject: [PATCH 15/18] getRandomColor changed from static to nonstatic added class fields for Color/FigureSupplier --- src/main/java/core/basesyntax/ColorSupplier.java | 4 ++-- src/main/java/core/basesyntax/FigureSupplier.java | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java index e257590b29..5a554372e5 100644 --- a/src/main/java/core/basesyntax/ColorSupplier.java +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -3,8 +3,8 @@ import java.util.Random; public class ColorSupplier { - public static String getRandomColor() { - Random random = new Random(); + static Random random = new Random(); + public String getRandomColor() { Color[] color = Color.values(); int randomIndex = random.nextInt(color.length); return color[randomIndex].name(); diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index 8971fc8ec1..bc391d1415 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -5,22 +5,24 @@ public class FigureSupplier { static final int figuresNumber = 5; static final double defaultRadius = 10; + static Random random = new Random(); + + static ColorSupplier colorSupplier = new ColorSupplier(); public static Figure getDefaultFigure() { return new Circle(Color.WHITE.name(), defaultRadius); } public static Figure getRandomFigure() { - Random random = new Random(); int randomFigure = random.nextInt(figuresNumber); return switch (randomFigure) { - case 0 -> new Square(ColorSupplier.getRandomColor(), random.nextDouble()); - case 1 -> new Rectangle(ColorSupplier.getRandomColor(), random.nextDouble(), + case 0 -> new Square(colorSupplier.getRandomColor(), random.nextDouble()); + case 1 -> new Rectangle(colorSupplier.getRandomColor(), random.nextDouble(), random.nextDouble()); - case 2 -> new Circle(ColorSupplier.getRandomColor(), random.nextDouble()); - case 3 -> new RightTriangle(ColorSupplier.getRandomColor(), random.nextDouble(), + case 2 -> new Circle(colorSupplier.getRandomColor(), random.nextDouble()); + case 3 -> new RightTriangle(colorSupplier.getRandomColor(), random.nextDouble(), random.nextDouble()); - default -> new IsoscelesTrapezoid(ColorSupplier.getRandomColor(), random.nextDouble(), + default -> new IsoscelesTrapezoid(colorSupplier.getRandomColor(), random.nextDouble(), random.nextDouble(), random.nextDouble()); }; } From 96dd21f2d2eccf8e00a76243aa00580e999d1e17 Mon Sep 17 00:00:00 2001 From: Marek Koscielewski Date: Fri, 15 Dec 2023 12:18:23 +0100 Subject: [PATCH 16/18] Constants Names changed to FIGURES_NUMBER and DEFAULT_RADIUS --- src/main/java/core/basesyntax/FigureSupplier.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index bc391d1415..a36acf84b8 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -3,18 +3,18 @@ import java.util.Random; public class FigureSupplier { - static final int figuresNumber = 5; - static final double defaultRadius = 10; + static final int FIGURES_NUMBER = 5; + static final double DEFAULT_RADIUS = 10; static Random random = new Random(); static ColorSupplier colorSupplier = new ColorSupplier(); public static Figure getDefaultFigure() { - return new Circle(Color.WHITE.name(), defaultRadius); + return new Circle(Color.WHITE.name(), DEFAULT_RADIUS); } public static Figure getRandomFigure() { - int randomFigure = random.nextInt(figuresNumber); + int randomFigure = random.nextInt(FIGURES_NUMBER); return switch (randomFigure) { case 0 -> new Square(colorSupplier.getRandomColor(), random.nextDouble()); case 1 -> new Rectangle(colorSupplier.getRandomColor(), random.nextDouble(), From 19d17920cf612405339bcef8a68980ab04d9ca8b Mon Sep 17 00:00:00 2001 From: Marek Koscielewski Date: Fri, 15 Dec 2023 12:23:32 +0100 Subject: [PATCH 17/18] Changed static methods in Class FigureSupplier to nonstatic --- src/main/java/core/basesyntax/FigureSupplier.java | 8 ++++---- src/main/java/core/basesyntax/Main.java | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index a36acf84b8..7e1db93b29 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -3,17 +3,17 @@ import java.util.Random; public class FigureSupplier { - static final int FIGURES_NUMBER = 5; - static final double DEFAULT_RADIUS = 10; + private static final int FIGURES_NUMBER = 5; + private static final double DEFAULT_RADIUS = 10; static Random random = new Random(); static ColorSupplier colorSupplier = new ColorSupplier(); - public static Figure getDefaultFigure() { + public Figure getDefaultFigure() { return new Circle(Color.WHITE.name(), DEFAULT_RADIUS); } - public static Figure getRandomFigure() { + public Figure getRandomFigure() { int randomFigure = random.nextInt(FIGURES_NUMBER); return switch (randomFigure) { case 0 -> new Square(colorSupplier.getRandomColor(), random.nextDouble()); diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java index d289f007e7..c56552732d 100644 --- a/src/main/java/core/basesyntax/Main.java +++ b/src/main/java/core/basesyntax/Main.java @@ -4,11 +4,12 @@ public class Main { public static void main(String[] args) { final int size = 6; Figure[] figureArray = new Figure[size]; + FigureSupplier figureSupplier =new FigureSupplier(); for (int i = 0; i < size; i++) { if (i < size / 2) { - figureArray[i] = FigureSupplier.getRandomFigure(); + figureArray[i] = figureSupplier.getRandomFigure(); } else { - figureArray[i] = FigureSupplier.getDefaultFigure(); + figureArray[i] = figureSupplier.getDefaultFigure(); } } for (int j = 0; j < size; j++) { From c6027266b21c0ecc910586b6d16b1f33bea61bf6 Mon Sep 17 00:00:00 2001 From: Marek Koscielewski Date: Fri, 15 Dec 2023 12:33:56 +0100 Subject: [PATCH 18/18] Changed from this.getArea() to this.calculateArea() in every figure CLass , added class object for FigureSupplier in Main CLass --- src/main/java/core/basesyntax/Circle.java | 2 +- src/main/java/core/basesyntax/ColorSupplier.java | 3 ++- src/main/java/core/basesyntax/FigureSupplier.java | 4 ++-- src/main/java/core/basesyntax/IsoscelesTrapezoid.java | 2 +- src/main/java/core/basesyntax/Main.java | 2 +- src/main/java/core/basesyntax/Rectangle.java | 2 +- src/main/java/core/basesyntax/RightTriangle.java | 2 +- src/main/java/core/basesyntax/Square.java | 4 ++-- 8 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java index 56fba49daf..b9702ce5fb 100644 --- a/src/main/java/core/basesyntax/Circle.java +++ b/src/main/java/core/basesyntax/Circle.java @@ -24,7 +24,7 @@ public double calculateArea() { @Override public void draw() { System.out.println("Figure: " + this.getClass() - + ", area: " + this.getArea() + " sq. units," + + ", area: " + this.calculateArea() + " sq. units," + " radius: " + this.radius + " units, " + "color: " + this.getColor()); } diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java index 5a554372e5..25068049be 100644 --- a/src/main/java/core/basesyntax/ColorSupplier.java +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -3,7 +3,8 @@ import java.util.Random; public class ColorSupplier { - static Random random = new Random(); + private static Random random = new Random(); + public String getRandomColor() { Color[] color = Color.values(); int randomIndex = random.nextInt(color.length); diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index 7e1db93b29..5425d93bc6 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -5,9 +5,9 @@ public class FigureSupplier { private static final int FIGURES_NUMBER = 5; private static final double DEFAULT_RADIUS = 10; - static Random random = new Random(); + private static Random random = new Random(); - static ColorSupplier colorSupplier = new ColorSupplier(); + private static ColorSupplier colorSupplier = new ColorSupplier(); public Figure getDefaultFigure() { return new Circle(Color.WHITE.name(), DEFAULT_RADIUS); diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index 0e59bdc9ea..cb7f2de564 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -39,7 +39,7 @@ public void setSecondBase(double secondBase) { @Override public void draw() { System.out.println("Figure: " + this.getClass() - + ", area: " + this.getArea() + " sq. units," + + ", area: " + this.calculateArea() + " sq. units," + " first base: " + this.firstBase + " units, " + " second base: " + this.secondBase + " units, " + " height: " + this.height + " units, " diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java index c56552732d..8ab39edd25 100644 --- a/src/main/java/core/basesyntax/Main.java +++ b/src/main/java/core/basesyntax/Main.java @@ -4,7 +4,7 @@ public class Main { public static void main(String[] args) { final int size = 6; Figure[] figureArray = new Figure[size]; - FigureSupplier figureSupplier =new FigureSupplier(); + FigureSupplier figureSupplier = new FigureSupplier(); for (int i = 0; i < size; i++) { if (i < size / 2) { figureArray[i] = figureSupplier.getRandomFigure(); diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index 274eb59bde..35c9a8bd42 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -18,7 +18,7 @@ public double calculateArea() { @Override public void draw() { System.out.println("Figure: " + this.getClass() - + ", area: " + this.getArea() + " sq. units," + + ", area: " + this.calculateArea() + " sq. units," + " first side: " + this.firstSide + " units, " + " second side: " + this.secondSide + " units, " + "color: " + this.getColor()); diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index 6ec717bea7..29ac9d9f9e 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -29,7 +29,7 @@ public void setSecondLeg(double seconLeg) { @Override public void draw() { System.out.println("Figure: " + this.getClass() - + ", area: " + this.getArea() + " sq. units," + + ", area: " + this.calculateArea() + " sq. units," + " first leg: " + this.firstLeg + " units, " + " second leg: " + this.secondLeg + " units, " + "color: " + this.getColor()); diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java index c5c055af67..cb242edbf8 100644 --- a/src/main/java/core/basesyntax/Square.java +++ b/src/main/java/core/basesyntax/Square.java @@ -18,13 +18,13 @@ public double getSide() { @Override public double calculateArea() { - return(side * side); + return (side * side); } @Override public void draw() { System.out.println("Figure: " + this.getClass() - + ", area: " + this.getArea() + " sq. units," + + ", area: " + this.calculateArea() + " sq. units," + " side: " + this.side + " units, " + "color: " + this.getColor()); }