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

/**
* Feel free to remove this class and create your own.
*/
import core.basesyntax.figures.Figure;
import core.basesyntax.service.FigureSupplier;
import core.basesyntax.variables.Constants;

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

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

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

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

for (Figure figure : figures) {
System.out.println(figure);
}
}
}
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.variables.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();
}
}
19 changes: 19 additions & 0 deletions src/main/java/core/basesyntax/figures/Figure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core.basesyntax.figures;

import core.basesyntax.variables.Color;

public abstract class Figure implements FigureBehavior {
private Color color;

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

public Color getColor() {
return color;
}

public void setColor(Color color) {
this.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.variables.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.variables.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.variables.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.variables.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();
}
}
14 changes: 14 additions & 0 deletions src/main/java/core/basesyntax/service/ColorSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package core.basesyntax.service;

import core.basesyntax.variables.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];
}
}
51 changes: 51 additions & 0 deletions src/main/java/core/basesyntax/service/FigureSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package core.basesyntax.service;

import core.basesyntax.figures.Circle;
import core.basesyntax.figures.Figure;
import core.basesyntax.figures.IsoscelesTrapezoid;
import core.basesyntax.figures.Rectangle;
import core.basesyntax.figures.RightTriangle;
import core.basesyntax.figures.Square;
import core.basesyntax.variables.Color;
import core.basesyntax.variables.FigureType;
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()
);
Comment on lines +24 to +44

Choose a reason for hiding this comment

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

The random dimensions generated for the figures (e.g., side lengths, radius) are not constrained, which might result in impractically small or large figures. Consider adding constraints to the random values to ensure they fall within a reasonable range.

};
}

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

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

public class Constants {
public static final int NUMBER_OF_FIGURES = 10;
public static final int HALF = NUMBER_OF_FIGURES / 2;
}
9 changes: 9 additions & 0 deletions src/main/java/core/basesyntax/variables/FigureType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package core.basesyntax.variables;

public enum FigureType {
SQUARE,
RECTANGLE,
RIGHT_TRIANGLE,
CIRCLE,
ISOSCELES_TRAPEZOID
}
Loading