Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version><![CDATA[4.13.1]]></version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/core/basesyntax/Circle.java
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();
}
}

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 a getArea() 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 formula Math.PI * radius * radius.

13 changes: 13 additions & 0 deletions src/main/java/core/basesyntax/ColorSupplier.java
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)];

}
}

17 changes: 17 additions & 0 deletions src/main/java/core/basesyntax/Figure.java
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;
}
}
33 changes: 33 additions & 0 deletions src/main/java/core/basesyntax/FigureSupplier.java
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();
};

}

}



8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

23 changes: 23 additions & 0 deletions src/main/java/core/basesyntax/IsoscelesTrapezoid.java
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();
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The IsoscelesTrapezoid class should implement a getArea() method to calculate the area of the trapezoid. The formula for the area of an isosceles trapezoid is ((base1 + base2) / 2) * height. Implement this method to comply with the task requirements.

16 changes: 16 additions & 0 deletions src/main/java/core/basesyntax/Main.java
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());
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/core/basesyntax/Rectangle.java
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();

}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Rectangle class should implement a getArea() method to calculate the area of the rectangle. The formula for the area of a rectangle is width * height. Implement this method to comply with the task requirements.

20 changes: 20 additions & 0 deletions src/main/java/core/basesyntax/RightTriangle.java
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;
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RightTriangle class should implement a getArea() method to calculate the area of the triangle. The formula for the area of a right triangle is (firstLeg * secondLeg) / 2. Implement this method to comply with the task requirements.

20 changes: 20 additions & 0 deletions src/main/java/core/basesyntax/Square.java
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;
}
}
Loading