-
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
Figures are calculated! #1776
base: master
Are you sure you want to change the base?
Figures are calculated! #1776
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,18 @@ | ||
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 double getArea() { | ||
return Math.PI * radius * radius; | ||
} | ||
@Override | ||
public String toString() { | ||
return "Circle with radius: " + radius + ", color: " + getColor(); | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class ColorSupplier { | ||
private static final String[] COLORS = {"Red", "Blue", "Green", "Yellow", "Black"}; | ||
public String getRandomColor() { | ||
Random random = new Random(); | ||
return COLORS[random.nextInt(COLORS.length)]; | ||
|
||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package core.basesyntax; | ||
|
||
public abstract class Figure { | ||
private final String color; | ||
public Figure(String color) { | ||
this.color = color; | ||
} | ||
public String getColor() { | ||
return color; | ||
} | ||
public abstract double getArea(); | ||
|
||
@Override | ||
public String toString() { | ||
return "Figure with color: " + color; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class FigureSupplier { | ||
|
||
private static final double DEFAULT_RADIUS = 10.0; | ||
private static final Random RANDOM = new Random(); | ||
private final ColorSupplier colorSupplier = new ColorSupplier(); | ||
|
||
public Figure getDefaultFigure() { | ||
return new Circle("white", DEFAULT_RADIUS); | ||
} | ||
public Figure getRandomFigure() { | ||
int figureType = RANDOM.nextInt(5); | ||
String color = colorSupplier.getRandomColor(); | ||
|
||
return switch (figureType) { | ||
case 0 -> new Circle(color, RANDOM.nextDouble() * 10 + 1); | ||
case 1 -> new Square(color, RANDOM.nextDouble() * 10 + 1); | ||
case 2 -> new Rectangle(color, RANDOM.nextDouble() * 10 + 1, RANDOM.nextDouble() * 10 + 1); | ||
case 3 -> new RightTriangle(color, RANDOM.nextDouble() * 10 + 1, RANDOM.nextDouble() * 10 + 1); | ||
case 4 -> | ||
new IsoscelesTrapezoid(color, RANDOM.nextDouble() * 10 + 1, RANDOM.nextDouble() * 10 + 1, RANDOM.nextDouble() * 10 + 1); | ||
default -> getDefaultFigure(); | ||
}; | ||
|
||
} | ||
|
||
} | ||
|
||
|
||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package core.basesyntax; | ||
|
||
public class IsoscelesTrapezoid extends Figure { | ||
private final double base1; | ||
private final double base2; | ||
private final 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 ((base1 + base2) / 2) * height; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "IsoscelesTrapezoid with bases: " + base1 + " and " + base2 + ", height: " + height + ", color: " + getColor(); | ||
} | ||
} | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package core.basesyntax; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
FigureSupplier figureSupplier = new FigureSupplier(); | ||
Figure defaultFigure = figureSupplier.getDefaultFigure(); | ||
System.out.println("Default figure: " + defaultFigure); | ||
System.out.println("Area: " + defaultFigure.getArea()); | ||
|
||
for (int i = 0; i < 5; i++) { | ||
Figure randomFigure = figureSupplier.getRandomFigure(); | ||
System.out.println("Random figure: " + randomFigure); | ||
System.out.println("Area: " + randomFigure.getArea()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Figure { | ||
private final double width; | ||
private final double height; | ||
public Rectangle(String color, double width, double height) { | ||
super(color); | ||
this.width = width; | ||
this.height = height; | ||
} | ||
@Override | ||
public double getArea() { | ||
return width * height; | ||
} | ||
@Override | ||
public String toString() { | ||
return "Rectangle with width: " + width + ", height: " + height + ", color: " + getColor(); | ||
|
||
} | ||
} | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
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 double getArea() { | ||
return (firstLeg * secondLeg) / 2; | ||
} | ||
@Override | ||
public String toString() { | ||
return "RightTriangle: color = " + getColor() + ", firstLeg = " + firstLeg + ", secondLeg = " + secondLeg; | ||
} | ||
} | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package core.basesyntax; | ||
|
||
public class Square extends Figure { | ||
private final double sideLength; | ||
|
||
public Square(String color, double sideLength) { | ||
super(color); | ||
this.sideLength = sideLength; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return sideLength * sideLength; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Square: color = " + getColor() + ", sideLength = " + sideLength; | ||
} | ||
} |
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
Circle
class should implement agetArea()
method to calculate the area of the circle. According to the task description, all figures should have a method to obtain their area. You can calculate the area of a circle using the formulaMath.PI * radius * radius
.