-
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
Fulfilled all the conditions of the task. Created classes of figures,… #1751
base: master
Are you sure you want to change the base?
Changes from 1 commit
71b8dbf
11059b8
b04ccc1
d842b70
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,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface AreaCalculator { | ||
double getArea(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package core.basesyntax; | ||
|
||
public class Circle extends Figure { | ||
private int radius; | ||
|
||
public Circle(int radius, String color) { | ||
this.radius = radius; | ||
this.color = color; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return Math.PI * radius * radius; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Circle, area: " + getArea() + " sq. units, radius: " | ||
+ radius + " units, color: " + color); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package core.basesyntax; | ||
|
||
public enum Color { | ||
WHITE, | ||
BLACK, | ||
GREEN, | ||
RED, | ||
YELLOW, | ||
BLUE | ||
} |
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 Random random = new Random(); | ||
|
||
public String getRandomColor() { | ||
int index = random.nextInt(Color.values().length); | ||
return Color.values()[index].name(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public abstract class Figure implements FigureDrawing, AreaCalculator { | ||
protected String color; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface FigureDrawing { | ||
void draw(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class FigureSupplier { | ||
private static final int FIGURE_NUMBER = 5; | ||
private static final int DEFAULT_RADIUS = 10; | ||
private static final int RANDOM_SIDE = 10; | ||
private ColorSupplier supplier = new ColorSupplier(); | ||
private Random random = new Random(); | ||
|
||
public Figure getRundomFigure() { | ||
String color = supplier.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. Instead of using |
||
int randomFigure = random.nextInt(FIGURE_NUMBER); | ||
int randomSide = getRandomSide(); | ||
return switch (randomFigure) { | ||
case 0 -> new Circle(randomSide, color); | ||
case 1 -> new IsoscelesTrapezoid(randomSide, randomSide, randomSide, color); | ||
case 2 -> new Rectangle(randomSide, randomSide, color); | ||
case 3 -> new RightTriangle(randomSide, randomSide, color); | ||
case 4 -> new Square(randomSide, color); | ||
Comment on lines
+17
to
+21
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. Consider creating separate variables for expressions passed to constructors for better code readability, as suggested in the checklist. |
||
default -> new Square(randomSide, color); | ||
}; | ||
} | ||
|
||
public Figure getDefaultFigure() { | ||
return new Circle(DEFAULT_RADIUS, Color.WHITE.name()); | ||
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. When creating the default figure, consider passing the |
||
} | ||
|
||
private int getRandomSide() { | ||
return random.nextInt(RANDOM_SIDE + 1); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package core.basesyntax; | ||
|
||
public class IsoscelesTrapezoid extends Figure { | ||
private int bottomBase; | ||
private int topBase; | ||
private int hight; | ||
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 variable name 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. There is a typo in the field name |
||
|
||
public IsoscelesTrapezoid(int bottomBase, int topBase, int hight, String color) { | ||
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. Consider using the |
||
this.bottomBase = bottomBase; | ||
this.topBase = topBase; | ||
this.hight = hight; | ||
this.color = color; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return ((bottomBase + topBase) / 2) * hight; | ||
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. Ensure that the calculation in |
||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("IsoscelesTrapezoid, area: " + getArea() + " sq. units, bottomBase: " | ||
+ bottomBase + " units, topBase: " + topBase + " units, hight: " + hight | ||
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. Ensure that the field name |
||
+ " units, color: " + color); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package core.basesyntax; | ||
|
||
public class Main { | ||
private static final int FIGURES_NUMBER = 6; | ||
|
||
public static void main(String[] args) { | ||
FigureSupplier figureSupplier = new FigureSupplier(); | ||
Figure[] figures = new Figure[FIGURES_NUMBER]; | ||
|
||
for (int i = 0; i < figures.length; i++) { | ||
if (i < figures.length / 2) { | ||
figures[i] = figureSupplier.getRundomFigure(); | ||
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. There is a typo in the method call |
||
} else { | ||
figures[i] = figureSupplier.getDefaultFigure(); | ||
} | ||
figures[i].draw(); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Figure { | ||
private int length; | ||
private int width; | ||
|
||
public Rectangle(int length, int width, String color) { | ||
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. Consider using the |
||
this.length = length; | ||
this.width = width; | ||
this.color = color; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return length * width; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Rectangle, area: " + getArea() + " sq. units, length: " | ||
+ length + " units, width: " + width + " units, color: " + color); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package core.basesyntax; | ||
|
||
public class RightTriangle extends Figure { | ||
private int legA; | ||
private int legB; | ||
|
||
public RightTriangle(int legA, int legB, String color) { | ||
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. Consider using the |
||
this.legA = legA; | ||
this.legB = legB; | ||
this.color = color; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return (legA * legB) / 2; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
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. Ensure that the |
||
System.out.println("Rectangle, area: " + getArea() + " sq. units, legA: " | ||
+ legA + " units, legB: " + legB + " units, color: " + color); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package core.basesyntax; | ||
|
||
public class Square extends Figure { | ||
private int side; | ||
|
||
public Square(int side, String color) { | ||
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. Consider using the |
||
this.side = side; | ||
this.color = color; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return side * side; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Square, area: " + getArea() + " sq. units, side: " | ||
+ side + " units, color: " + color); | ||
} | ||
} |
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.
There is a typo in the method name
getRundomFigure()
. It should be corrected togetRandomFigure()
.