-
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
Implement tasks #1760
base: master
Are you sure you want to change the base?
Implement tasks #1760
Changes from 8 commits
648d5f6
e9a4fb8
34c0654
914f263
97552aa
823960e
e8cef99
be5e385
0c2f03e
4819868
1579066
2cdfd4b
fd9ebc8
aa4a06a
576dc56
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package core.basesyntax; | ||
|
||
public class Application { | ||
public static void main(String[] args) { | ||
Figure[] figures = new Figure[6]; | ||
int halfSize = figures.length / 2; | ||
for (int i = 0; i < halfSize; i++) { | ||
figures[i] = FigureSupplier.getRandomFigure(); | ||
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. Avoid using static methods for |
||
figures[halfSize + i] = FigureSupplier.getDefaultFigure(); | ||
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. Similarly, avoid using static methods for |
||
} | ||
for (Figure figure : figures) { | ||
figure.draw(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class ColorSupplier { | ||
public static String getRandomColor() { | ||
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. Avoid using a static method for |
||
StringBuilder sb = new StringBuilder("rgb("); | ||
Random random = new Random(); | ||
int red = random.nextInt(256); | ||
int green = random.nextInt(256); | ||
int blue = random.nextInt(256); | ||
sb.append(red) | ||
.append(", ") | ||
.append(green) | ||
.append(", ") | ||
.append(blue) | ||
.append(")"); | ||
return sb.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface Drawable { | ||
void draw(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package core.basesyntax; | ||
|
||
public abstract class Figure implements Drawable { | ||
private final String color; | ||
|
||
protected Figure(String color) { | ||
this.color = color; | ||
} | ||
|
||
public String getColor() { | ||
return color; | ||
} | ||
|
||
public abstract double getArea(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package core.basesyntax; | ||
|
||
import core.basesyntax.models.Circle; | ||
import core.basesyntax.models.IsoscelesTrapezoid; | ||
import core.basesyntax.models.Rectangle; | ||
import core.basesyntax.models.RightTriangle; | ||
import core.basesyntax.models.Square; | ||
import java.util.Random; | ||
|
||
public class FigureSupplier { | ||
public static Figure getRandomFigure() { | ||
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. Avoid using a static method for |
||
Random random = new Random(); | ||
double squareSide = random.nextDouble(); | ||
double width = random.nextDouble(); | ||
double height = random.nextDouble(); | ||
double firstLeg = random.nextDouble(); | ||
double secondLeg = random.nextDouble(); | ||
double radius = random.nextDouble(); | ||
double bottom = random.nextDouble(); | ||
double top = random.nextDouble(); | ||
double trapSide = random.nextDouble(); | ||
Comment on lines
+23
to
+31
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 random values for dimensions should be within a realistic range to ensure meaningful figures are generated. Consider setting a minimum and maximum value for these random numbers. |
||
int randomFigureIndex = random.nextInt(Figures.values().length); | ||
Figures figure = Figures.values()[randomFigureIndex]; | ||
Figure randomFigure = switch (figure) { | ||
case SQUARE -> new Square(ColorSupplier.getRandomColor(), squareSide); | ||
case RECTANGLE -> new Rectangle(ColorSupplier.getRandomColor(), width, height); | ||
case RIGHT_TRIANGLE -> | ||
new RightTriangle(ColorSupplier.getRandomColor(), firstLeg, secondLeg); | ||
case CIRCLE -> new Circle(ColorSupplier.getRandomColor(), radius); | ||
case ISOSCELES_TRAPEZOID -> | ||
new IsoscelesTrapezoid(ColorSupplier.getRandomColor(), trapSide, bottom, top); | ||
}; | ||
return randomFigure; | ||
} | ||
|
||
public static Figure getDefaultFigure() { | ||
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. Avoid using a static method for |
||
return new Circle("rgb(255, 255, 255)", 10.0); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package core.basesyntax; | ||
|
||
public enum Figures { | ||
SQUARE, | ||
RECTANGLE, | ||
RIGHT_TRIANGLE, | ||
CIRCLE, | ||
ISOSCELES_TRAPEZOID | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package core.basesyntax.models; | ||
|
||
import core.basesyntax.Figure; | ||
|
||
public class Circle extends Figure { | ||
private final 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.printf( | ||
"Figure: circle, area: %.1f sq. units, radius: %.1f units, color: %s\n", | ||
this.getArea(), | ||
this.radius, | ||
this.getColor() | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package core.basesyntax.models; | ||
|
||
import core.basesyntax.Figure; | ||
|
||
public class IsoscelesTrapezoid extends Figure { | ||
private final double side; | ||
private final double bottom; | ||
private final double top; | ||
|
||
public IsoscelesTrapezoid(String color, double side, double bottom, double top) { | ||
super(color); | ||
this.side = side; | ||
this.bottom = bottom; | ||
this.top = top; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return ((bottom + top) / 4) * Math.sqrt(4 * side * side - Math.pow((bottom - top), 2)); | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.printf( | ||
"Figure: isosceles trapezoid, area: %.1f sq. units, side: %.1f units," | ||
+ " bottom: %.1f units, top: %.1f units, color: %s\n", | ||
this.getArea(), | ||
this.side, | ||
this.bottom, | ||
this.top, | ||
this.getColor() | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package core.basesyntax.models; | ||
|
||
import core.basesyntax.Figure; | ||
|
||
public class Rectangle extends Figure { | ||
private final double width; | ||
private final double height; | ||
|
||
public Rectangle(String color, double width, double height) { | ||
super(color); | ||
this.width = width; | ||
this.height = height; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return width * height; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.printf( | ||
"Figure: rectangle, area: %.1f sq. units, width: %.1f units," | ||
+ " height: %.1f units, color: %s\n", | ||
this.getArea(), | ||
this.width, | ||
this.height, | ||
this.getColor() | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package core.basesyntax.models; | ||
|
||
import core.basesyntax.Figure; | ||
|
||
public class RightTriangle extends Figure { | ||
private final double firstLeg; | ||
private final double secondLeg; | ||
|
||
public RightTriangle(String color, double firstLeg, double secondLeg) { | ||
super(color); | ||
this.firstLeg = firstLeg; | ||
this.secondLeg = secondLeg; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return firstLeg * secondLeg / 2; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.printf( | ||
"Figure: right triangle, area: %.1f sq. units, first leg: %.1f units," | ||
+ " second leg: %.1f units, color: %s\n", | ||
this.getArea(), | ||
this.firstLeg, | ||
this.secondLeg, | ||
this.getColor() | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package core.basesyntax.models; | ||
|
||
import core.basesyntax.Figure; | ||
|
||
public class Square extends Figure { | ||
private final 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.printf( | ||
"Figure: square, area: %.1f sq. units, side: %.1f units, color: %s\n", | ||
this.getArea(), | ||
this.side, | ||
this.getColor() | ||
); | ||
} | ||
} |
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.
Consider replacing the magic number
6
with a named constant to improve code readability and maintainability. This aligns with the checklist requirement to avoid magic numbers in your code.