Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fulfilled all the conditions of the task. Created classes of figures,… #1751

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/AreaCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

public interface AreaCalculator {
double getArea();
}
21 changes: 21 additions & 0 deletions src/main/java/core/basesyntax/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.basesyntax;

public class Circle extends Figure {
private int radius;

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

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

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

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

import java.util.Random;

public class ColorSupplier {
private Random random = new Random();

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

public abstract class Figure implements FigureDrawing, AreaCalculator {
protected String color;
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/FigureDrawing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

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

import java.util.Random;

public class FigureSupplier {
private static final int FIGURE_NUMBER = 5;
private static final int DEFAULT_RADIUS = 10;
private static final int RANDOM_SIDE = 10;
private ColorSupplier supplier = new ColorSupplier();
private Random random = new Random();

public Figure getRundomFigure() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a typo in the method name getRundomFigure(). It should be corrected to getRandomFigure().

String color = supplier.getRandomColor();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using String for color, consider using the Color enum directly to improve type safety and align with the task requirements.

int randomFigure = random.nextInt(FIGURE_NUMBER);
int randomSide = getRandomSide();
return switch (randomFigure) {
case 0 -> new Circle(randomSide, color);
case 1 -> new IsoscelesTrapezoid(randomSide, randomSide, randomSide, color);
case 2 -> new Rectangle(randomSide, randomSide, color);
case 3 -> new RightTriangle(randomSide, randomSide, color);
case 4 -> new Square(randomSide, color);
Comment on lines +17 to +21

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider creating separate variables for expressions passed to constructors for better code readability, as suggested in the checklist.

default -> new Square(randomSide, color);
};
}

public Figure getDefaultFigure() {
return new Circle(DEFAULT_RADIUS, Color.WHITE.name());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When creating the default figure, consider passing the Color.WHITE enum directly instead of using Color.WHITE.name() to maintain consistency with the use of enums.

}

private int getRandomSide() {
return random.nextInt(RANDOM_SIDE + 1);
}
}
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 int bottomBase;
private int topBase;
private int hight;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name hight is misspelled. It should be corrected to height.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a typo in the field name hight. It should be corrected to height to maintain clarity and correctness.


public IsoscelesTrapezoid(int bottomBase, int topBase, int hight, String color) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the Color enum instead of a String for the color parameter to maintain type safety and consistency with the rest of the code.

this.bottomBase = bottomBase;
this.topBase = topBase;
this.hight = hight;
this.color = color;
}

@Override
public double getArea() {
return ((bottomBase + topBase) / 2) * hight;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the calculation in getArea() is correct according to the formula for the area of an isosceles trapezoid: ((bottomBase + topBase) / 2) * height.

}

@Override
public void draw() {
System.out.println("IsoscelesTrapezoid, area: " + getArea() + " sq. units, bottomBase: "
+ bottomBase + " units, topBase: " + topBase + " units, hight: " + hight

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the field name hight is corrected to height in the draw() method as well.

+ " units, color: " + color);
}
}
20 changes: 20 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package core.basesyntax;

public class Main {
private static final int FIGURES_NUMBER = 6;

public static void main(String[] args) {
FigureSupplier figureSupplier = new FigureSupplier();
Figure[] figures = new Figure[FIGURES_NUMBER];

for (int i = 0; i < figures.length; i++) {
if (i < figures.length / 2) {
figures[i] = figureSupplier.getRundomFigure();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a typo in the method call getRundomFigure(). It should be corrected to getRandomFigure() to match the method name in the FigureSupplier class.

} else {
figures[i] = figureSupplier.getDefaultFigure();
}
figures[i].draw();
}
}

}
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 int length;
private int width;

public Rectangle(int length, int width, String color) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the Color enum instead of a String for the color parameter to maintain type safety and consistency with the rest of the code.

this.length = length;
this.width = width;
this.color = color;
}

@Override
public double getArea() {
return length * width;
}

@Override
public void draw() {
System.out.println("Rectangle, area: " + getArea() + " sq. units, length: "
+ length + " units, width: " + width + " units, color: " + color);
}
}
23 changes: 23 additions & 0 deletions src/main/java/core/basesyntax/RightTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package core.basesyntax;

public class RightTriangle extends Figure {
private int legA;
private int legB;

public RightTriangle(int legA, int legB, String color) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the Color enum instead of a String for the color parameter to maintain type safety and consistency with the rest of the code.

this.legA = legA;
this.legB = legB;
this.color = color;
}

@Override
public double getArea() {
return (legA * legB) / 2;
}

@Override
public void draw() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The draw() method incorrectly labels the shape as 'Rectangle'. It should be corrected to 'RightTriangle'.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the draw() method correctly labels the shape as 'RightTriangle'.

System.out.println("Rectangle, area: " + getArea() + " sq. units, legA: "
+ legA + " units, legB: " + legB + " units, color: " + color);
}
}
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 int side;

public Square(int side, String color) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the Color enum instead of a String for the color parameter to maintain type safety and consistency with the rest of the code.

this.side = side;
this.color = color;
}

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

@Override
public void draw() {
System.out.println("Square, area: " + getArea() + " sq. units, side: "
+ side + " units, color: " + color);
}
}