From dcaeb3da3f46cd3eb34046e88250b4d53fd34260 Mon Sep 17 00:00:00 2001 From: Myself Date: Sun, 1 Oct 2023 18:01:36 +0300 Subject: [PATCH 1/5] Final version --- src/main/java/core/basesyntax/Circle.java | 24 ++++++++++++ src/main/java/core/basesyntax/Color.java | 11 ++++++ .../java/core/basesyntax/ColorSupplier.java | 12 ++++++ src/main/java/core/basesyntax/Figure.java | 5 +++ .../java/core/basesyntax/FigureSupplier.java | 39 +++++++++++++++++++ src/main/java/core/basesyntax/GetArea.java | 5 +++ src/main/java/core/basesyntax/HelloWorld.java | 8 ---- .../core/basesyntax/IsoscelesTrapezoid.java | 27 +++++++++++++ src/main/java/core/basesyntax/MainApp.java | 22 +++++++++++ src/main/java/core/basesyntax/Print.java | 5 +++ src/main/java/core/basesyntax/Rectangle.java | 27 +++++++++++++ .../java/core/basesyntax/RightTriangle.java | 26 +++++++++++++ src/main/java/core/basesyntax/Square.java | 24 ++++++++++++ 13 files changed, 227 insertions(+), 8 deletions(-) create mode 100644 src/main/java/core/basesyntax/Circle.java create mode 100644 src/main/java/core/basesyntax/Color.java create mode 100644 src/main/java/core/basesyntax/ColorSupplier.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/GetArea.java delete mode 100644 src/main/java/core/basesyntax/HelloWorld.java create mode 100644 src/main/java/core/basesyntax/IsoscelesTrapezoid.java create mode 100644 src/main/java/core/basesyntax/MainApp.java create mode 100644 src/main/java/core/basesyntax/Print.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..cd51c8e394 --- /dev/null +++ b/src/main/java/core/basesyntax/Circle.java @@ -0,0 +1,24 @@ +package core.basesyntax; + +public class Circle extends Figure { + private Color color; + private double radius; + + public Circle(Color color, double radius) { + this.color = color; + this.radius = radius; + } + + @Override + public double getArea() { + return Math.PI * radius * radius; + } + + @Override + public void print() { + System.out.println("Figure: Circle " + + " area " + getArea() + " sq.units," + + " side " + radius + " units" + + " color " + color); + } +} diff --git a/src/main/java/core/basesyntax/Color.java b/src/main/java/core/basesyntax/Color.java new file mode 100644 index 0000000000..6f3e01b780 --- /dev/null +++ b/src/main/java/core/basesyntax/Color.java @@ -0,0 +1,11 @@ +package core.basesyntax; + +public enum Color { + RED, + BLUE, + GREEN, + YELLOW, + WHITE, + BLACK, + VELVET; +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java new file mode 100644 index 0000000000..a85911c25b --- /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 Random random = new Random(); + + public Color getRandomColor() { + int index = random.nextInt(Color.values().length); + return Color.values()[index]; + } +} diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java new file mode 100644 index 0000000000..b7d8034577 --- /dev/null +++ b/src/main/java/core/basesyntax/Figure.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public abstract class Figure implements GetArea, Print { + private 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..404125aaf7 --- /dev/null +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -0,0 +1,39 @@ +package core.basesyntax; + +import java.util.Random; + +public class FigureSupplier { + static final int FIGURE_AMOUNT = 5; + static final int FIGURE_PARAMETERS_MAX = 20; + private Random random = new Random(); + private ColorSupplier colorSupplier = new ColorSupplier(); + + public Figure getRandomFigure() { + int randomFigureNumber = random.nextInt(FIGURE_AMOUNT + 1); + int randomFigureParameter = random.nextInt(FIGURE_PARAMETERS_MAX + 1); + switch (randomFigureNumber) { + case 0: { + return new Circle(colorSupplier.getRandomColor(), randomFigureParameter); + } + case 1: { + return new Square(colorSupplier.getRandomColor(), randomFigureParameter); + } + case 2: { + return new Rectangle(colorSupplier.getRandomColor(), + randomFigureParameter, randomFigureParameter); + } + case 3: { + return new RightTriangle(colorSupplier.getRandomColor(), + randomFigureParameter, randomFigureParameter); + } + default: { + return new IsoscelesTrapezoid(colorSupplier.getRandomColor(), + randomFigureParameter, randomFigureParameter); + } + } + } + + public Figure getDefaultFigure() { + return new Circle(Color.WHITE, 10); + } +} diff --git a/src/main/java/core/basesyntax/GetArea.java b/src/main/java/core/basesyntax/GetArea.java new file mode 100644 index 0000000000..82349a9e29 --- /dev/null +++ b/src/main/java/core/basesyntax/GetArea.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface GetArea { + double getArea(); +} 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 new file mode 100644 index 0000000000..23f6ad19b5 --- /dev/null +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -0,0 +1,27 @@ +package core.basesyntax; + +public class IsoscelesTrapezoid extends Figure { + private Color color; + private int side; + private int height; + + public IsoscelesTrapezoid(Color color, int side, int height) { + this.color = color; + this.side = side; + this.height = height; + } + + @Override + public double getArea() { + return ((side + side) / 2) * height; + } + + @Override + public void print() { + System.out.println("Figure: Square " + + " area " + getArea() + " sq.units," + + " side " + side + " units," + + " height " + height + " units," + + " color " + color); + } +} diff --git a/src/main/java/core/basesyntax/MainApp.java b/src/main/java/core/basesyntax/MainApp.java new file mode 100644 index 0000000000..3b10abf4ee --- /dev/null +++ b/src/main/java/core/basesyntax/MainApp.java @@ -0,0 +1,22 @@ +package core.basesyntax; + +public class MainApp { + public static final int FIGURES_AMOUNT = 6; + + public static void main(String[] args) { + FigureSupplier figureSupplier = new FigureSupplier(); + Figure[] figures = new Figure[FIGURES_AMOUNT]; + + for (int i = 0; i < figures.length; i++) { + if (i < FIGURES_AMOUNT / 2) { + figures[i] = figureSupplier.getRandomFigure(); + } else { + figures[i] = figureSupplier.getDefaultFigure(); + } + } + for (Figure figure : figures) { + figure.getArea(); + figure.print(); + } + } +} diff --git a/src/main/java/core/basesyntax/Print.java b/src/main/java/core/basesyntax/Print.java new file mode 100644 index 0000000000..e1964671cf --- /dev/null +++ b/src/main/java/core/basesyntax/Print.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface Print { + void print(); +} diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java new file mode 100644 index 0000000000..2b538485d4 --- /dev/null +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -0,0 +1,27 @@ +package core.basesyntax; + +public class Rectangle extends Figure { + private Color color; + private int height; + private int width; + + public Rectangle(Color color, int height, int width) { + this.color = color; + this.height = height; + this.width = width; + } + + @Override + public double getArea() { + return height * width; + } + + @Override + public void print() { + System.out.println("Figure: Square " + + " area " + getArea() + " sq.units," + + " heigth " + height + " units" + + " width " + width + " units" + + " color " + color); + } +} diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java new file mode 100644 index 0000000000..41a6c9d276 --- /dev/null +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -0,0 +1,26 @@ +package core.basesyntax; + +public class RightTriangle extends Figure { + private Color color; + private int side; + private int height; + + public RightTriangle(Color color, int side, int height) { + this.color = color; + this.side = side; + } + + @Override + public double getArea() { + return (side * height) / 2; + } + + @Override + public void print() { + System.out.println("Figure: Circle " + + " area " + getArea() + " sq.units," + + " side " + side + " units" + + " height " + height + " units" + + " color " + color); + } +} diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java new file mode 100644 index 0000000000..f8ce2767f7 --- /dev/null +++ b/src/main/java/core/basesyntax/Square.java @@ -0,0 +1,24 @@ +package core.basesyntax; + +public class Square extends Figure { + private int side; + private Color color; + + public Square(Color color, int side) { + this.color = color; + this.side = side; + } + + @Override + public double getArea() { + return side * side; + } + + @Override + public void print() { + System.out.println("Figure: Square " + + " area " + getArea() + " sq.units," + + " side " + side + " units" + + " color " + color); + } +} From b77655afb1a17a56b496da08128010b32d1f20d8 Mon Sep 17 00:00:00 2001 From: Myself Date: Mon, 2 Oct 2023 13:32:08 +0300 Subject: [PATCH 2/5] integer division fixed, MainApp changed --- src/main/java/core/basesyntax/FigureSupplier.java | 4 +++- src/main/java/core/basesyntax/IsoscelesTrapezoid.java | 4 +++- src/main/java/core/basesyntax/MainApp.java | 1 - src/main/java/core/basesyntax/RightTriangle.java | 4 +++- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index 404125aaf7..9825bbddad 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -5,6 +5,8 @@ public class FigureSupplier { static final int FIGURE_AMOUNT = 5; static final int FIGURE_PARAMETERS_MAX = 20; + static final Color DEFAULT_COLOR = Color.WHITE; + static final int DEFAULT_RADIUS = 10; private Random random = new Random(); private ColorSupplier colorSupplier = new ColorSupplier(); @@ -34,6 +36,6 @@ public Figure getRandomFigure() { } public Figure getDefaultFigure() { - return new Circle(Color.WHITE, 10); + return new Circle(DEFAULT_COLOR, DEFAULT_RADIUS); } } diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index 23f6ad19b5..a966791db7 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -13,7 +13,9 @@ public IsoscelesTrapezoid(Color color, int side, int height) { @Override public double getArea() { - return ((side + side) / 2) * height; + double sideD = side; + double heightD = height; + return ((sideD + sideD) / 2d) * heightD; } @Override diff --git a/src/main/java/core/basesyntax/MainApp.java b/src/main/java/core/basesyntax/MainApp.java index 3b10abf4ee..335631c2ae 100644 --- a/src/main/java/core/basesyntax/MainApp.java +++ b/src/main/java/core/basesyntax/MainApp.java @@ -15,7 +15,6 @@ public static void main(String[] args) { } } for (Figure figure : figures) { - figure.getArea(); figure.print(); } } diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index 41a6c9d276..eb003947b6 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -12,7 +12,9 @@ public RightTriangle(Color color, int side, int height) { @Override public double getArea() { - return (side * height) / 2; + double sideD = side; + double heightD = height; + return (sideD * heightD) / 2d; } @Override From fae84d51c420964b75657836a3b86b8f9794766f Mon Sep 17 00:00:00 2001 From: Myself Date: Tue, 3 Oct 2023 13:09:30 +0300 Subject: [PATCH 3/5] Changes were made --- src/main/java/core/basesyntax/AreaCalculatable.java | 5 +++++ src/main/java/core/basesyntax/Circle.java | 6 +++--- src/main/java/core/basesyntax/ColorSupplier.java | 3 ++- src/main/java/core/basesyntax/Drawable.java | 5 +++++ src/main/java/core/basesyntax/Figure.java | 2 +- src/main/java/core/basesyntax/GetArea.java | 5 ----- src/main/java/core/basesyntax/IsoscelesTrapezoid.java | 10 ++++------ src/main/java/core/basesyntax/MainApp.java | 2 +- src/main/java/core/basesyntax/Print.java | 5 ----- src/main/java/core/basesyntax/Rectangle.java | 6 +++--- src/main/java/core/basesyntax/RightTriangle.java | 10 ++++------ src/main/java/core/basesyntax/Square.java | 6 +++--- 12 files changed, 31 insertions(+), 34 deletions(-) create mode 100644 src/main/java/core/basesyntax/AreaCalculatable.java create mode 100644 src/main/java/core/basesyntax/Drawable.java delete mode 100644 src/main/java/core/basesyntax/GetArea.java delete mode 100644 src/main/java/core/basesyntax/Print.java diff --git a/src/main/java/core/basesyntax/AreaCalculatable.java b/src/main/java/core/basesyntax/AreaCalculatable.java new file mode 100644 index 0000000000..d5368c270c --- /dev/null +++ b/src/main/java/core/basesyntax/AreaCalculatable.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface AreaCalculatable { + double calculateArea(); +} diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java index cd51c8e394..8e641e13b5 100644 --- a/src/main/java/core/basesyntax/Circle.java +++ b/src/main/java/core/basesyntax/Circle.java @@ -10,14 +10,14 @@ public Circle(Color color, double radius) { } @Override - public double getArea() { + public double calculateArea() { return Math.PI * radius * radius; } @Override - public void print() { + public void draw() { System.out.println("Figure: Circle " - + " area " + getArea() + " sq.units," + + " area " + calculateArea() + " sq.units," + " side " + radius + " units" + " color " + color); } diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java index a85911c25b..fa59f4c016 100644 --- a/src/main/java/core/basesyntax/ColorSupplier.java +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -3,10 +3,11 @@ import java.util.Random; public class ColorSupplier { + private static final Color[] COLORS = Color.values(); private Random random = new Random(); public Color getRandomColor() { int index = random.nextInt(Color.values().length); - return Color.values()[index]; + return COLORS[index]; } } 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 index b7d8034577..b4d7dd690b 100644 --- a/src/main/java/core/basesyntax/Figure.java +++ b/src/main/java/core/basesyntax/Figure.java @@ -1,5 +1,5 @@ package core.basesyntax; -public abstract class Figure implements GetArea, Print { +public abstract class Figure implements AreaCalculatable, Drawable { private Color color; } diff --git a/src/main/java/core/basesyntax/GetArea.java b/src/main/java/core/basesyntax/GetArea.java deleted file mode 100644 index 82349a9e29..0000000000 --- a/src/main/java/core/basesyntax/GetArea.java +++ /dev/null @@ -1,5 +0,0 @@ -package core.basesyntax; - -public interface GetArea { - double getArea(); -} diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index a966791db7..1ccb7caf33 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -12,16 +12,14 @@ public IsoscelesTrapezoid(Color color, int side, int height) { } @Override - public double getArea() { - double sideD = side; - double heightD = height; - return ((sideD + sideD) / 2d) * heightD; + public double calculateArea() { + return ((side + side) * 0.5) * height; } @Override - public void print() { + public void draw() { System.out.println("Figure: Square " - + " area " + getArea() + " sq.units," + + " area " + calculateArea() + " sq.units," + " side " + side + " units," + " height " + height + " units," + " color " + color); diff --git a/src/main/java/core/basesyntax/MainApp.java b/src/main/java/core/basesyntax/MainApp.java index 335631c2ae..a2c6d0c728 100644 --- a/src/main/java/core/basesyntax/MainApp.java +++ b/src/main/java/core/basesyntax/MainApp.java @@ -15,7 +15,7 @@ public static void main(String[] args) { } } for (Figure figure : figures) { - figure.print(); + figure.draw(); } } } diff --git a/src/main/java/core/basesyntax/Print.java b/src/main/java/core/basesyntax/Print.java deleted file mode 100644 index e1964671cf..0000000000 --- a/src/main/java/core/basesyntax/Print.java +++ /dev/null @@ -1,5 +0,0 @@ -package core.basesyntax; - -public interface Print { - void print(); -} diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index 2b538485d4..4ce6f08a1c 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -12,14 +12,14 @@ public Rectangle(Color color, int height, int width) { } @Override - public double getArea() { + public double calculateArea() { return height * width; } @Override - public void print() { + public void draw() { System.out.println("Figure: Square " - + " area " + getArea() + " sq.units," + + " area " + calculateArea() + " sq.units," + " heigth " + height + " units" + " width " + width + " units" + " color " + color); diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index eb003947b6..794e1ba583 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -11,16 +11,14 @@ public RightTriangle(Color color, int side, int height) { } @Override - public double getArea() { - double sideD = side; - double heightD = height; - return (sideD * heightD) / 2d; + public double calculateArea() { + return (side * height) * 0.5; } @Override - public void print() { + public void draw() { System.out.println("Figure: Circle " - + " area " + getArea() + " sq.units," + + " area " + calculateArea() + " sq.units," + " side " + side + " units" + " height " + height + " units" + " color " + color); diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java index f8ce2767f7..9310e48585 100644 --- a/src/main/java/core/basesyntax/Square.java +++ b/src/main/java/core/basesyntax/Square.java @@ -10,14 +10,14 @@ public Square(Color color, int side) { } @Override - public double getArea() { + public double calculateArea() { return side * side; } @Override - public void print() { + public void draw() { System.out.println("Figure: Square " - + " area " + getArea() + " sq.units," + + " area " + calculateArea() + " sq.units," + " side " + side + " units" + " color " + color); } From 979e9ace21c3e6afac20092aacbe01b8cf87f614 Mon Sep 17 00:00:00 2001 From: Myself Date: Tue, 3 Oct 2023 13:37:46 +0300 Subject: [PATCH 4/5] Changes were made 2 --- src/main/java/core/basesyntax/Circle.java | 5 ++--- src/main/java/core/basesyntax/Figure.java | 8 ++++++++ src/main/java/core/basesyntax/IsoscelesTrapezoid.java | 5 ++--- src/main/java/core/basesyntax/Rectangle.java | 5 ++--- src/main/java/core/basesyntax/RightTriangle.java | 6 +++--- src/main/java/core/basesyntax/Square.java | 5 ++--- 6 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java index 8e641e13b5..3fdb84faba 100644 --- a/src/main/java/core/basesyntax/Circle.java +++ b/src/main/java/core/basesyntax/Circle.java @@ -1,11 +1,10 @@ package core.basesyntax; public class Circle extends Figure { - private Color color; private double radius; public Circle(Color color, double radius) { - this.color = color; + super(color); this.radius = radius; } @@ -19,6 +18,6 @@ public void draw() { System.out.println("Figure: Circle " + " area " + calculateArea() + " sq.units," + " side " + radius + " units" - + " color " + color); + + " color " + getColor()); } } diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java index b4d7dd690b..907909a277 100644 --- a/src/main/java/core/basesyntax/Figure.java +++ b/src/main/java/core/basesyntax/Figure.java @@ -2,4 +2,12 @@ public abstract class Figure implements AreaCalculatable, Drawable { private Color color; + + public Figure(Color color) { + this.color = color; + } + + public Color getColor() { + return color; + } } diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index 1ccb7caf33..d81b91ce0c 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -1,12 +1,11 @@ package core.basesyntax; public class IsoscelesTrapezoid extends Figure { - private Color color; private int side; private int height; public IsoscelesTrapezoid(Color color, int side, int height) { - this.color = color; + super(color); this.side = side; this.height = height; } @@ -22,6 +21,6 @@ public void draw() { + " area " + calculateArea() + " sq.units," + " side " + side + " units," + " height " + height + " units," - + " color " + color); + + " color " + getColor()); } } diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index 4ce6f08a1c..3d390b26d4 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -1,12 +1,11 @@ package core.basesyntax; public class Rectangle extends Figure { - private Color color; private int height; private int width; public Rectangle(Color color, int height, int width) { - this.color = color; + super(color); this.height = height; this.width = width; } @@ -22,6 +21,6 @@ public void draw() { + " area " + calculateArea() + " sq.units," + " heigth " + height + " units" + " width " + width + " units" - + " color " + color); + + " color " + getColor()); } } diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index 794e1ba583..6711283db8 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -1,13 +1,13 @@ package core.basesyntax; public class RightTriangle extends Figure { - private Color color; private int side; private int height; public RightTriangle(Color color, int side, int height) { - this.color = color; + super(color); this.side = side; + this.height = height; } @Override @@ -21,6 +21,6 @@ public void draw() { + " area " + calculateArea() + " sq.units," + " side " + side + " units" + " height " + height + " units" - + " color " + color); + + " color " + getColor()); } } diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java index 9310e48585..f32f69dc06 100644 --- a/src/main/java/core/basesyntax/Square.java +++ b/src/main/java/core/basesyntax/Square.java @@ -2,10 +2,9 @@ public class Square extends Figure { private int side; - private Color color; public Square(Color color, int side) { - this.color = color; + super(color); this.side = side; } @@ -19,6 +18,6 @@ public void draw() { System.out.println("Figure: Square " + " area " + calculateArea() + " sq.units," + " side " + side + " units" - + " color " + color); + + " color " + getColor()); } } From 2a2920d98ad4ba97d3e4e9d653703f57a01273d2 Mon Sep 17 00:00:00 2001 From: Myself Date: Tue, 3 Oct 2023 19:35:25 +0300 Subject: [PATCH 5/5] riddiculus mistakes were fixed --- .../java/core/basesyntax/ColorSupplier.java | 2 +- .../java/core/basesyntax/FigureSupplier.java | 27 ++++++++++--------- .../core/basesyntax/IsoscelesTrapezoid.java | 15 ++++++----- src/main/java/core/basesyntax/Rectangle.java | 2 +- .../java/core/basesyntax/RightTriangle.java | 2 +- 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java index fa59f4c016..cf3dee3e73 100644 --- a/src/main/java/core/basesyntax/ColorSupplier.java +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -7,7 +7,7 @@ public class ColorSupplier { private Random random = new Random(); public Color getRandomColor() { - int index = random.nextInt(Color.values().length); + int index = random.nextInt(COLORS.length); return COLORS[index]; } } diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index 9825bbddad..258a68ee01 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -3,34 +3,37 @@ import java.util.Random; public class FigureSupplier { - static final int FIGURE_AMOUNT = 5; - static final int FIGURE_PARAMETERS_MAX = 20; - static final Color DEFAULT_COLOR = Color.WHITE; - static final int DEFAULT_RADIUS = 10; + private static final int FIGURE_AMOUNT = 5; + private static final int FIGURE_PARAMETERS_MAX = 20; + private static final Color DEFAULT_COLOR = Color.WHITE; + private static final int DEFAULT_RADIUS = 10; private Random random = new Random(); private ColorSupplier colorSupplier = new ColorSupplier(); public Figure getRandomFigure() { + Color randomColor = colorSupplier.getRandomColor(); int randomFigureNumber = random.nextInt(FIGURE_AMOUNT + 1); int randomFigureParameter = random.nextInt(FIGURE_PARAMETERS_MAX + 1); + int randomFigureHeight = random.nextInt(FIGURE_PARAMETERS_MAX + 1); + int randomTrapezoidBase = random.nextInt(FIGURE_PARAMETERS_MAX + 1); switch (randomFigureNumber) { case 0: { - return new Circle(colorSupplier.getRandomColor(), randomFigureParameter); + return new Circle(randomColor, randomFigureParameter); } case 1: { - return new Square(colorSupplier.getRandomColor(), randomFigureParameter); + return new Square(randomColor, randomFigureParameter); } case 2: { - return new Rectangle(colorSupplier.getRandomColor(), - randomFigureParameter, randomFigureParameter); + return new Rectangle(randomColor,randomFigureHeight, + randomFigureParameter); } case 3: { - return new RightTriangle(colorSupplier.getRandomColor(), - randomFigureParameter, randomFigureParameter); + return new RightTriangle(randomColor, + randomFigureParameter, randomFigureHeight); } default: { - return new IsoscelesTrapezoid(colorSupplier.getRandomColor(), - randomFigureParameter, randomFigureParameter); + return new IsoscelesTrapezoid(randomColor, + randomTrapezoidBase, randomFigureParameter, randomFigureHeight); } } } diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index d81b91ce0c..83801c8a53 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -1,25 +1,28 @@ package core.basesyntax; public class IsoscelesTrapezoid extends Figure { - private int side; + private int topBase; + private int bottomBase; private int height; - public IsoscelesTrapezoid(Color color, int side, int height) { + public IsoscelesTrapezoid(Color color, int topBase, int bottomBase, int height) { super(color); - this.side = side; + this.topBase = topBase; + this.bottomBase = bottomBase; this.height = height; } @Override public double calculateArea() { - return ((side + side) * 0.5) * height; + return ((topBase + bottomBase) * 0.5) * height; } @Override public void draw() { - System.out.println("Figure: Square " + System.out.println("Figure: IsoscelesTrapezoid " + " area " + calculateArea() + " sq.units," - + " side " + side + " units," + + " topBase " + topBase + " units," + + "bottomBase" + bottomBase + " units" + " height " + height + " units," + " color " + getColor()); } diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index 3d390b26d4..5044f544c8 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -17,7 +17,7 @@ public double calculateArea() { @Override public void draw() { - System.out.println("Figure: Square " + System.out.println("Figure: Rectangle " + " area " + calculateArea() + " sq.units," + " heigth " + height + " units" + " width " + width + " units" diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index 6711283db8..89158a0031 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -17,7 +17,7 @@ public double calculateArea() { @Override public void draw() { - System.out.println("Figure: Circle " + System.out.println("Figure: RightTriangle " + " area " + calculateArea() + " sq.units," + " side " + side + " units" + " height " + height + " units"