From 30e84512f83e47196e25cf4d14d757ab22d76f9d Mon Sep 17 00:00:00 2001 From: Oleksii Vitiuk Date: Sat, 21 Dec 2024 21:53:00 +0200 Subject: [PATCH 1/2] done 1.0 --- pom.xml | 8 ++++ .../java/core/basesyntax/AbstractFigure.java | 13 +++++++ src/main/java/core/basesyntax/Circle.java | 20 ++++++++++ .../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/HelloWorld.java | 8 ---- .../core/basesyntax/IsoscelesTrapezoid.java | 25 ++++++++++++ src/main/java/core/basesyntax/MainApp.java | 16 ++++++++ src/main/java/core/basesyntax/Print.java | 5 +++ src/main/java/core/basesyntax/Rectangle.java | 23 +++++++++++ .../java/core/basesyntax/RightTriangle.java | 23 +++++++++++ src/main/java/core/basesyntax/Square.java | 20 ++++++++++ 13 files changed, 209 insertions(+), 8 deletions(-) create mode 100644 src/main/java/core/basesyntax/AbstractFigure.java 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/Figure.java create mode 100644 src/main/java/core/basesyntax/FigureSupplier.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/pom.xml b/pom.xml index 699207173a..7e47618330 100644 --- a/pom.xml +++ b/pom.xml @@ -47,6 +47,14 @@ false + + org.apache.maven.plugins + maven-compiler-plugin + + 17 + 17 + + diff --git a/src/main/java/core/basesyntax/AbstractFigure.java b/src/main/java/core/basesyntax/AbstractFigure.java new file mode 100644 index 0000000000..c28bd51aa2 --- /dev/null +++ b/src/main/java/core/basesyntax/AbstractFigure.java @@ -0,0 +1,13 @@ +package core.basesyntax; + +public abstract class AbstractFigure implements Figure,Print { + private String color; + + public AbstractFigure(String color) { + this.color = color; + } + + public String getColor() { + return color; + } +} diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java new file mode 100644 index 0000000000..0983807ebe --- /dev/null +++ b/src/main/java/core/basesyntax/Circle.java @@ -0,0 +1,20 @@ +package core.basesyntax; + +public class Circle extends AbstractFigure { + private String color; + private double radius; + + public Circle(String color, double radius) { + super(color); + this.radius = radius; + } + + public double getArea() { + return Math.PI * radius * radius; + } + + @Override + public String describe() { + return "Circle " + getColor() + " radius: " + radius + " area: " + getArea(); + } +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java new file mode 100644 index 0000000000..cdf1d3696f --- /dev/null +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -0,0 +1,12 @@ +package core.basesyntax; + +import java.util.Random; + +public class ColorSupplier { + public String getRandomColor() { + String[] colors = {"Red","Green","Blue","Yellow","Black","Purple"}; + Random random = new Random(); + String randomColor = colors[random.nextInt(colors.length)]; + return randomColor; + } +} diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java new file mode 100644 index 0000000000..c94c27ea29 --- /dev/null +++ b/src/main/java/core/basesyntax/Figure.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface Figure { + 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..079d72db28 --- /dev/null +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -0,0 +1,39 @@ +package core.basesyntax; + +import java.util.Random; + +public class FigureSupplier { + public AbstractFigure getRandomFigure() { + Random random = new Random(); + ColorSupplier colorSupplier = new ColorSupplier(); + String color = colorSupplier.getRandomColor(); + int figureType = random.nextInt(4); + switch (figureType) { + case 0: //Circle + double radius = 1 + random.nextInt(10); + return new Circle(color,radius); + case 1: //RightTriangle + double base = 1 + random.nextInt(10); + double heigth = 1 + random.nextInt(10); + return new RightTriangle(color,base,heigth); + case 2: //Square + double side = 1 + random.nextInt(10); + return new Square(color,side); + case 3: //IsoscelesTrapezoid + double side1 = 1 + random.nextInt(10); + double side2 = 1 + random.nextInt(10); + heigth = 1 + random.nextInt(10); + return new IsoscelesTrapezoid(color,side1,side2,heigth); + case 4: //Rectangle + double length = 1 + random.nextInt(10); + double width = 1 + random.nextInt(10); + return new Rectangle(color,length,width); + default: + throw new IllegalStateException("Unexpected value: " + figureType); + } + } + + 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 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..03d5a79e07 --- /dev/null +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -0,0 +1,25 @@ +package core.basesyntax; + +public class IsoscelesTrapezoid extends AbstractFigure { + private String color; + 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; + } + + public double getArea() { + return 0.5 * (base1 + base2) * height; + } + + @Override + public String describe() { + return "IsoscelesTrapezoid " + getColor() + " base1: " + base1 + " base2: " + base2 + + " height: " + height + " area: " + getArea(); + } +} diff --git a/src/main/java/core/basesyntax/MainApp.java b/src/main/java/core/basesyntax/MainApp.java new file mode 100644 index 0000000000..b30315a392 --- /dev/null +++ b/src/main/java/core/basesyntax/MainApp.java @@ -0,0 +1,16 @@ +package core.basesyntax; + +public class MainApp { + public static void main(String[] args) { + AbstractFigure[] figures = new AbstractFigure[6]; + FigureSupplier figureSupplier = new FigureSupplier(); + for (int i = 0; i < 3;i++) { + figures[i] = figureSupplier.getRandomFigure(); + System.out.println(figures[i].describe()); + } + for (int i = 3; i < 6;i++) { + figures[i] = (AbstractFigure) figureSupplier.getDefaultFigure(); + System.out.println(figures[i].describe()); + } + } +} diff --git a/src/main/java/core/basesyntax/Print.java b/src/main/java/core/basesyntax/Print.java new file mode 100644 index 0000000000..c5bd181546 --- /dev/null +++ b/src/main/java/core/basesyntax/Print.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface Print extends Figure { + String describe(); +} diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java new file mode 100644 index 0000000000..14366fb010 --- /dev/null +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -0,0 +1,23 @@ +package core.basesyntax; + +public class Rectangle extends AbstractFigure { + private String color; + private double length; + private double width; + + public Rectangle(String color, double length, double width) { + super(color); + this.length = length; + this.width = width; + } + + public double getArea() { + return length * width; + } + + @Override + public String describe() { + return "Rectangle " + getColor() + " length: " + length + " width: " + + width + " area: " + getArea(); + } +} diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java new file mode 100644 index 0000000000..83c5b6fd97 --- /dev/null +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -0,0 +1,23 @@ +package core.basesyntax; + +public class RightTriangle extends AbstractFigure { + private String color; + private double base; + private double heigth; + + public RightTriangle(String color,double base, double heigth) { + super(color); + this.base = base; + this.heigth = heigth; + } + + public double getArea() { + return 0.5 * base * heigth; + } + + @Override + public String describe() { + return "RightTriange " + getColor() + " base: " + base + " heigth: " + + heigth + " area: " + getArea(); + } +} diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java new file mode 100644 index 0000000000..d555f20e9f --- /dev/null +++ b/src/main/java/core/basesyntax/Square.java @@ -0,0 +1,20 @@ +package core.basesyntax; + +public class Square extends AbstractFigure { + private String color; + private double side; + + public Square(String color, double side) { + super(color); + this.side = side; + } + + public double getArea() { + return side * side; + } + + @Override + public String describe() { + return "Square " + getColor() + " side: " + side + " area: " + getArea(); + } +} From b44c7c11902ba4367969f7c93d611ac260776071 Mon Sep 17 00:00:00 2001 From: Oleksii Vitiuk Date: Sat, 21 Dec 2024 22:02:44 +0200 Subject: [PATCH 2/2] done 1.1 --- src/main/java/core/basesyntax/Circle.java | 1 - src/main/java/core/basesyntax/FigureSupplier.java | 2 +- src/main/java/core/basesyntax/IsoscelesTrapezoid.java | 9 ++++----- src/main/java/core/basesyntax/Rectangle.java | 1 - src/main/java/core/basesyntax/RightTriangle.java | 1 - src/main/java/core/basesyntax/Square.java | 1 - 6 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java index 0983807ebe..1d65cf77aa 100644 --- a/src/main/java/core/basesyntax/Circle.java +++ b/src/main/java/core/basesyntax/Circle.java @@ -1,7 +1,6 @@ package core.basesyntax; public class Circle extends AbstractFigure { - private String color; private double radius; public Circle(String color, double radius) { diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index 079d72db28..954ccf95d9 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -7,7 +7,7 @@ public AbstractFigure getRandomFigure() { Random random = new Random(); ColorSupplier colorSupplier = new ColorSupplier(); String color = colorSupplier.getRandomColor(); - int figureType = random.nextInt(4); + int figureType = random.nextInt(5); switch (figureType) { case 0: //Circle double radius = 1 + random.nextInt(10); diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index 03d5a79e07..ed13dc192e 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -1,25 +1,24 @@ package core.basesyntax; public class IsoscelesTrapezoid extends AbstractFigure { - private String color; private double base1; private double base2; - private double height; + private double heigth; public IsoscelesTrapezoid(String color, double base1, double base2, double height) { super(color); this.base1 = base1; this.base2 = base2; - this.height = height; + this.heigth = height; } public double getArea() { - return 0.5 * (base1 + base2) * height; + return 0.5 * (base1 + base2) * heigth; } @Override public String describe() { return "IsoscelesTrapezoid " + getColor() + " base1: " + base1 + " base2: " + base2 - + " height: " + height + " area: " + getArea(); + + " height: " + heigth + " area: " + getArea(); } } diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index 14366fb010..6db20c0ff7 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -1,7 +1,6 @@ package core.basesyntax; public class Rectangle extends AbstractFigure { - private String color; private double length; private double width; diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index 83c5b6fd97..c455695765 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -1,7 +1,6 @@ package core.basesyntax; public class RightTriangle extends AbstractFigure { - private String color; private double base; private double heigth; diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java index d555f20e9f..e23885e4c7 100644 --- a/src/main/java/core/basesyntax/Square.java +++ b/src/main/java/core/basesyntax/Square.java @@ -1,7 +1,6 @@ package core.basesyntax; public class Square extends AbstractFigure { - private String color; private double side; public Square(String color, double side) {