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

solution #1774

Open
wants to merge 2 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
28 changes: 25 additions & 3 deletions src/main/java/core/basesyntax/HelloWorld.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
package core.basesyntax;

/**
* Feel free to remove this class and create your own.
*/
import core.basesyntax.enums.Color;
import core.basesyntax.figures.Figure;
import core.basesyntax.figures.Square;
import core.basesyntax.service.ColorSupplier;
import core.basesyntax.service.FigureSupplier;

public class HelloWorld {
public static void main(String[] args) {
FigureSupplier figureSupplier = new FigureSupplier();

final int NUMBER_OF_FIGURES = 10;
final int HALF = NUMBER_OF_FIGURES / 2;

final Figure[] figures = new Figure[NUMBER_OF_FIGURES];

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

for (int i = HALF; i < NUMBER_OF_FIGURES; i++) {
figures[i] = figureSupplier.getDefaultFigure();
}

for (Figure figure : figures) {
System.out.println(figure);
}
}
}
11 changes: 11 additions & 0 deletions src/main/java/core/basesyntax/enums/Color.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package core.basesyntax.enums;

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

public enum FigureType {
SQUARE,
RECTANGLE,
RIGHT_TRIANGLE,
CIRCLE,
ISOSCELES_TRAPEZOID
}
34 changes: 34 additions & 0 deletions src/main/java/core/basesyntax/figures/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package core.basesyntax.figures;

import core.basesyntax.enums.Color;

public class Circle extends Figure {
private double radius;

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

public double getRadius() {
return radius;
}

public void setRadius(double radius) {
this.radius = radius;
}

@Override
public double calculateArea() {
return 2 * Math.pow(radius, 2) * Math.PI;
Comment on lines +22 to +23

Choose a reason for hiding this comment

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

The formula for calculating the area of a circle is incorrect. It should be Math.PI * Math.pow(radius, 2) instead of 2 * Math.pow(radius, 2) * Math.PI. Please correct this to ensure the area is calculated properly.

Comment on lines +22 to +23

Choose a reason for hiding this comment

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

The formula for calculating the area of a circle is incorrect. It should be Math.PI * Math.pow(radius, 2) instead of 2 * Math.pow(radius, 2) * Math.PI. Please update this to ensure accurate area calculations.

}

@Override
public String toString() {
return "Figure: circle"
+ ", area: " + calculateArea()
+ " sq. units"
+ ", radius: " + getRadius()
+ ", color: " + getColor();
}
}
21 changes: 21 additions & 0 deletions src/main/java/core/basesyntax/figures/Figure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.basesyntax.figures;

import core.basesyntax.enums.Color;

public abstract class Figure implements FigureBehavior {
public Figure(Color color) {
this.color = color;
}

public Color getColor() {
return color;
}

public void setColor(Color color) {
this.color = color;
}

private Color color;


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

public interface FigureBehavior {
double calculateArea();
}
65 changes: 65 additions & 0 deletions src/main/java/core/basesyntax/figures/IsoscelesTrapezoid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package core.basesyntax.figures;

import core.basesyntax.enums.Color;

public class IsoscelesTrapezoid extends Figure {
private double top;
private double bottom;
private double height;

public IsoscelesTrapezoid(Color color, double top, double bottom, double side) {

Choose a reason for hiding this comment

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

The constructor parameter side is misleading as it is used to set the height. Consider renaming it to height for better clarity.

super(color);
this.top = top;
this.bottom = bottom;
this.height = side;
Comment on lines +10 to +14

Choose a reason for hiding this comment

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

The constructor parameter side is misleading as it is used to set the height. Consider renaming it to height for clarity.

}

public double getTop() {
return top;
}

public void setTop(double top) {
this.top = top;
}

public double getBottom() {
return bottom;
}

public void setBottom(double bottom) {
this.bottom = bottom;
}

public double getSide() {
return height;
}

public void setSide(double side) {
this.height = side;
}

public double getHeight() {
return height;
}

public void setHeight(double height) {
this.height = height;
}

@Override
public double calculateArea() {
return (top + bottom) * height / 2;
}

@Override
public String toString() {
return "Figure: right triangle"

Choose a reason for hiding this comment

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

The toString method incorrectly describes the figure as a 'right triangle'. It should be 'isosceles trapezoid'. Please update the description to accurately reflect the figure type.

Choose a reason for hiding this comment

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

The toString method incorrectly labels the figure as a 'right triangle'. It should be 'isosceles trapezoid' to accurately reflect the figure type.

+ ", area: " + calculateArea()
+ " sq. units"
+ ", top: " + getTop()
+ ", bottom: " + getBottom()
+ ", side: " + getSide()
+ ", height: " + getHeight()
+ ", color: " + getColor();
}
}
45 changes: 45 additions & 0 deletions src/main/java/core/basesyntax/figures/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package core.basesyntax.figures;

import core.basesyntax.enums.Color;

public class Rectangle extends Figure {
private double firstSide;
private double secondSide;

public Rectangle(Color color, double firstSide, double secondSide) {
super(color);
this.firstSide = firstSide;
this.secondSide = secondSide;
}

public double getFirstSide() {
return firstSide;
}

public void setFirstSide(double firstSide) {
this.firstSide = firstSide;
}

public double getSecondSide() {
return secondSide;
}

public void setSecondSide(double secondSide) {
this.secondSide = secondSide;
}

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

@Override
public String toString() {
return "Figure: rectangle"
+ ", area: " + calculateArea()
+ " sq. units"
+ ", first side: " + getFirstSide()
+ ", second side: " + getSecondSide()
+ ", color: " + getColor();
}
}
45 changes: 45 additions & 0 deletions src/main/java/core/basesyntax/figures/RightTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package core.basesyntax.figures;

import core.basesyntax.enums.Color;

public class RightTriangle extends Figure {
private double firstLeg;
private double secondLeg;

public RightTriangle(Color color, double firstLeg, double secondLeg) {
super(color);
this.firstLeg = firstLeg;
this.secondLeg = secondLeg;
}

public double getFirstLeg() {
return firstLeg;
}

public void setFirstLeg(double firstLeg) {
this.firstLeg = firstLeg;
}

public double getSecondLeg() {
return secondLeg;
}

public void setSecondLeg(double secondLeg) {
this.secondLeg = secondLeg;
}

@Override
public double calculateArea() {
return firstLeg * secondLeg / 2;
}

@Override
public String toString() {
return "Figure: right triangle"
+ ", area: " + calculateArea()
+ " sq. units"
+ ", first leg: " + getFirstLeg()
+ ", second leg: " + getSecondLeg()
+ ", color: " + getColor();
}
}
34 changes: 34 additions & 0 deletions src/main/java/core/basesyntax/figures/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package core.basesyntax.figures;

import core.basesyntax.enums.Color;

public class Square extends Figure {
private double side;

public Square(Color color, double side) {
super(color);
this.side = side;
}

public double getSide() {
return side;
}

public void setSide(double side) {
this.side = side;
}

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

@Override
public String toString() {
return "Figure: square"
+ ", area: " + calculateArea()
+ " sq. units"
+ ", side: " + getSide()
+ ", color: " + getColor();
}
}
15 changes: 15 additions & 0 deletions src/main/java/core/basesyntax/service/ColorSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package core.basesyntax.service;

import core.basesyntax.enums.Color;

import java.util.Random;

public class ColorSupplier {
private static final Random RANDOM = new Random();

public Color getRandomColor() {
int index = RANDOM.nextInt(Color.values().length);

return Color.values()[index];
}
}
31 changes: 31 additions & 0 deletions src/main/java/core/basesyntax/service/FigureSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package core.basesyntax.service;

import core.basesyntax.enums.Color;
import core.basesyntax.enums.FigureType;
import core.basesyntax.figures.*;

import java.util.Random;

public class FigureSupplier {
private static final Random RANDOM = new Random();
private static final ColorSupplier colorSupplier = new ColorSupplier();
private static final double DEFAULT_RADIUS = 10;

public Figure getRandomFigure() {
int index = RANDOM.nextInt(FigureType.values().length);
FigureType figureType = FigureType.values()[index];
Color color = colorSupplier.getRandomColor();

return switch (figureType) {
case SQUARE -> new Square(color, RANDOM.nextDouble());
case RECTANGLE -> new Rectangle(color, RANDOM.nextDouble(), RANDOM.nextDouble());
case RIGHT_TRIANGLE -> new RightTriangle(color, RANDOM.nextDouble(), RANDOM.nextDouble());
case CIRCLE -> new Circle(color, RANDOM.nextDouble());
default -> new IsoscelesTrapezoid(color, RANDOM.nextDouble(), RANDOM.nextDouble(), RANDOM.nextDouble());

Choose a reason for hiding this comment

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

The RANDOM.nextDouble() method generates a random double between 0.0 and 1.0. If you want to have a more meaningful range for the dimensions of the figures, consider scaling the random values by a factor or using a different method to generate them.

};
}

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