-
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
created several Figure and color classes and their suppliers #1729
base: master
Are you sure you want to change the base?
Changes from all commits
ef09d43
53f9913
934604d
12cba27
9ab5bf8
74547db
42293b5
1252a82
9036275
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,7 @@ | ||
package core.basesyntax; | ||
|
||
public interface AreaCalculator { | ||
|
||
int calculateArea(); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package core.basesyntax; | ||
|
||
public class Circle extends Figure { | ||
private int radius; | ||
|
||
public Circle(String color, String name, int radius) { | ||
super(color, name); | ||
this.radius = radius; | ||
} | ||
|
||
@Override | ||
public int calculateArea() { | ||
return (int) (radius * radius * Math.PI); | ||
|
||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Figure:" | ||
+ "name: " | ||
+ super.getName() | ||
+ ", " | ||
+ "area: " | ||
+ calculateArea() | ||
+ " sq. units, " | ||
+ "radius: " | ||
+ radius | ||
+ ", " | ||
+ "color: " | ||
+ super.getColor(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package core.basesyntax; | ||
|
||
public enum Color { | ||
YELLOW, | ||
BLUE, | ||
BLACK, | ||
WHITE, | ||
GREEN, | ||
RED | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class ColorSupplier { | ||
|
||
public String getRandomColor() { | ||
Color[] values = Color.values(); | ||
int randomIndex = new Random().nextInt(values.length); | ||
return values[randomIndex].name(); | ||
Comment on lines
+7
to
+10
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 changing the return type of |
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package core.basesyntax; | ||
|
||
public class Figure implements AreaCalculator { | ||
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 making |
||
private String color; | ||
private String name; | ||
|
||
public Figure(String color, String name) { | ||
this.color = color; | ||
this.name = name; | ||
} | ||
|
||
public Figure() { | ||
} | ||
|
||
public void draw() { | ||
System.out.println(this); | ||
} | ||
|
||
public String getColor() { | ||
return color; | ||
} | ||
|
||
public void setColor(String color) { | ||
this.color = color; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Figure{" | ||
+ "color='" | ||
+ color | ||
+ ", " | ||
+ "name='" | ||
+ name | ||
+ '\'' | ||
+ '}'; | ||
} | ||
|
||
@Override | ||
public int calculateArea() { | ||
return 0; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package core.basesyntax; | ||
|
||
public enum FigureName { | ||
CIRCLE, | ||
ISOSCELES_TRAPEZOID, | ||
RECTANGLE, | ||
SQUARE, | ||
RIGHT_TRIANGLE | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class FigureSupplier { | ||
|
||
public Figure getRandomFigure() { | ||
Figure randomFigure; | ||
int randomIndex = new Random().nextInt(5); | ||
switch (randomIndex) { | ||
case 0 -> randomFigure = getRandomCircle(); | ||
case 1 -> randomFigure = getIsoscelesTrapezoid(); | ||
case 2 -> randomFigure = getRandomRectangle(); | ||
case 3 -> randomFigure = getRightTriangle(); | ||
case 4 -> randomFigure = getRandomSquare(); | ||
default -> randomFigure = new Figure(); | ||
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 default case in the switch statement creates a new |
||
} | ||
return randomFigure; | ||
} | ||
|
||
public Figure getDefaultFigure() { | ||
return new Circle(Color.WHITE.name(), FigureName.CIRCLE.name(), 10); | ||
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 |
||
} | ||
|
||
private Circle getRandomCircle() { | ||
return new Circle(new ColorSupplier().getRandomColor(), | ||
FigureName.CIRCLE.name(), | ||
Comment on lines
+26
to
+27
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 |
||
new Random().nextInt(101)); | ||
} | ||
|
||
private IsoscelesTrapezoid getIsoscelesTrapezoid() { | ||
return new IsoscelesTrapezoid(new ColorSupplier().getRandomColor(), | ||
FigureName.ISOSCELES_TRAPEZOID.name(), | ||
Comment on lines
+32
to
+33
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 |
||
new Random().nextInt(101), | ||
new Random().nextInt(101), | ||
new Random().nextInt(101)); | ||
} | ||
|
||
private Rectangle getRandomRectangle() { | ||
return new Rectangle(new ColorSupplier().getRandomColor(), | ||
FigureName.RECTANGLE.name(), | ||
Comment on lines
+40
to
+41
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 |
||
new Random().nextInt(101), | ||
new Random().nextInt(101)); | ||
} | ||
|
||
private RightTriangle getRightTriangle() { | ||
return new RightTriangle(new ColorSupplier().getRandomColor(), | ||
FigureName.RIGHT_TRIANGLE.name(), | ||
Comment on lines
+47
to
+48
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 |
||
new Random().nextInt(101)); | ||
} | ||
|
||
private Square getRandomSquare() { | ||
return new Square(new ColorSupplier().getRandomColor(), | ||
FigureName.SQUARE.name(), | ||
Comment on lines
+53
to
+54
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 |
||
new Random().nextInt(101)); | ||
} | ||
|
||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package core.basesyntax; | ||
|
||
public class IsoscelesTrapezoid extends Figure { | ||
private int bottomBase; | ||
private int topBase; | ||
private int side; | ||
|
||
public IsoscelesTrapezoid(String color, String name, int bottomBase, int topBase, int side) { | ||
super(color, name); | ||
this.bottomBase = bottomBase; | ||
this.topBase = topBase; | ||
this.side = side; | ||
} | ||
|
||
@Override | ||
public int calculateArea() { | ||
return (int) (((bottomBase + topBase) / 2) | ||
* (Math.sqrt((Math.pow(side, 2)) | ||
- Math.pow((((Math.pow((bottomBase - topBase), 2)) | ||
+ (Math.pow(side, 2)) - (Math.pow(side, 2))) | ||
/ (2 * (bottomBase - topBase))), 2)))); | ||
Comment on lines
+16
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. The |
||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Figure:" | ||
+ "name: " | ||
+ super.getName() | ||
+ ", " | ||
+ "bottomBase: " | ||
+ bottomBase | ||
+ ", " | ||
+ "topBase: " | ||
+ topBase | ||
+ ", " | ||
+ "side: " | ||
+ side | ||
+ ", " | ||
+ "area: " | ||
+ calculateArea() | ||
+ " sq. units, " | ||
+ ", " | ||
Comment on lines
+41
to
+42
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 an extra comma before the 'color' field in the |
||
+ "color: " | ||
+ super.getColor(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package core.basesyntax; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
Figure[] figures = new Figure[6]; | ||
for (int i = 0; i < 3; i++) { | ||
figures[i] = new 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. Ensure that |
||
} | ||
for (int i = 3; i < 6; i++) { | ||
figures[i] = new FigureSupplier().getDefaultFigure(); | ||
} | ||
for (Figure figure : figures) { | ||
figure.draw(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Figure { | ||
private int firstLag; | ||
private int secondLag; | ||
Comment on lines
+4
to
+5
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 names |
||
|
||
public Rectangle(String color, String name, int firstLag, int secondLag) { | ||
super(color, name); | ||
this.firstLag = firstLag; | ||
this.secondLag = secondLag; | ||
} | ||
|
||
@Override | ||
public int calculateArea() { | ||
return firstLag * secondLag; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Figure:" | ||
+ "name: " | ||
+ super.getName() | ||
+ ", " | ||
+ "area: " | ||
+ calculateArea() | ||
+ " sq. units, " | ||
+ "firstLag: " | ||
+ firstLag | ||
+ ", " | ||
+ "secondLag: " | ||
+ secondLag | ||
+ ", " | ||
+ "color: " | ||
+ super.getColor(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package core.basesyntax; | ||
|
||
public class RightTriangle extends Figure { | ||
private int firstLag; | ||
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 |
||
|
||
public RightTriangle(String color, String name, int firstLag) { | ||
super(color, name); | ||
this.firstLag = firstLag; | ||
} | ||
|
||
@Override | ||
public int calculateArea() { | ||
return (int) (Math.pow(firstLag, 2) * (Math.sqrt(3)) / 4); | ||
Comment on lines
+12
to
+13
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 used in |
||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Figure:" | ||
+ "name: " | ||
+ super.getName() | ||
+ ", " | ||
+ "area: " | ||
+ calculateArea() | ||
+ " sq. units, " | ||
+ "firstLag: " | ||
+ firstLag | ||
+ ", " | ||
+ "color: " | ||
+ super.getColor(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package core.basesyntax; | ||
|
||
public class Square extends Figure { | ||
private int firstLag; | ||
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 |
||
|
||
public Square(String color, String name, int firstLag) { | ||
super(color, name); | ||
this.firstLag = firstLag; | ||
} | ||
|
||
@Override | ||
public int calculateArea() { | ||
return firstLag * firstLag; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Figure:" | ||
+ "name: " | ||
+ super.getName() | ||
+ ", " | ||
+ "area: " | ||
+ calculateArea() | ||
+ " sq. units, " | ||
+ "firstLag: " | ||
+ firstLag | ||
+ ", " | ||
+ "color: " | ||
+ super.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.
The
calculateArea
method returns anint
, but the area of a circle is a double value due to the use ofMath.PI
. Consider changing the return type todouble
to avoid loss of precision.