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

jv-oop-advanced the task is completed #1720

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

public class Circle extends Default {

Choose a reason for hiding this comment

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

The class Circle should not extend Default. Consider using an interface to define the behavior instead of using an abstract class.

private double size;

public Circle(String color, double size) {
super(color);
this.size = size;
}

@Override
public double getArea() {
return size * size;

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 a circle is incorrect. It should be Math.PI * size * size to calculate the area using the radius.

}

@Override
public double getProperty() {
return size;
}

@Override
public void draw() {
System.out.println("Circle: area: " + Math.round(getArea())
+ ", Radius: " + Math.round(getProperty()) + ", Color: " + getColor());
}
}
9 changes: 9 additions & 0 deletions src/main/java/core/basesyntax/Color.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package core.basesyntax;

public enum Color {
WHITE,
GREEN,
BLUE,
YELLOW,
ORANGE
}
10 changes: 10 additions & 0 deletions src/main/java/core/basesyntax/ColorSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.basesyntax;

import java.util.Random;

public class ColorSupplier {
public String getRandomColor() {
int index = new Random().nextInt(Color.values().length);

Choose a reason for hiding this comment

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

Consider creating a single instance of Random as a class-level variable instead of creating a new instance each time the method is called. This aligns with the checklist item: 'Think about which variables should be local in the method and which should be class-level'.

return Color.values()[index].name();
}
}
29 changes: 29 additions & 0 deletions src/main/java/core/basesyntax/Default.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package core.basesyntax;

public class Default implements Figure {

Choose a reason for hiding this comment

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

The class name 'Default' is a verb, which is not recommended for class names. Consider using a noun that better describes the purpose of this class.

private String color;

public Default(String color) {
this.color = color;
}

@Override
public double getArea() {
return 0;
}
Comment on lines +11 to +13

Choose a reason for hiding this comment

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

Returning 0 as the area might not be meaningful. Consider implementing this method to return a more appropriate value or throw an exception if this class is not supposed to have an area.


@Override
public double getProperty() {
return 0;
}
Comment on lines +16 to +18

Choose a reason for hiding this comment

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

Returning 0 as a property value might not be meaningful. Consider implementing this method to return a more appropriate value or throw an exception if this class is not supposed to have a property.


@Override
public void draw() {

}
Comment on lines +21 to +23

Choose a reason for hiding this comment

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

The 'draw' method is empty. If this class is not supposed to implement drawing, consider throwing an UnsupportedOperationException or providing a meaningful implementation.


public String getColor() {
return color;
}

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

public interface Figure {
double getArea();

double getProperty();

void draw();
Comment on lines +4 to +8

Choose a reason for hiding this comment

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

The methods getArea() and draw() are conceptually different and should not be in the same interface. According to the checklist, all classes and interfaces should have a single purpose.

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

import java.util.Random;

public class FigureSupplier {
private ColorSupplier colorSupplier = new ColorSupplier();
private Random random = new Random();
private int randomNumber = random.nextInt(5);

Choose a reason for hiding this comment

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

The random number should be generated inside the getRandomFigure method to ensure a new random figure is generated each time the method is called. Consider moving randomNumber to be a local variable inside getRandomFigure.


public Figure getRandomFigure() {
switch (randomNumber) {
case 0:
return new Square(colorSupplier.getRandomColor(), random.nextDouble() * 10);
case 1:
return new Circle(colorSupplier.getRandomColor(), random.nextDouble() * 10);
case 2:
return new Rectangle(colorSupplier.getRandomColor(), random.nextDouble() * 10,
random.nextDouble() * 10, random.nextDouble() * 10);
case 3:
return new RightTriangle(colorSupplier.getRandomColor(), random.nextDouble() * 10,
random.nextDouble() * 10, random.nextDouble() * 10);
default:
return new IsoscelesTrapezoid(colorSupplier.getRandomColor(),
random.nextDouble() * 10,
random.nextDouble() * 10,
random.nextDouble() * 10,
random.nextDouble() * 10);
Comment on lines +13 to +27

Choose a reason for hiding this comment

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

Avoid passing expressions directly in the constructor for better readability. Create separate variables for the random values and pass them to the constructors.

}
}

public Figure getDefaultFigure() {
return new Circle("WHITE", 10);
}
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

public class IsoscelesTrapezoid extends Default {

Choose a reason for hiding this comment

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

The class IsoscelesTrapezoid should not extend Default. Consider extending a more appropriate class or implementing an interface that matches the intended behavior.

private double size;
private double side1;
private double side2;
private double length;

public IsoscelesTrapezoid(String color, double size, double side1,
double side2, double length) {
super(color);
this.size = size;
this.side1 = side1;
this.side2 = side2;
this.length = length;
}

@Override
public double getArea() {
return size * size;
}

@Override
public double getProperty() {
return 0.5 * (side1 * side2) * length;
Comment on lines +24 to +25

Choose a reason for hiding this comment

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

The method name getProperty() is not descriptive of its purpose. Consider renaming it to something more meaningful, like getPerimeter(), if it is intended to calculate the perimeter.

Choose a reason for hiding this comment

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

The formula used in getProperty() seems incorrect for calculating the perimeter or any other property of an isosceles trapezoid. Please review the formula to ensure it matches the intended calculation.

}

@Override
public void draw() {
System.out.println("IsoscelesTrapezoid: area: " + Math.round(getArea())
+ ", Property: " + Math.round(getProperty())
+ ", Color: " + getColor() + ", side1: " + Math.round(side1)
+ ", side2: " + Math.round(side2)
+ ", length: " + Math.round(length));
}
}
20 changes: 20 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package core.basesyntax;

public class Main {
public static void main(String[] args) {
FigureSupplier figureSupplier = new FigureSupplier();
Figure[] figures = new Figure[4];

Choose a reason for hiding this comment

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

You should create several random Figures, so you will use a loop. Please don't create a new FigureSupplier() inside the loop. However, the current implementation is correct as FigureSupplier is instantiated outside the loop.


for (int i = 0; i < figures.length / 2; i++) {

Choose a reason for hiding this comment

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

The loop correctly uses figures.length / 2 to fill half of the array with random figures.

figures[i] = figureSupplier.getRandomFigure();
}

for (int i = figures.length / 2; i < figures.length; i++) {

Choose a reason for hiding this comment

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

The loop correctly starts from figures.length / 2 to fill the remaining half of the array with default figures.

figures[i] = figureSupplier.getDefaultFigure();
}

for (Figure figure : figures) {
figure.draw();
}
}
}
33 changes: 33 additions & 0 deletions src/main/java/core/basesyntax/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package core.basesyntax;

public class Rectangle extends Default {

Choose a reason for hiding this comment

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

The class name 'Rectangle' suggests it should have properties related to a rectangle, such as 'length' and 'width', rather than 'side', 'firstLeg', and 'secondLeg'. Consider renaming these variables to better reflect the properties of a rectangle.

private double side;
private double firstLeg;
private double secondLeg;
Comment on lines +4 to +6

Choose a reason for hiding this comment

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

The variables 'side', 'firstLeg', and 'secondLeg' are not conceptually aligned with a rectangle's properties. Typically, a rectangle is defined by its 'length' and 'width'.


public Rectangle(String color, double side, double firstLeg, double secondLeg) {
super(color);
this.side = side;
this.firstLeg = firstLeg;
this.secondLeg = secondLeg;
}

@Override
public double getProperty() {
return firstLeg * secondLeg;
Comment on lines +16 to +17

Choose a reason for hiding this comment

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

The method 'getProperty()' seems to calculate the area of a rectangle using 'firstLeg' and 'secondLeg', which is inconsistent with the naming and purpose of the method. Consider renaming the method or adjusting its logic to align with its intended purpose.

}

@Override
public double getArea() {
return side * side;
Comment on lines +21 to +22

Choose a reason for hiding this comment

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

The method 'getArea()' calculates the area using 'side * side', which is incorrect for a rectangle. It should use 'length * width' or equivalent variables.

}

@Override
public void draw() {
System.out.println("Rectangle: area: " + Math.round(getArea())
+ " Property: " + Math.round(getProperty())
+ " Color: " + getColor()
+ " firstLeg: " + Math.round(firstLeg)
+ " secondLeg: " + Math.round(secondLeg));
Comment on lines +27 to +31

Choose a reason for hiding this comment

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

The 'draw()' method outputs 'Property' which is not a standard term for rectangle dimensions. Consider using 'Perimeter' or another appropriate term if that's the intended calculation.

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

public class RightTriangle extends Default {

Choose a reason for hiding this comment

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

The class RightTriangle should not extend Default if Default is an abstract class used to set behavior. Consider using an interface for behavior definition if applicable.

private double size;

Choose a reason for hiding this comment

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

The variable size seems redundant for a RightTriangle since the area is calculated using firstLeg and secondLeg. Consider removing it if it's not necessary.

private double firstLeg;
private double secondLeg;

public RightTriangle(String color, double size, double firstLeg, double secondLeg) {
super(color);
this.size = size;
this.firstLeg = firstLeg;
this.secondLeg = secondLeg;
}

@Override
public double getArea() {

return size * size;

Choose a reason for hiding this comment

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

The calculation of the area for a right triangle should be 0.5 * firstLeg * secondLeg instead of size * size. This seems to be an error in the logic.

}

@Override
public double getProperty() {

Choose a reason for hiding this comment

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

The method name getProperty() is not descriptive. Consider renaming it to something more meaningful like getPerimeter() if it calculates the perimeter, or another appropriate name.

return 0.5 * firstLeg * secondLeg;

Choose a reason for hiding this comment

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

The calculation here is correct for the area of a right triangle, but it should be in the getArea() method. Ensure that getProperty() is used for a different purpose or rename it.

}

@Override
public void draw() {
System.out.println("RightTriangle: area: " + Math.round(getArea())
+ ", Property: " + Math.round(getProperty())
+ ", Color: " + getColor()
+ ", firstLeg: " + Math.round(firstLeg)
+ ", secondLeg: " + Math.round(secondLeg));
}
}
27 changes: 27 additions & 0 deletions src/main/java/core/basesyntax/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package core.basesyntax;

public class Square extends Default {

Choose a reason for hiding this comment

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

The class name 'Square' is appropriate as it is a noun and represents a shape. However, ensure that the class 'Default' is correctly used and named, as it might not be descriptive enough.

private double size;

public Square(String color,double size) {

Choose a reason for hiding this comment

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

Consider adding a space after the comma in the constructor parameters for better readability: 'String color, double size'.

super(color);
this.size = size;
}

@Override
public double getProperty() {

Choose a reason for hiding this comment

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

The method name 'getProperty' is not very descriptive. Consider renaming it to something more specific like 'getPerimeter' to better reflect its purpose.

return 4 * size;
}

@Override
public double getArea() {
return size * size;
}

@Override
public void draw() {
System.out.println("Square: color = " + getColor()
+ ", size = " + Math.round(size)
+ ", Perimeter = " + Math.round(getProperty()));
}
}
Loading