-
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
Realized the creation of a generator of geometric shapes with random … #1753
Changes from all commits
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,22 @@ | ||
package core.basesyntax; | ||
|
||
public class Circle extends Figure { | ||
private double radius; | ||
|
||
public Circle(String color, double radius) { | ||
super(color); | ||
this.radius = radius; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return Math.PI * radius * radius; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: circle, area: " + getArea() | ||
+ " radius: " + radius + " color " + color); | ||
|
||
} | ||
} |
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 static final Random random = new Random(); | ||
private static final String[] Colors = {"red", "blue", "yellow", "green", "purple", "black"}; | ||
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 array |
||
|
||
public String getColorSupplier() { | ||
return Colors[random.nextInt(Colors.length)]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package core.basesyntax; | ||
|
||
public interface Drawable { | ||
|
||
void draw(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package core.basesyntax; | ||
|
||
public abstract class Figure implements Drawable, Measurable { | ||
protected String color; | ||
|
||
public Figure(String color) { | ||
this.color = color; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
|
||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return 0; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class FigureSupplier { | ||
private static final Random random2 = 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. The variable |
||
private static final ColorSupplier COLOR_SUPPLIER = new ColorSupplier(); | ||
|
||
public Figure getRandomFigure() { | ||
switch (random2.nextInt(5)) { | ||
case 0: | ||
return new Square(COLOR_SUPPLIER.getColorSupplier(), random2.nextDouble() * 10 + 1); | ||
case 1: | ||
return new Rectangle(COLOR_SUPPLIER.getColorSupplier(), random2.nextDouble() | ||
* 10 + 1, random2.nextDouble() * 10 + 1); | ||
case 2: | ||
return new RightTriangle(COLOR_SUPPLIER.getColorSupplier(), random2.nextDouble() | ||
* 10 + 1, random2.nextDouble() * 10 * 1); | ||
case 3: | ||
return new Circle(COLOR_SUPPLIER.getColorSupplier(), random2.nextDouble() * 10 + 1); | ||
default: | ||
return new IsoscelesTrapezoid(COLOR_SUPPLIER.getColorSupplier(),random2.nextDouble() | ||
* 10 + 1, random2.nextDouble() * 10 + 1, random2.nextDouble() * 10 + 1); | ||
} | ||
} | ||
|
||
public Figure getDefaultFigure() { | ||
return new Circle("white", 10); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package core.basesyntax; | ||
|
||
public class IsoscelesTrapezoid extends Figure { | ||
private double base1; | ||
private double base2; | ||
private double height; | ||
|
||
public IsoscelesTrapezoid(String color, double base1, double base2, double height) { | ||
super(color); | ||
this.base1 = base1; | ||
this.base2 = base2; | ||
this.height = height; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return 0.5 * (base1 + base2) * height; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println(" Figure: IsoscelesTrapezoid, area: " + getArea() + " base1: " | ||
+ base1 + " base2: " + base2 + " height: " + height + " color: " + color); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
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 < figures.length / 2; i++) { | ||
figures[i] = figureSupplier.getRandomFigure(); | ||
} | ||
for (int i = figures.length / 2; i < figures.length; i++) { | ||
figures[i] = figureSupplier.getDefaultFigure(); | ||
} | ||
for (Figure figure : figures) { | ||
figure.draw(); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax; | ||
|
||
public interface Measurable { | ||
|
||
double getArea(); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Figure { | ||
private double width; | ||
private double height; | ||
|
||
public Rectangle(String color, double width, double height) { | ||
super(color); | ||
this.width = width; | ||
this.width = height; | ||
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 mistake in the constructor: |
||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return width * height; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure rectangle, area: " + getArea() + " width" + width | ||
+ " ,height" + height + " 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 double fistLeg; | ||
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 variable name |
||
private double secondLeg; | ||
|
||
public RightTriangle(String color, double fistLeg, double secondLeg) { | ||
super(color); | ||
this.fistLeg = fistLeg; | ||
this.secondLeg = secondLeg; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return 0.5 * fistLeg * secondLeg; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure RightTriangle, area: " + getArea() + ", firstLeg " | ||
+ fistLeg + ", secondLeg " + secondLeg + " color " + color); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package core.basesyntax; | ||
|
||
public class Square extends Figure { | ||
|
||
private double side; | ||
|
||
public Square(String color, double side) { | ||
super(color); | ||
this.side = side; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return side * side; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: square, area: " | ||
+ getArea() + " one side: " + side + " 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.
The
color
field is accessed directly, assuming it is a protected or public field in theFigure
class. Ifcolor
is private inFigure
, you need to provide a getter method inFigure
to access it.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.
🔄 Исправьте проблемы, внесите изменения, а затем повторно запросите мою проверку с помощью Re-request reviewкнопки на вкладке «Рецензенты».
Где эта кнопка?