Skip to content

Commit

Permalink
make figure clases and implementations for them
Browse files Browse the repository at this point in the history
  • Loading branch information
Mykola committed Dec 25, 2024
1 parent 3b03b22 commit 5078789
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 8 deletions.
30 changes: 30 additions & 0 deletions src/main/java/core/basesyntax/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package core.basesyntax;

import java.util.Random;

public class Circle extends Figure implements FindArea {
private int radius;

public Circle() {
super();
this.radius = new Random().nextInt(1, 10);
}

public Circle(int radius, String color) {
super(color);
this.radius = radius;
}

@Override
public double getArea() {
return 2 * Math.PI * Math.pow(radius, 2);
}

@Override
public void draw() {
System.out.println("\nCircle "
+ "color: " + this.getColor()
+ " area: " + this.getArea()
+ " radius: " + radius);
}
}
11 changes: 11 additions & 0 deletions src/main/java/core/basesyntax/Color.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package core.basesyntax;

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

import java.util.Random;

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

public abstract class Figure {
private String color;
private int area;

public Figure() {
color = new ColorSupplier().getRandomColor();
}

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

public double getArea() {
return area;
}

public void setArea(int area) {
this.area = area;
}

public abstract void draw();

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}
}
22 changes: 22 additions & 0 deletions src/main/java/core/basesyntax/FigureSuplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package core.basesyntax;

import java.lang.invoke.SwitchPoint;
import java.util.Random;

public class FigureSuplier {
public Figure getRandomFigure() {
int index = new Random().nextInt(5);
return switch (index) {
case 1 -> new Circle();
case 2 -> new IsoscelesTrapezoid();
case 3 -> new Rectangle();
case 4 -> new RightTriangle();
case 5 -> new Square();
default -> null;
};
}

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

public interface FindArea {
double getArea();
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

50 changes: 50 additions & 0 deletions src/main/java/core/basesyntax/IsoscelesTrapezoid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package core.basesyntax;

import java.util.Random;

public class IsoscelesTrapezoid extends Figure implements FindArea {
private int biggerBaseSide;
private int smallerBaseSide;
private double side;
private int height;

public IsoscelesTrapezoid() {
super();
int firstRandomInt = new Random().nextInt(1, 10);
int secondRandomInt = new Random().nextInt(1, 10);
if (firstRandomInt > secondRandomInt) {
biggerBaseSide = firstRandomInt;
smallerBaseSide = secondRandomInt;
} else {
smallerBaseSide = firstRandomInt;
biggerBaseSide = secondRandomInt;
}
this.height = 10;
this.side = getSide();
}

@Override
public double getArea() {
return (double) (biggerBaseSide + smallerBaseSide) * height / 2;
}

@Override
public void draw() {
System.out.println("\nIsosceles trapezoid "
+ "color: " + this.getColor()
+ " area: " + this.getArea()
+ " bigger base: " + biggerBaseSide
+ " smaller base: " + smallerBaseSide
+ " side: " + side
+ " height: " + height);
}

private double getSide() {
if (biggerBaseSide == smallerBaseSide) {
return height;
}

return Math.sqrt(Math.pow(((double) (biggerBaseSide - smallerBaseSide) / 2), 2)
+ Math.pow(height, 2));
}
}
19 changes: 19 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core.basesyntax;

public class Main {
public static void main(String[] arg) {
Figure[] figures = new Figure[6];

for (int i = 0; i < figures.length/2; i++) {
figures[i] = new FigureSuplier().getRandomFigure();
}

for (int i = 3; i < figures.length; i++) {
figures[i] = new FigureSuplier().getDeafoltFigure();
}

for (Figure figure: figures) {
figure.draw();
}
}
}
28 changes: 28 additions & 0 deletions src/main/java/core/basesyntax/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package core.basesyntax;

import java.util.Random;

public class Rectangle extends Figure implements FindArea {
private int firstSide;
private int secondSide;

public Rectangle() {
super();
this.firstSide = new Random().nextInt(1, 10);
this.secondSide = new Random().nextInt(1, 10);
}

@Override
public double getArea() {
return firstSide * secondSide;
}

@Override
public void draw() {
System.out.println("\nRectangle "
+ "color: " + this.getColor()
+ " area: " + this.getArea()
+ " first side: " + firstSide
+ " first side: " + secondSide);
}
}
31 changes: 31 additions & 0 deletions src/main/java/core/basesyntax/RightTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package core.basesyntax;

import java.util.Random;

public class RightTriangle extends Figure implements FindArea {
private int firsLeg;
private int secondLeg;
private double thirdLeg;

public RightTriangle() {
super();
this.firsLeg = new Random().nextInt(1, 10);
this.secondLeg = new Random().nextInt(1, 10);
this.thirdLeg = Math.sqrt(Math.pow(firsLeg, 2) + Math.pow(secondLeg, 2));
}

@Override
public double getArea() {
return (double) firsLeg * secondLeg / 2;
}

@Override
public void draw() {
System.out.println("\nRight triangle "
+ "color: " + this.getColor()
+ " area: " + this.getArea()
+ " first leg: " + firsLeg
+ " second leg: " + secondLeg
+ " third leg: " + thirdLeg);
}
}
25 changes: 25 additions & 0 deletions src/main/java/core/basesyntax/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package core.basesyntax;

import java.util.Random;

public class Square extends Figure implements FindArea {
private int side;

public Square() {
super();
this.side = new Random().nextInt(1, 10);
}

@Override
public double getArea() {
return Math.pow(side, 2);
}

@Override
public void draw() {
System.out.println("\nSquere "
+ "color: " + this.getColor()
+ " area: " + this.getArea()
+ " side: " + side);
}
}

0 comments on commit 5078789

Please sign in to comment.