-
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 1 commit
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 Area { | ||
double getArea(); | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,23 @@ | ||||||||||||
package core.basesyntax; | ||||||||||||
|
||||||||||||
public class Circle extends Figure { | ||||||||||||
private int radius; | ||||||||||||
|
||||||||||||
public Circle(int radius,String color) { | ||||||||||||
this.radius = radius; | ||||||||||||
setColor(color); | ||||||||||||
getArea(); | ||||||||||||
} | ||||||||||||
|
||||||||||||
@Override | ||||||||||||
public double getArea() { | ||||||||||||
return Math.PI * radius * radius; | ||||||||||||
} | ||||||||||||
|
||||||||||||
@Override | ||||||||||||
public String toString() { | ||||||||||||
System.out.println("Circle{" + "radius=" + radius | ||||||||||||
+ ", color='" + getColor() + '\'' + ", area=" + getArea() + '}'); | ||||||||||||
return ""; | ||||||||||||
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.
Suggested change
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,8 @@ | ||
package core.basesyntax; | ||
|
||
public enum Color { | ||
RED, | ||
WHITE, | ||
BLACK, | ||
BLUE | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class ColorSupplier { | ||
public String getRandomColor() { | ||
Random rand = new Random(); | ||
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. |
||
return Color.values()[rand.nextInt(Color.values().length)].toString(); | ||
Paradoxallist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package core.basesyntax; | ||
|
||
public abstract class Figure implements Area { | ||
private String color; | ||
|
||
public String getColor() { | ||
return color; | ||
} | ||
|
||
public void setColor(String color) { | ||
this.color = color; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package core.basesyntax; | ||
|
||
public enum FigureList { | ||
Circle, | ||
IsoscelesTrapezoid, | ||
Rectangle, | ||
RightTriangle, | ||
Square | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class FigureSupplier { | ||
|
||
public Figure getRandomFigure() { | ||
Random random = new Random(); | ||
ColorSupplier colorSupplier = new ColorSupplier(); | ||
FigureList randomFigure = FigureList.values()[random.nextInt(FigureList.values().length)]; | ||
int randomRange = 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. should be class level constant.
|
||
switch (randomFigure) { | ||
case Circle: | ||
return new Circle(random.nextInt(randomRange),colorSupplier.getRandomColor()); | ||
case Square: | ||
return new Square(random.nextInt(randomRange),colorSupplier.getRandomColor()); | ||
case Rectangle: | ||
return new Rectangle(random.nextInt(randomRange), | ||
random.nextInt(randomRange), | ||
colorSupplier.getRandomColor()); | ||
case RightTriangle: | ||
return new RightTriangle(random.nextInt(randomRange), | ||
random.nextInt(randomRange), | ||
colorSupplier.getRandomColor()); | ||
case IsoscelesTrapezoid: | ||
return new IsoscelesTrapezoid(random.nextInt(randomRange), | ||
random.nextInt(randomRange), | ||
random.nextInt(randomRange), | ||
colorSupplier.getRandomColor()); | ||
default: | ||
return new Circle(10,Color.WHITE.toString()); | ||
} | ||
} | ||
|
||
public Figure getDefaultFigure() { | ||
return new Circle(10,Color.WHITE.toString()); | ||
} | ||
|
||
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 |
||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package core.basesyntax; | ||
|
||
public class IsoscelesTrapezoid extends Figure { | ||
private double upSide; | ||
private double downSide; | ||
private double height; | ||
|
||
public IsoscelesTrapezoid(double upSide, double downSide, double height, String color) { | ||
this.upSide = upSide; | ||
this.downSide = downSide; | ||
this.height = height; | ||
setColor(color); | ||
getArea(); | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return (upSide + downSide) * height / 2; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
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() |
||
return ""; | ||
} | ||
} |
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(10,Color.WHITE.toString()); | ||
figures[3] = new Square(5,Color.BLACK.toString()); | ||
for (Figure figure : figures) { | ||
figure.toString(); | ||
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 adding interface instead of overriding toString |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Figure { | ||
private double firstLeg; | ||
private double secondLeg; | ||
|
||
public Rectangle(double firstLeg, double secondLeg, String color) { | ||
this.firstLeg = firstLeg; | ||
this.secondLeg = secondLeg; | ||
setColor(color); | ||
getArea(); | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return firstLeg * secondLeg; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
System.out.println("Rectangle{" + "firstLeg=" + firstLeg | ||
+ ", secondLeg=" + secondLeg + ", color='" + getColor() + '\'' | ||
+ ", area=" + getArea() + '}'); | ||
return ""; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package core.basesyntax; | ||
|
||
public class RightTriangle extends Figure { | ||
private double firstLeg; | ||
private double secondLeg; | ||
|
||
public RightTriangle(double firstLeg, double secondLeg, String color) { | ||
this.firstLeg = firstLeg; | ||
this.secondLeg = secondLeg; | ||
setColor(color); | ||
getArea(); | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return firstLeg * secondLeg / 2; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
System.out.println("RightTriangle{" + "firstLeg=" + firstLeg | ||
+ ", secondLeg=" + secondLeg + ", color='" + getColor() + '\'' | ||
+ ", area=" + getArea() + '}'); | ||
return ""; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package core.basesyntax; | ||
|
||
public class Square extends Figure { | ||
private double side; | ||
|
||
public Square(double side,String color) { | ||
this.side = side; | ||
setColor(color); | ||
getArea(); | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return side * side; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
System.out.println("Square{" + "side=" + side | ||
+ ", color='" + getColor() + '\'' | ||
+ ", area=" + getArea() + '}'); | ||
return ""; | ||
} | ||
} |
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.