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

created several Figure and color classes and their suppliers #1729

Open
wants to merge 9 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
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/AreaCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax;

public interface AreaCalculator {

int calculateArea();

}
32 changes: 32 additions & 0 deletions src/main/java/core/basesyntax/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package core.basesyntax;

public class Circle extends Figure {
private int radius;

public Circle(String color, String name, int radius) {
super(color, name);
this.radius = radius;
}

@Override
public int calculateArea() {
return (int) (radius * radius * Math.PI);
Comment on lines +12 to +13

Choose a reason for hiding this comment

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

The calculateArea method returns an int, but the area of a circle is a double value due to the use of Math.PI. Consider changing the return type to double to avoid loss of precision.


}

@Override
public String toString() {
return "Figure:"
+ "name: "
+ super.getName()
+ ", "
+ "area: "
+ calculateArea()
+ " sq. units, "
+ "radius: "
+ radius
+ ", "
+ "color: "
+ super.getColor();
}
}
11 changes: 11 additions & 0 deletions src/main/java/core/basesyntax/Color.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package core.basesyntax;

public enum Color {
YELLOW,
BLUE,
BLACK,
WHITE,
GREEN,
RED

}
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 {

public String getRandomColor() {
Color[] values = Color.values();
int randomIndex = new Random().nextInt(values.length);
return values[randomIndex].name();
Comment on lines +7 to +10

Choose a reason for hiding this comment

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

Consider changing the return type of getRandomColor from String to Color to maintain consistency with the Color enum. This will allow you to work directly with the Color type instead of converting it to a String.

}

}
51 changes: 51 additions & 0 deletions src/main/java/core/basesyntax/Figure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package core.basesyntax;

public class Figure implements AreaCalculator {

Choose a reason for hiding this comment

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

Consider making Figure an abstract class if it is intended to serve as a base class for other specific figure types like Circle. This would prevent direct instantiation of Figure and emphasize that it is meant to be extended.

private String color;
private String name;

public Figure(String color, String name) {
this.color = color;
this.name = name;
}

public Figure() {
}

public void draw() {
System.out.println(this);
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "Figure{"
+ "color='"
+ color
+ ", "
+ "name='"
+ name
+ '\''
+ '}';
}

@Override
public int calculateArea() {
return 0;
}
}
10 changes: 10 additions & 0 deletions src/main/java/core/basesyntax/FigureName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.basesyntax;

public enum FigureName {
CIRCLE,
ISOSCELES_TRAPEZOID,
RECTANGLE,
SQUARE,
RIGHT_TRIANGLE

}
58 changes: 58 additions & 0 deletions src/main/java/core/basesyntax/FigureSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package core.basesyntax;

import java.util.Random;

public class FigureSupplier {

public Figure getRandomFigure() {
Figure randomFigure;
int randomIndex = new Random().nextInt(5);
switch (randomIndex) {
case 0 -> randomFigure = getRandomCircle();
case 1 -> randomFigure = getIsoscelesTrapezoid();
case 2 -> randomFigure = getRandomRectangle();
case 3 -> randomFigure = getRightTriangle();
case 4 -> randomFigure = getRandomSquare();
default -> randomFigure = new Figure();

Choose a reason for hiding this comment

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

The default case in the switch statement creates a new Figure object. If Figure is intended to be an abstract class or should not be instantiated directly, consider removing this default case or handling it differently.

}
return randomFigure;
}

public Figure getDefaultFigure() {
return new Circle(Color.WHITE.name(), FigureName.CIRCLE.name(), 10);

Choose a reason for hiding this comment

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

Consider using the Color and FigureName enums directly instead of calling name(). This will maintain type safety and consistency with the enum usage.

}

private Circle getRandomCircle() {
return new Circle(new ColorSupplier().getRandomColor(),
FigureName.CIRCLE.name(),
Comment on lines +26 to +27

Choose a reason for hiding this comment

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

Consider using the Color and FigureName enums directly instead of calling name(). This will maintain type safety and consistency with the enum usage.

new Random().nextInt(101));
}

private IsoscelesTrapezoid getIsoscelesTrapezoid() {
return new IsoscelesTrapezoid(new ColorSupplier().getRandomColor(),
FigureName.ISOSCELES_TRAPEZOID.name(),
Comment on lines +32 to +33

Choose a reason for hiding this comment

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

Consider using the Color and FigureName enums directly instead of calling name(). This will maintain type safety and consistency with the enum usage.

new Random().nextInt(101),
new Random().nextInt(101),
new Random().nextInt(101));
}

private Rectangle getRandomRectangle() {
return new Rectangle(new ColorSupplier().getRandomColor(),
FigureName.RECTANGLE.name(),
Comment on lines +40 to +41

Choose a reason for hiding this comment

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

Consider using the Color and FigureName enums directly instead of calling name(). This will maintain type safety and consistency with the enum usage.

new Random().nextInt(101),
new Random().nextInt(101));
}

private RightTriangle getRightTriangle() {
return new RightTriangle(new ColorSupplier().getRandomColor(),
FigureName.RIGHT_TRIANGLE.name(),
Comment on lines +47 to +48

Choose a reason for hiding this comment

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

Consider using the Color and FigureName enums directly instead of calling name(). This will maintain type safety and consistency with the enum usage.

new Random().nextInt(101));
}

private Square getRandomSquare() {
return new Square(new ColorSupplier().getRandomColor(),
FigureName.SQUARE.name(),
Comment on lines +53 to +54

Choose a reason for hiding this comment

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

Consider using the Color and FigureName enums directly instead of calling name(). This will maintain type safety and consistency with the enum usage.

new Random().nextInt(101));
}

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

This file was deleted.

47 changes: 47 additions & 0 deletions src/main/java/core/basesyntax/IsoscelesTrapezoid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package core.basesyntax;

public class IsoscelesTrapezoid extends Figure {
private int bottomBase;
private int topBase;
private int side;

public IsoscelesTrapezoid(String color, String name, int bottomBase, int topBase, int side) {
super(color, name);
this.bottomBase = bottomBase;
this.topBase = topBase;
this.side = side;
}

@Override
public int calculateArea() {
return (int) (((bottomBase + topBase) / 2)
* (Math.sqrt((Math.pow(side, 2))
- Math.pow((((Math.pow((bottomBase - topBase), 2))
+ (Math.pow(side, 2)) - (Math.pow(side, 2)))
/ (2 * (bottomBase - topBase))), 2))));
Comment on lines +16 to +21

Choose a reason for hiding this comment

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

The calculateArea method uses integer division, which can lead to precision loss. Consider using double for calculations to ensure accuracy, especially when dealing with square roots and divisions.

}

@Override
public String toString() {
return "Figure:"
+ "name: "
+ super.getName()
+ ", "
+ "bottomBase: "
+ bottomBase
+ ", "
+ "topBase: "
+ topBase
+ ", "
+ "side: "
+ side
+ ", "
+ "area: "
+ calculateArea()
+ " sq. units, "
+ ", "
Comment on lines +41 to +42

Choose a reason for hiding this comment

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

There is an extra comma before the 'color' field in the toString method. Consider removing it for better formatting.

+ "color: "
+ super.getColor();
}

}
17 changes: 17 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package core.basesyntax;

public class Main {

public static void main(String[] args) {
Figure[] figures = new Figure[6];
for (int i = 0; i < 3; i++) {
figures[i] = new FigureSupplier().getRandomFigure();

Choose a reason for hiding this comment

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

Ensure that FigureSupplier.getRandomFigure() does not return a direct instance of Figure if Figure is intended to be a base class or abstract. This aligns with the suggestion to make Figure an abstract class.

}
for (int i = 3; i < 6; i++) {
figures[i] = new FigureSupplier().getDefaultFigure();
}
for (Figure figure : figures) {
figure.draw();
}
}
}
37 changes: 37 additions & 0 deletions src/main/java/core/basesyntax/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package core.basesyntax;

public class Rectangle extends Figure {
private int firstLag;
private int secondLag;
Comment on lines +4 to +5

Choose a reason for hiding this comment

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

The variable names firstLag and secondLag might be confusing. Consider renaming them to width and height or length and breadth for better clarity and understanding.


public Rectangle(String color, String name, int firstLag, int secondLag) {
super(color, name);
this.firstLag = firstLag;
this.secondLag = secondLag;
}

@Override
public int calculateArea() {
return firstLag * secondLag;
}

@Override
public String toString() {
return "Figure:"
+ "name: "
+ super.getName()
+ ", "
+ "area: "
+ calculateArea()
+ " sq. units, "
+ "firstLag: "
+ firstLag
+ ", "
+ "secondLag: "
+ secondLag
+ ", "
+ "color: "
+ super.getColor();
}

}
32 changes: 32 additions & 0 deletions src/main/java/core/basesyntax/RightTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package core.basesyntax;

public class RightTriangle extends Figure {
private int firstLag;

Choose a reason for hiding this comment

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

The variable name firstLag might be confusing. Consider renaming it to base or height for better clarity and understanding, especially since this is a right triangle.


public RightTriangle(String color, String name, int firstLag) {
super(color, name);
this.firstLag = firstLag;
}

@Override
public int calculateArea() {
return (int) (Math.pow(firstLag, 2) * (Math.sqrt(3)) / 4);
Comment on lines +12 to +13

Choose a reason for hiding this comment

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

The formula used in calculateArea is for an equilateral triangle, not a right triangle. For a right triangle, the area should be calculated as (base * height) / 2. Ensure that the correct dimensions are used for a right triangle.

}

@Override
public String toString() {
return "Figure:"
+ "name: "
+ super.getName()
+ ", "
+ "area: "
+ calculateArea()
+ " sq. units, "
+ "firstLag: "
+ firstLag
+ ", "
+ "color: "
+ super.getColor();
}

}
32 changes: 32 additions & 0 deletions src/main/java/core/basesyntax/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package core.basesyntax;

public class Square extends Figure {
private int firstLag;

Choose a reason for hiding this comment

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

The variable name firstLag might be confusing. Consider renaming it to side for better clarity and understanding, as it represents the side length of the square.


public Square(String color, String name, int firstLag) {
super(color, name);
this.firstLag = firstLag;
}

@Override
public int calculateArea() {
return firstLag * firstLag;
}

@Override
public String toString() {
return "Figure:"
+ "name: "
+ super.getName()
+ ", "
+ "area: "
+ calculateArea()
+ " sq. units, "
+ "firstLag: "
+ firstLag
+ ", "
+ "color: "
+ super.getColor();
}

}
Loading