-
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
make task #1311
base: master
Are you sure you want to change the base?
make task #1311
Changes from 8 commits
56e3ccd
ee4de70
81341e0
1f64ec8
4627f3f
716941b
bf0e20d
1f73b62
368c98e
e7b5c24
4fe9026
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 final int radius; | ||
|
||
public Circle(Color color,int radius) { | ||
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 should be a space after the comma in the parameters list of the constructor. |
||
super(color); | ||
this.radius = radius; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return Math.PI * radius * radius; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Circle{" + "radius=" + radius | ||
+ ", color='" + getColor() + '\'' + ", area=" + getArea() + '}'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package core.basesyntax; | ||
|
||
public enum Color { | ||
WHITE, | ||
BLACK, | ||
BLUE, | ||
RED | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class ColorSupplier { | ||
private final Random rand = new Random(); | ||
|
||
public Color getRandomColor() { | ||
return Color.values()[rand.nextInt(Color.values().length)]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package core.basesyntax; | ||
|
||
public abstract class Figure implements AreaCalculator, FigureDrawer { | ||
private final Color 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. this field is marked as 'final' which means that it can't be changed after it's initialized. This is okay if you will never need to change this value, but if you want your objects to be mutable, you should remove the 'final' keyword. |
||
|
||
public Figure(Color color) { | ||
this.color = color; | ||
} | ||
|
||
public Color getColor() { | ||
return color; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface FigureDrawer { | ||
void draw(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class FigureSupplier { | ||
public static final int MAX_NUMBER = 100; | ||
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 MAX_NUMBER constant could be private as it's not used outside of this class. |
||
private final ColorSupplier colorSupplier = new ColorSupplier(); | ||
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 colorSupplier object could be static because its method is stateless, and it would be more efficient to have only one instance of ColorSupplier for all FigureSupplier objects. |
||
private final Random random = new Random(); | ||
|
||
public Figure getRandomFigure() { | ||
FigureType randomFigure = FigureType.values()[random.nextInt(FigureType.values().length)]; | ||
switch (randomFigure) { | ||
case CIRCLE: | ||
return new Circle(colorSupplier.getRandomColor(), | ||
random.nextInt(MAX_NUMBER)); | ||
case SQUARE: | ||
return new Square(colorSupplier.getRandomColor(), | ||
random.nextInt(MAX_NUMBER)); | ||
case RECTANGLE: | ||
return new Rectangle(colorSupplier.getRandomColor(), | ||
random.nextInt(MAX_NUMBER), | ||
random.nextInt(MAX_NUMBER)); | ||
case RIGHT_RECTANGLE: | ||
return new RightTriangle(colorSupplier.getRandomColor(), | ||
random.nextInt(MAX_NUMBER), | ||
random.nextInt(MAX_NUMBER)); | ||
case ISOSCELES_TRAPEZOID: | ||
return new IsoscelesTrapezoid(colorSupplier.getRandomColor(), | ||
random.nextInt(MAX_NUMBER), | ||
random.nextInt(MAX_NUMBER), | ||
random.nextInt(MAX_NUMBER)); | ||
default: | ||
return getDefaultFigure(); | ||
} | ||
} | ||
|
||
public Figure getDefaultFigure() { | ||
return new Circle(Color.WHITE,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. |
||
} | ||
|
||
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. redundant indent line |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package core.basesyntax; | ||
|
||
public enum FigureType { | ||
CIRCLE, | ||
SQUARE, | ||
RECTANGLE, | ||
RIGHT_RECTANGLE, | ||
ISOSCELES_TRAPEZOID | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package core.basesyntax; | ||
|
||
public class IsoscelesTrapezoid extends Figure { | ||
private final double upSide; | ||
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. this fields is marked as 'final' which means that it can't be changed after it's initialized. This is okay if you will never need to change this value, but if you want your objects to be mutable, you should remove the 'final' keyword. |
||
private final double downSide; | ||
private final double height; | ||
|
||
public IsoscelesTrapezoid(Color color, double upSide, double downSide, double height) { | ||
super(color); | ||
this.upSide = upSide; | ||
this.downSide = downSide; | ||
this.height = height; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return (upSide + downSide) * height / 2; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("IsoscelesTrapezoid{" + "upSide=" + upSide | ||
+ ", downSide=" + downSide | ||
+ ", height=" + height + ", color='" | ||
+ getColor() + '\'' + ", area=" + getArea() + '}'); | ||
Comment on lines
+22
to
+25
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. behaviour - we can obtain the area of any figure and are able to draw it. To 'draw' means to print out all information about a figure using System.out.println() |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package core.basesyntax; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
Figure[] figures = new Figure[4]; | ||
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 number of figures is hardcoded to 4, but the task description says to create an array of figures where the first half are generated with random parameters and the second half have default parameters. the key point is "the size of the array can be 3 or 6, it doesn't matter". Consider making the size of the array dynamic and clarify the number of figures to be generated to the constant. use loop for dynamic objects processing |
||
FigureSupplier figureSupplier = new FigureSupplier(); | ||
figures[0] = figureSupplier.getRandomFigure(); | ||
figures[1] = figureSupplier.getRandomFigure(); | ||
figures[2] = new Circle(Color.WHITE, 10); | ||
figures[3] = new Square(Color.BLACK, 5); | ||
for (Figure figure : figures) { | ||
figure.draw(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Figure { | ||
private final double firstLeg; | ||
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. this fields is marked as 'final' which means that it can't be changed after it's initialized. This is okay if you will never need to change this value, but if you want your objects to be mutable, you should remove the 'final' keyword. |
||
private final double secondLeg; | ||
|
||
public Rectangle(Color color, double firstLeg, double secondLeg) { | ||
super(color); | ||
this.firstLeg = firstLeg; | ||
this.secondLeg = secondLeg; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return firstLeg * secondLeg; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Rectangle{" + "firstLeg=" + firstLeg | ||
+ ", secondLeg=" + secondLeg + ", color='" + getColor() + '\'' | ||
+ ", area=" + getArea() + '}'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package core.basesyntax; | ||
|
||
public class RightTriangle extends Figure { | ||
private final double firstLeg; | ||
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. this fields is marked as 'final' which means that it can't be changed after it's initialized. This is okay if you will never need to change this value, but if you want your objects to be mutable, you should remove the 'final' keyword. |
||
private final double secondLeg; | ||
|
||
public RightTriangle(Color 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.println("RightTriangle{" + "firstLeg=" + firstLeg | ||
+ ", secondLeg=" + secondLeg + ", color='" + getColor() + '\'' | ||
+ ", area=" + getArea() + '}'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package core.basesyntax; | ||
|
||
public class Square extends Figure { | ||
private final 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. this fields is marked as 'final' which means that it can't be changed after it's initialized. This is okay if you will never need to change this value, but if you want your objects to be mutable, you should remove the 'final' keyword. 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. This object is designed to be immutable, which does not change after it is created. In fact, I should have made the shape classes themselves final (Circle, Square and others). |
||
|
||
public Square(Color color, double side) { | ||
super(color); | ||
this.side = side; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return side * side; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Square{" + "side=" + side | ||
+ ", color='" + getColor() + '\'' | ||
+ ", area=" + getArea() + '}'); | ||
} | ||
} |
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.
this field is marked as 'final' which means that it can't be changed after it's initialized. This is okay if you will never need to change this value, but if you want your objects to be mutable, you should remove the 'final' keyword.