Skip to content

Commit

Permalink
Added interface CalculateArea.java, class Circle.java, class ColorSup…
Browse files Browse the repository at this point in the history
…plier.java, interface Colors.java, interface Drawable.java, class Figure.java, class FigureSupplier.java, interface Figures.java, class IsoscelesTrapezoid.java, class Rectangle.java, class RightTriangle.java, class Square.java; Changed HelloWorld.java
  • Loading branch information
Danila2006 committed Nov 11, 2024
1 parent 3b03b22 commit 34e82cb
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/CalculateArea.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

public interface CalculateArea {
double getS();
}
20 changes: 20 additions & 0 deletions src/main/java/core/basesyntax/Circle.java
Original file line number Diff line number Diff line change
@@ -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;
}

public double getS() {
return 3.14 * Math.pow(this.radius, 2);
}

@Override
public void drawFigure() {
System.out.println("Коло { радіус = " + this.radius + ", площа = "
+ getS() + ", колір - " + getColor() + " }");
}
}
11 changes: 11 additions & 0 deletions src/main/java/core/basesyntax/ColorSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package core.basesyntax;

import java.util.Random;

public class ColorSupplier {
public String getRandomColor() {
int index = new Random().nextInt(Colors.values().length);
Colors color = Colors.values()[index];
return color.toString();
}
}
11 changes: 11 additions & 0 deletions src/main/java/core/basesyntax/Colors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package core.basesyntax;

public enum Colors {
RED,
BLUE,
GREEN,
BLACK,
ORANGE,
YELLOW,
WHITE
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/Drawable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

public interface Drawable {
void drawFigure();
}
17 changes: 17 additions & 0 deletions src/main/java/core/basesyntax/Figure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package core.basesyntax;

public abstract class Figure implements Drawable, CalculateArea {
private String color;

public Figure(String color) {
this.color = color;
}

public abstract double getS();

public abstract void drawFigure();

public String getColor() {
return this.color;
}
}
36 changes: 36 additions & 0 deletions src/main/java/core/basesyntax/FigureSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package core.basesyntax;

import java.util.Random;

public class FigureSupplier {
public Figure getRandomFigure() {
ColorSupplier colorSupplier = new ColorSupplier();
Random random = new Random();
int index = random.nextInt(Figures.values().length);
Figures figure = Figures.values()[index];

switch (figure.toString()) {
case "SQUARE":
return new Square(colorSupplier.getRandomColor(), random.nextDouble() * 100);
case "RECTANGLE":
return new Rectangle(colorSupplier.getRandomColor(),
random.nextDouble() * 100, random.nextDouble() * 100);
case "RIGHT_TRIANGLE":
return new RightTriangle(colorSupplier.getRandomColor(), random.nextDouble() * 100,
random.nextDouble() * 100, random.nextDouble() * 100);
case "CIRCLE":
return new Circle(colorSupplier.getRandomColor(), random.nextDouble() * 100);
case "ISOSCELES_TRAPEZOID":
return new IsoscelesTrapezoid(colorSupplier.getRandomColor(),
random.nextDouble() * 100,
random.nextDouble() * 100,
random.nextDouble() * 100);
default:
return null;
}
}

public Figure getDefaultFigure() {
return new Circle(Colors.WHITE.toString(), 10);
}
}
9 changes: 9 additions & 0 deletions src/main/java/core/basesyntax/Figures.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package core.basesyntax;

public enum Figures {
SQUARE,
RECTANGLE,
RIGHT_TRIANGLE,
CIRCLE,
ISOSCELES_TRAPEZOID
}
15 changes: 15 additions & 0 deletions src/main/java/core/basesyntax/HelloWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,20 @@
* Feel free to remove this class and create your own.
*/
public class HelloWorld {
public static void main(String[] args) {
Figure[] array = new Figure[6];
FigureSupplier figureSupplier = new FigureSupplier();

for (int i = 0; i <= 2; i++) {
array[i] = figureSupplier.getRandomFigure();
}

for (int i = 3; i < array.length; i++) {
array[i] = figureSupplier.getDefaultFigure();
}

for (Figure figure : array) {
figure.drawFigure();
}
}
}
26 changes: 26 additions & 0 deletions src/main/java/core/basesyntax/IsoscelesTrapezoid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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;
}

public double getS() {
return ((this.base1 + this.base2) / 2) * this.height;
}

@Override
public void drawFigure() {
System.out.println("Рівнобічна трапеція { основа1 = " + this.base1
+ ", основа2 = " + this.base2
+ ", висота = " + this.height + ", площа = " + getS()
+ "колір - " + getColor() + " }");
}
}
23 changes: 23 additions & 0 deletions src/main/java/core/basesyntax/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package core.basesyntax;

public class Rectangle extends Figure {
private double side1;
private double side2;

public Rectangle(String color, double side1, double side2) {
super(color);
this.side1 = side1;
this.side2 = side2;
}

@Override
public double getS() {
return side1 * side2;
}

@Override
public void drawFigure() {
System.out.println("Прямокутник { сторона1 = " + this.side1 + ", сторона2 = " + this.side2
+ ", площа = " + getS() + ", колір - " + getColor() + " }");
}
}
26 changes: 26 additions & 0 deletions src/main/java/core/basesyntax/RightTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package core.basesyntax;

public class RightTriangle extends Figure {
private double leg1;
private double leg2;
private double hypotenusa;

public RightTriangle(String color, double leg1, double leg2, double hypotenusa) {
super(color);
this.leg1 = leg1;
this.leg2 = leg2;
this.hypotenusa = hypotenusa;
}

public double getS() {
return (this.leg1 * this.leg2) / 2;
}

@Override
public void drawFigure() {
System.out.println("Прямокутний трикутник { катет1 = " + this.leg1 + ", катет2 = "
+ this.leg2 + ", гіпотенуза = " + this.hypotenusa
+ ", площа = " + getS()
+ ", колір - " + getColor() + " }");
}
}
21 changes: 21 additions & 0 deletions src/main/java/core/basesyntax/Square.java
Original file line number Diff line number Diff line change
@@ -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 double getS() {
return Math.pow(this.side, 2);
}

@Override
public void drawFigure() {
System.out.println("Квадрат { сторона = " + this.side + ", площа = "
+ getS() + ", колір - " + getColor() + " }");
}
}

0 comments on commit 34e82cb

Please sign in to comment.