-
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 classes, interface and abstract class for them, class ColorSupplier with public String getRandomColor(), class FigureSupplier with public Figure getRandomFigure() and getDefaultFigure(), also created main with random and default figures #1314
base: master
Are you sure you want to change the base?
Changes from 1 commit
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,16 @@ | ||
package core.basesyntax; | ||
|
||
public class Circle extends Figure { | ||
private final double radius; | ||
|
||
public Circle(String color, double radius) { | ||
super(color); | ||
this.radius = radius; | ||
} | ||
|
||
@Override | ||
public void printfInfoAboutFigure() { | ||
System.out.println("Figure: circle, area: " + Math.PI * radius * radius | ||
+ " sq.units, radius: " + radius + " units, color: " + getColor()); | ||
} | ||
} |
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 final Random random = new Random(); | ||
private final String[] colors = new String[]{"Blue", "Red", "Yellow", "Green", "Black"}; | ||
|
||
public String getRandomColor() { | ||
return colors[random.nextInt(colors.length)]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package core.basesyntax; | ||
|
||
public abstract class Figure implements FigureBehaviour { | ||
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. |
||
private final String color; | ||
|
||
public Figure(String color) { | ||
this.color = color; | ||
} | ||
|
||
public String getColor() { | ||
return color; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,5 @@ | ||||||||||||
package core.basesyntax; | ||||||||||||
|
||||||||||||
public interface FigureBehaviour { | ||||||||||||
void printfInfoAboutFigure(); | ||||||||||||
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
|
||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,35 @@ | ||||||||
package core.basesyntax; | ||||||||
|
||||||||
import java.util.Random; | ||||||||
|
||||||||
public class FigureSupplier { | ||||||||
private final Random random = new Random(); | ||||||||
private final ColorSupplier colorSupplier = new ColorSupplier(); | ||||||||
|
||||||||
public Figure getRandomFigure() { | ||||||||
String figureColor = colorSupplier.getRandomColor(); | ||||||||
int randomFigure = random.nextInt(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. use constants instead of numbers |
||||||||
|
||||||||
switch (randomFigure) { | ||||||||
case 0: | ||||||||
return new Circle(figureColor, random.nextDouble() * 9.0 + 1.0); | ||||||||
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. All magic numbers, strings, and symbols in your code should be constants. |
||||||||
case 1: | ||||||||
return new IsoscelesTrapezoid(figureColor, random.nextDouble() * 9.0 + 1.0, | ||||||||
random.nextDouble() * 9.0 + 1.0, random.nextDouble() * 9.0 + 1.0); | ||||||||
case 2: | ||||||||
return new Rectangle(figureColor, random.nextDouble() * 9.0 + 1.0, | ||||||||
random.nextDouble() * 9.0 + 1.0); | ||||||||
case 3: | ||||||||
return new RightTriangle(figureColor, random.nextDouble() * 9.0 + 1.0, | ||||||||
random.nextDouble() * 9.0 + 1.0); | ||||||||
case 4: | ||||||||
return new Square(figureColor, random.nextDouble() * 9.0 + 1.0); | ||||||||
default: | ||||||||
return null; | ||||||||
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
|
||||||||
} | ||||||||
} | ||||||||
|
||||||||
public Figure getDefaultFigure() { | ||||||||
return new Circle("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. All magic numbers, strings, and symbols in your code should be constants. |
||||||||
} | ||||||||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package core.basesyntax; | ||
|
||
public class IsoscelesTrapezoid extends Figure { | ||
private final double side1; | ||
private final double side2; | ||
private final double height; | ||
|
||
public IsoscelesTrapezoid(String color, double side1, double side2, double height) { | ||
super(color); | ||
this.side1 = side1; | ||
this.side2 = side2; | ||
this.height = height; | ||
} | ||
|
||
@Override | ||
public void printfInfoAboutFigure() { | ||
System.out.println("Figure: trapezoid, area: " + 0.5 * (side1 + side2) * height | ||
+ " sq.units, side1: " + side1 + " sq.units, side2: " + side2 | ||
+ " sq.units, height: " + height + " units, color: " + getColor()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package core.basesyntax; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
FigureSupplier figureSupplier = new FigureSupplier(); | ||
|
||
Figure[] figures = new Figure[6]; | ||
|
||
for (int i = 0; i < 3; i++) { | ||
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. use one loop |
||
figures[i] = figureSupplier.getRandomFigure(); | ||
} | ||
|
||
for (int i = 3; i < 6; i++) { | ||
figures[i] = figureSupplier.getDefaultFigure(); | ||
} | ||
|
||
for (Figure figure : figures) { | ||
figure.printfInfoAboutFigure(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Figure { | ||
private final double length; | ||
private final double width; | ||
|
||
public Rectangle(String color, double width, double length) { | ||
super(color); | ||
this.width = width; | ||
this.length = length; | ||
} | ||
|
||
@Override | ||
public void printfInfoAboutFigure() { | ||
System.out.println("Figure: rectangle, area: " + length * width + " sq.units, length: " | ||
+ length + " sq.units, width: " + width + " units, color: " + getColor()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package core.basesyntax; | ||
|
||
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 void printfInfoAboutFigure() { | ||
System.out.println("Figure: triangle, area: " + 0.5 * firstLeg * secondLeg | ||
+ " sq.units, firstLeg: " + firstLeg + " sq.units, secondLeg: " + secondLeg | ||
+ " units, color: " + getColor()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package core.basesyntax; | ||
|
||
public class Square extends Figure { | ||
private final double side; | ||
|
||
public Square(String color, double side) { | ||
super(color); | ||
this.side = side; | ||
} | ||
|
||
@Override | ||
public void printfInfoAboutFigure() { | ||
System.out.println("Figure: square, area: " + side * side + " sq.units, side: " | ||
+ side + " units, color: " + 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.
create
Enum Color
instead of String