-
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
jv-oop-advanced the task is completed #1720
base: master
Are you sure you want to change the base?
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,26 @@ | ||
package core.basesyntax; | ||
|
||
public class Circle extends Default { | ||
private double size; | ||
|
||
public Circle(String color, double size) { | ||
super(color); | ||
this.size = size; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return size * size; | ||
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 formula for the area of a circle is incorrect. It should be |
||
} | ||
|
||
@Override | ||
public double getProperty() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Circle: area: " + Math.round(getArea()) | ||
+ ", Radius: " + Math.round(getProperty()) + ", Color: " + getColor()); | ||
} | ||
} |
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 | ||
} |
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); | ||
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. 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(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package core.basesyntax; | ||
|
||
public class Default implements Figure { | ||
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 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
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. 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
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. 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
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 '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; | ||
} | ||
|
||
} |
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
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 methods |
||
} |
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); | ||
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 random number should be generated inside the |
||
|
||
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
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. 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); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package core.basesyntax; | ||
|
||
public class IsoscelesTrapezoid extends Default { | ||
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 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
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 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. 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 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)); | ||
} | ||
} |
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]; | ||
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. You should create several random Figures, so you will use a loop. Please don't create a |
||
|
||
for (int i = 0; i < figures.length / 2; i++) { | ||
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 loop correctly uses |
||
figures[i] = figureSupplier.getRandomFigure(); | ||
} | ||
|
||
for (int i = figures.length / 2; i < figures.length; i++) { | ||
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 loop correctly starts from |
||
figures[i] = figureSupplier.getDefaultFigure(); | ||
} | ||
|
||
for (Figure figure : figures) { | ||
figure.draw(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Default { | ||
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 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
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 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
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 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
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 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
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 '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. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package core.basesyntax; | ||
|
||
public class RightTriangle extends Default { | ||
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 class |
||
private double size; | ||
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 variable |
||
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; | ||
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 calculation of the area for a right triangle should be |
||
} | ||
|
||
@Override | ||
public double getProperty() { | ||
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 method name |
||
return 0.5 * firstLeg * 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 calculation here is correct for the area of a right triangle, but it should be in the |
||
} | ||
|
||
@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)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package core.basesyntax; | ||
|
||
public class Square extends Default { | ||
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 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) { | ||
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. 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() { | ||
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 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())); | ||
} | ||
} |
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 class
Circle
should not extendDefault
. Consider using an interface to define the behavior instead of using an abstract class.