-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
base: master
Are you sure you want to change the base?
solution #1774
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
} | ||
} |
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 | ||
} |
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 | ||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Figure: circle" | ||
+ ", area: " + calculateArea() | ||
+ " sq. units" | ||
+ ", radius: " + getRadius() | ||
+ ", color: " + getColor(); | ||
} | ||
} |
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; | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax.figures; | ||
|
||
public interface FigureBehavior { | ||
double calculateArea(); | ||
} |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The constructor parameter |
||
super(color); | ||
this.top = top; | ||
this.bottom = bottom; | ||
this.height = side; | ||
Comment on lines
+10
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The constructor parameter |
||
} | ||
|
||
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
+ ", area: " + calculateArea() | ||
+ " sq. units" | ||
+ ", top: " + getTop() | ||
+ ", bottom: " + getBottom() | ||
+ ", side: " + getSide() | ||
+ ", height: " + getHeight() | ||
+ ", color: " + getColor(); | ||
} | ||
} |
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(); | ||
} | ||
} |
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(); | ||
} | ||
} |
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(); | ||
} | ||
} |
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]; | ||
} | ||
} |
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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
}; | ||
} | ||
|
||
public Figure getDefaultFigure() { | ||
return new Circle(Color.WHITE, DEFAULT_RADIUS); | ||
} | ||
} |
There was a problem hiding this comment.
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 of2 * Math.pow(radius, 2) * Math.PI
. Please correct this to ensure the area is calculated properly.