-
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 a program that generate the array with random/default figures #1771
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,19 @@ | ||
package core.basesyntax; | ||
|
||
public class Circle extends Figure { | ||
private int radius; | ||
|
||
public Circle(int radius) { | ||
this.radius = radius; | ||
} | ||
|
||
public double getArea() { | ||
return Math.PI * radius * radius; | ||
} | ||
|
||
@Override | ||
public String printInformation() { | ||
return "Figure: Circle, area: " + getArea() | ||
+ "sq. units, radius: " + radius + " units, color: " + getColor(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package core.basesyntax; | ||
|
||
public enum Color { | ||
BLUE, | ||
YELLOW, | ||
BLACK, | ||
BROWN, | ||
PINK, | ||
GREEN, | ||
WHITE | ||
} |
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(); | ||
|
||
public String getRandomColor() { | ||
int index = random.nextInt(Color.values().length); | ||
return Color.values()[index].name(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package core.basesyntax; | ||
|
||
public class Ellipse extends Figure { | ||
private double lengthA; | ||
private double lengthB; | ||
|
||
public Ellipse(double sideA, double lengthB) { | ||
this.lengthA = sideA; | ||
this.lengthB = lengthB; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return Math.PI * lengthA * lengthB; | ||
} | ||
|
||
@Override | ||
public String printInformation() { | ||
return "Figure: Rectangle, area: " + getArea() | ||
+ "sq. units, lengthA: " + lengthA + " units, lengthB: " | ||
+ lengthB + " units, color: " + getColor(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package core.basesyntax; | ||
|
||
public abstract class Figure implements FigureSquare, FigureInformation { | ||
private String 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. consider creating a constructor for color and calling super() in subclasses |
||
public String getColor() { | ||
return color; | ||
} | ||
|
||
public void setColor(String color) { | ||
this.color = color; | ||
} | ||
|
||
public abstract double getArea(); | ||
|
||
public abstract String printInformation(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface FigureInformation { | ||
String printInformation(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface FigureSquare { | ||
double getArea(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class FigureSupplier { | ||
private static final int FIRST_MAX_NUMBER = 5; | ||
private static final int SECOND_MAX_NUMBER = 100; | ||
private static final int RADIUS = 10; | ||
private final Random random; | ||
private final ColorSupplier colorSupplier; | ||
|
||
public FigureSupplier(Random random, ColorSupplier colorSupplier) { | ||
this.random = random; | ||
this.colorSupplier = colorSupplier; | ||
} | ||
|
||
public Figure getRandomFigure() { | ||
int max1 = random.nextInt(SECOND_MAX_NUMBER) + 1; | ||
int max2 = random.nextInt(SECOND_MAX_NUMBER) + 1; | ||
Figure figure = switch (random.nextInt(FIRST_MAX_NUMBER) + 1) { | ||
case 1 -> new Rectangle(max1, max2); | ||
case 2 -> new RightTriangle(max1, max2); | ||
case 3 -> new Ellipse(max1, max2); | ||
case 4 -> new Square(max1); | ||
case 5 -> new IsoscelesTrapezoid(max1, max2, max2); | ||
default -> throw new IllegalArgumentException("Unexpected value"); | ||
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 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. IllegalState is more applicable here, cuz this method doesn't have arguments, problem can only occur via developer's mistake, misconfiguration of lengths. You can also make case 5 default |
||
}; | ||
figure.setColor(colorSupplier.getRandomColor()); | ||
return figure; | ||
} | ||
|
||
public Figure getDefaultFigure() { | ||
Figure figure = new Circle(RADIUS); | ||
figure.setColor("WHITE"); | ||
return figure; | ||
} | ||
} |
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 sideA; | ||
private double sideB; | ||
private double height; | ||
|
||
public IsoscelesTrapezoid(double sideA, double sideB, double height) { | ||
this.sideA = sideA; | ||
this.sideB = sideB; | ||
this.height = height; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return (sideA + sideB) / 2 * height; | ||
} | ||
|
||
@Override | ||
public String printInformation() { | ||
return "Figure: RightTriangle, area: " + getArea() | ||
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 |
||
+ "sq. units, sideA: " + sideA + " units, sideB: " + sideB + " units, height: " | ||
+ height + " units, color: " + getColor(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class MainApp { | ||
public static void main(String[] args) { | ||
ColorSupplier colorSupplier = new ColorSupplier(); | ||
Random random = new Random(); | ||
|
||
FigureSupplier figureSupplier = new FigureSupplier(random, colorSupplier); | ||
Figure[] figures = new Figure[6]; | ||
|
||
for (int i = 0; i < figures.length; i++) { | ||
if (i < figures.length / 2) { | ||
figures[i] = figureSupplier.getRandomFigure(); | ||
System.out.println(figures[i].printInformation()); | ||
} else { | ||
figures[i] = figureSupplier.getDefaultFigure(); | ||
System.out.println(figures[i].printInformation()); | ||
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. extract common part from if-else |
||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Figure { | ||
private double sideA; | ||
private double sideB; | ||
|
||
public Rectangle(double sideA, double sideB) { | ||
this.sideA = sideA; | ||
this.sideB = sideB; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return sideA * sideB; | ||
} | ||
|
||
@Override | ||
public String printInformation() { | ||
return "Figure: Rectangle, area: " + getArea() | ||
+ "sq. units, sideA: " + sideA + " units, sideB: " | ||
+ sideB + " units, color: " + getColor(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package core.basesyntax; | ||
|
||
public class RightTriangle extends Figure { | ||
private double base; | ||
private double height; | ||
|
||
public RightTriangle(double base, double height) { | ||
this.base = base; | ||
this.height = height; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return base * height / 2; | ||
} | ||
|
||
@Override | ||
public String printInformation() { | ||
return "Figure: RightTriangle, area: " + getArea() | ||
+ "sq. units, base: " + base + " units, height: " | ||
+ height + " units, color: " + getColor(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package core.basesyntax; | ||
|
||
public class Square extends Figure { | ||
private int side; | ||
|
||
public Square(int side) { | ||
this.side = side; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return side * side; | ||
} | ||
|
||
@Override | ||
public String printInformation() { | ||
return "Figure: Square, area: " + getArea() | ||
+ "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.
The
printInformation
method incorrectly refers to the figure as a 'Rectangle'. It should be 'Ellipse' to accurately describe the figure type.