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

Realized the creation of a generator of geometric shapes with random … #1753

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 22 additions & 0 deletions src/main/java/core/basesyntax/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package core.basesyntax;

public class Circle extends Figure {
private double radius;

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

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

@Override
public void draw() {
System.out.println("Figure: circle, area: " + getArea()
+ " radius: " + radius + " color " + color);
Comment on lines +18 to +19

Choose a reason for hiding this comment

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

The color field is accessed directly, assuming it is a protected or public field in the Figure class. If color is private in Figure, you need to provide a getter method in Figure to access it.

Copy link
Author

Choose a reason for hiding this comment

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

🔄 Исправьте проблемы, внесите изменения, а затем повторно запросите мою проверку с помощью Re-request reviewкнопки на вкладке «Рецензенты».
Где эта кнопка?


}
}
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 static final Random random = new Random();
private static final String[] Colors = {"red", "blue", "yellow", "green", "purple", "black"};

Choose a reason for hiding this comment

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

The array Colors should follow the Java naming convention for constants, which is all uppercase letters with words separated by underscores. Consider renaming it to COLORS.


public String getColorSupplier() {
return Colors[random.nextInt(Colors.length)];
}
}
6 changes: 6 additions & 0 deletions src/main/java/core/basesyntax/Drawable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package core.basesyntax;

public interface Drawable {

void draw();
}
19 changes: 19 additions & 0 deletions src/main/java/core/basesyntax/Figure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core.basesyntax;

public abstract class Figure implements Drawable, Measurable {
protected String color;

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

@Override
public void draw() {

}

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

import java.util.Random;

public class FigureSupplier {
private static final Random random2 = new Random();

Choose a reason for hiding this comment

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

The variable random2 should follow a more descriptive naming convention. Consider renaming it to random or randomGenerator for clarity.

private static final ColorSupplier COLOR_SUPPLIER = new ColorSupplier();

public Figure getRandomFigure() {
switch (random2.nextInt(5)) {
case 0:
return new Square(COLOR_SUPPLIER.getColorSupplier(), random2.nextDouble() * 10 + 1);
case 1:
return new Rectangle(COLOR_SUPPLIER.getColorSupplier(), random2.nextDouble()
* 10 + 1, random2.nextDouble() * 10 + 1);
case 2:
return new RightTriangle(COLOR_SUPPLIER.getColorSupplier(), random2.nextDouble()
* 10 + 1, random2.nextDouble() * 10 * 1);
case 3:
return new Circle(COLOR_SUPPLIER.getColorSupplier(), random2.nextDouble() * 10 + 1);
default:
return new IsoscelesTrapezoid(COLOR_SUPPLIER.getColorSupplier(),random2.nextDouble()
* 10 + 1, random2.nextDouble() * 10 + 1, random2.nextDouble() * 10 + 1);
}
}

public Figure getDefaultFigure() {
return new Circle("white", 10);
}
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

25 changes: 25 additions & 0 deletions src/main/java/core/basesyntax/IsoscelesTrapezoid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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;
}

@Override
public double getArea() {
return 0.5 * (base1 + base2) * height;
}

@Override
public void draw() {
System.out.println(" Figure: IsoscelesTrapezoid, area: " + getArea() + " base1: "
+ base1 + " base2: " + base2 + " height: " + height + " 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 {
public static void main(String[] args) {

FigureSupplier figureSupplier = new FigureSupplier();
Figure[] figures = new Figure[6];

for (int i = 0; i < figures.length / 2; i++) {
figures[i] = figureSupplier.getRandomFigure();
}
for (int i = figures.length / 2; i < figures.length; i++) {
figures[i] = figureSupplier.getDefaultFigure();
}
for (Figure figure : figures) {
figure.draw();
}
}
}

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

public interface Measurable {

double getArea();

}
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 width;
private double height;

public Rectangle(String color, double width, double height) {
super(color);
this.width = width;
this.width = height;

Choose a reason for hiding this comment

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

There is a mistake in the constructor: this.width = height; should be this.height = height;. This error causes the height field to be incorrectly initialized, affecting the area calculation.

}

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

@Override
public void draw() {
System.out.println("Figure rectangle, area: " + getArea() + " width" + width
+ " ,height" + height + " 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 double fistLeg;

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 variable name fistLeg. It should be firstLeg to accurately describe the first leg of the triangle.

private double secondLeg;

public RightTriangle(String color, double fistLeg, double secondLeg) {
super(color);
this.fistLeg = fistLeg;
this.secondLeg = secondLeg;
}

@Override
public double getArea() {
return 0.5 * fistLeg * secondLeg;
}

@Override
public void draw() {
System.out.println("Figure RightTriangle, area: " + getArea() + ", firstLeg "
+ fistLeg + ", secondLeg " + secondLeg + " color " + color);
}
}
22 changes: 22 additions & 0 deletions src/main/java/core/basesyntax/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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 getArea() {
return side * side;
}

@Override
public void draw() {
System.out.println("Figure: square, area: "
+ getArea() + " one side: " + side + " color:" + color);
}
}