-
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
add Figures with OOP #1773
base: master
Are you sure you want to change the base?
add Figures with OOP #1773
Changes from 1 commit
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,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface AreaCalculator { | ||
double getArea(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package core.basesyntax; | ||
|
||
class Circle extends Figure { | ||
private double radius; | ||
|
||
public Circle(String color, double radius) { | ||
super(color); | ||
this.radius = radius; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return Math.PI * radius * radius; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: circle, area: " | ||
+ getArea() + " sq. units, radius: " | ||
+ this.radius + " units, color: " + this.color); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package core.basesyntax; | ||
|
||
public enum Colors { | ||
blue, | ||
red, | ||
green, | ||
white, | ||
black, | ||
yellow | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface Drawable { | ||
void draw(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package core.basesyntax; | ||
|
||
abstract class Figure implements Drawable, AreaCalculator { | ||
protected String color; | ||
|
||
public Figure() { | ||
} | ||
|
||
public Figure(String color) { | ||
this.color = color; | ||
} | ||
|
||
public String getColor() { | ||
return color; | ||
} | ||
|
||
public void setColor(String color) { | ||
this.color = color; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class FigureSupplier { | ||
private Random random = new Random(); | ||
|
||
public String getRandomColor() { | ||
int index = random.nextInt(Colors.values().length); | ||
Colors randomColors = Colors.values()[index]; | ||
return randomColors.toString(); | ||
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. Use 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. Use |
||
} | ||
|
||
public Figure getRandomFigure() { | ||
int index = random.nextInt(Figures.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 defining a constant for the number of figure types to avoid using magic numbers. For example, |
||
Figures randomFigure = Figures.values()[index]; | ||
|
||
switch (randomFigure) { | ||
case Circle: | ||
return new Circle(getRandomColor(), random.nextInt(1, 10)); | ||
case Square: | ||
return new Square(getRandomColor(), random.nextInt(1, 10)); | ||
case Rectangle: | ||
return new Rectangle(getRandomColor(), random.nextInt(1, 10), | ||
random.nextInt(1, 10)); | ||
case RightTriangle: | ||
return new RightTriangle(getRandomColor(), random.nextInt(1, 10), | ||
random.nextInt(1, 10)); | ||
case IsoscelesTrapezoid: | ||
return new IsoscelesTrapezoid(getRandomColor(), random.nextInt(1, 10), | ||
random.nextInt(1, 10), random.nextInt(1, 10)); | ||
Comment on lines
+20
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. Avoid passing expressions directly into constructors. Assign the random values to variables first, then pass those variables to the constructors for better readability. |
||
default: | ||
return null; | ||
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 |
||
} | ||
} | ||
|
||
public Figure getDefaultFigure() { | ||
return new Circle("white", 10); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package core.basesyntax; | ||
|
||
public enum Figures { | ||
Square, | ||
Rectangle, | ||
RightTriangle, | ||
Circle, | ||
IsoscelesTrapezoid | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package core.basesyntax; | ||
|
||
class IsoscelesTrapezoid extends Figure { | ||
private double base1; | ||
private double base2; | ||
private 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 0.5 * (base1 + base2) * height; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: isosceles trapezoid, area: " | ||
+ getArea() + " sq. units, base1: " | ||
+ this.base1 + ", base2: " | ||
+ this.base2 + ", height: " | ||
+ this.height + " units, color: " | ||
+ this.color); | ||
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 using |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package core.basesyntax; | ||
|
||
class Rectangle extends Figure { | ||
private double length; | ||
private double width; | ||
|
||
public Rectangle(String color, double length, double width) { | ||
super(color); | ||
this.length = length; | ||
this.width = width; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return length * width; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: rectangle, area: " | ||
+ getArea() + " sq. units, length: " | ||
+ this.length + ", width: " + this.width + " units, color: " + this.color); | ||
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 using |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package core.basesyntax; | ||
|
||
class RightTriangle extends Figure { | ||
private double firstLeg; | ||
private double secondLeg; | ||
|
||
public RightTriangle(String color, double firstLeg, double secondLeg) { | ||
super(color); | ||
this.firstLeg = firstLeg; | ||
this.secondLeg = secondLeg; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return 0.5 * firstLeg * secondLeg; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: right triangle, area: " | ||
+ getArea() + " sq. units, firstLeg: " | ||
+ this.firstLeg + ", secondLeg: " | ||
+ this.secondLeg + " units, color: " | ||
+ this.color); | ||
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 using |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package core.basesyntax; | ||
|
||
public class Square extends Figure { | ||
|
||
private double site; | ||
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 renaming the |
||
|
||
public Square() { | ||
} | ||
|
||
public Square(String color, int site) { | ||
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 changing the parameter type of |
||
super(color); | ||
this.site = site; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return this.site * this.site; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: square, area: " | ||
+ getArea() + " sq. units, side: " | ||
+ this.site + " units, color: " + this.color); | ||
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 using |
||
} | ||
} |
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.
It's a good practice to use the getter method
getColor()
to access thecolor
field instead of accessing it directly. This ensures better encapsulation and consistency with OOP principles.