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

Done #1775

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open

Done #1775

Show file tree
Hide file tree
Changes from 3 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
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax;

import java.lang.Math;

public class Circle extends Figure implements FigureMethods {
private final float radius;

public Circle(Colors color, float radius) {
super(color, "Circle");

Choose a reason for hiding this comment

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

Suggested change
super(color, "Circle");
super(color);

this.radius = radius;
}

@Override
public double area() {
return Math.PI * Math.pow(radius, 2);
}

@Override
public void draw() {
System.out.println("Figure: " + getName() + ", area: "
+ area() + " sq. units, radius: "
+ radius + " units, color: " + getColor());
}
}
11 changes: 11 additions & 0 deletions src/main/java/core/basesyntax/ColorSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package core.basesyntax;

import java.util.Random;

public class ColorSupplier {
private final static Random picker = new Random();

public static Colors getRandomColor() {
return Colors.values()[picker.nextInt(Colors.values().length)];
}
}
9 changes: 9 additions & 0 deletions src/main/java/core/basesyntax/Colors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package core.basesyntax;

public enum Colors {

Choose a reason for hiding this comment

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

Suggested change
public enum Colors {
public enum Color {

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

public abstract class Figure {
private Colors color;
private String name;

public abstract void draw();

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

public Colors getColor() {
return this.color;
}

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

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/FigureMethods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

public interface FigureMethods {
double area();
}
21 changes: 21 additions & 0 deletions src/main/java/core/basesyntax/FigureSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.basesyntax;

import java.util.Random;

public class FigureSupplier extends ColorSupplier {

Choose a reason for hiding this comment

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

Avoid extending ColorSupplier as it doesn't provide any additional benefit in this context. Consider using composition instead of inheritance if you need to use methods from ColorSupplier.

private final static Random picker = new Random();

Choose a reason for hiding this comment

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

Consider defining the number of figure types as a constant for better readability and maintainability. For example, private static final int FIGURE_TYPES_COUNT = 4;.


public static Figure getRandomFigure() {
return switch(picker.nextInt(4)) {
case 0 -> new Circle(getRandomColor(), picker.nextInt(1, 50));
case 1 -> new Rectangle(getRandomColor(), picker.nextInt(1, 50), picker.nextInt(1, 50));
case 2 -> new RightTriangle(getRandomColor(), picker.nextInt(1, 50), picker.nextInt(1, 50));
case 3 -> new Square(getRandomColor(), picker.nextInt(1, 50));
default -> new Circle(Colors.WHITE, 10);

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 is redundant because the picker.nextInt(randomNum) will always return a value between 0 and 3. You can safely remove the default case.

Choose a reason for hiding this comment

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

IsoscelesTrapesoid missed

};

Choose a reason for hiding this comment

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

Replace the magic number 4 with a constant, such as FIGURE_TYPES_COUNT, to improve code readability and maintainability.

}

Choose a reason for hiding this comment

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

Avoid using static methods. Instead, create an instance of FigureSupplier and call the method on that instance. This aligns with the checklist's recommendation to avoid static methods.


public static Figure getDefaultFigure() {
return new Circle(Colors.WHITE, 10);

Choose a reason for hiding this comment

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

the same

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

This file was deleted.

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

public class IsoscelesTrapezoid extends Figure implements FigureMethods {
private final float firstBase;
private final float secondBase;
private final float height;

public IsoscelesTrapezoid(Colors color, float firstBase, float secondBase, float height) {
super(color, "IsoscelesTrapezoid");
this.firstBase = firstBase;
this.secondBase = secondBase;
this.height = height;
}

@Override
public double area() {
return (firstBase + secondBase) * height;

Choose a reason for hiding this comment

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

The formula for the area of an isosceles trapezoid is incorrect. It should be ((firstBase + secondBase) / 2) * height to correctly calculate the area.

}

@Override
public void draw() {
System.out.println("Figure: " + getName()
+ ", area: " + area() + " sq. units, firstBase: "
+ firstBase + " units, secondBase: "
+ secondBase + " units, height: "
+ height + " units, color: " + getColor());
}
}
27 changes: 27 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package core.basesyntax;

public class Main {
public static void main(String args[]) {

Choose a reason for hiding this comment

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

It's a common convention to declare the main method parameter as String[] args instead of String args[]. Consider changing it for consistency.

printArray();

Choose a reason for hiding this comment

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

redundant separate method, create Figure[] here, 6 - should be constant, use one for loop for this task

}

public static Figure[] listOfRandomFigures() {
Figure[] array = new Figure[6];

for(int i = 0; i < 3; i++) {
array[i] = FigureSupplier.getRandomFigure();
}

for(int i = 3; i < 6; i++) {
array[i] = FigureSupplier.getDefaultFigure();
}

return array;
}

public static void printArray() {
for(Figure i : listOfRandomFigures()) {
i.draw();
}
}

Choose a reason for hiding this comment

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

Avoid using static methods for listOfRandomFigures and printArray. Consider creating an instance of Main and calling these methods on that instance, aligning with the checklist's recommendation to avoid static methods.

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

public class Rectangle extends Figure implements FigureMethods {
private final float firstSide;
private final float secondSide;

public Rectangle(Colors color, float firstSide, float secondSide) {
super(color, "Rectangle");
this.firstSide = firstSide;
this.secondSide = secondSide;
}

@Override
public double area() {
return firstSide * secondSide;
}

@Override
public void draw() {
System.out.println("Figure: " + getName() + ", area: "
+ area() + " sq. units, firstSide: "
+ firstSide + " units, secondSide: "
+ secondSide + " units, color: " + getColor());
}
}
25 changes: 25 additions & 0 deletions src/main/java/core/basesyntax/RightTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package core.basesyntax;

public class RightTriangle extends Figure implements FigureMethods {
private final float firstLeg;
private final float secondLeg;

public RightTriangle(Colors color, float firstLeg, float secondLeg) {
super(color, "RightTriangle");
this.firstLeg = firstLeg;
this.secondLeg = secondLeg;
}

@Override
public double area() {
return (firstLeg * secondLeg) / 2;
}

@Override
public void draw() {
System.out.println("Figure: " + getName()
+ ", area: " + area() + " sq. units, firstLeg: "
+ firstLeg + " units, secondLeg: "
+ secondLeg + " units, color: " + getColor());
}
}
22 changes: 22 additions & 0 deletions src/main/java/core/basesyntax/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package core.basesyntax;

public class Square extends Figure implements FigureMethods {
private final float side;

public Square(Colors color, float side) {
super(color, "Square");
this.side = side;
}

@Override
public double area() {
return side * side;
}

@Override
public void draw() {
System.out.println("Figure: " + getName() + ", area: "
+ area() + " sq. units, side: "
+ side + " units, color: " + getColor());
}
}
Loading