-
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
Solved the problem according to the principles of oop #1744
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,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface AreaCalculable { | ||
double calculateArea(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package core.basesyntax; | ||
|
||
public class Circle extends Figure { | ||
private double radius; | ||
|
||
public Circle(String color, double radius) { | ||
super(color); | ||
this.radius = radius; | ||
} | ||
|
||
@Override | ||
public double calculateArea() { | ||
return Math.PI * radius * radius; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: circle, area: " + calculateArea() + " sq. units, radius: " | ||
+ radius + " units, color: " + getColor()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class ColorSupplier { | ||
private static final String[] COLORS = {"red", "blue", "green", "yellow", "purple", "black"}; | ||
|
||
public String getRandomColor() { | ||
Random random = new Random(); | ||
return COLORS[random.nextInt(COLORS.length)]; | ||
} | ||
} |
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,12 @@ | ||
package core.basesyntax; | ||
|
||
public abstract class Figure implements AreaCalculable, Drawable { | ||
private String color; | ||
|
||
public Figure(String color) { | ||
this.color = color; | ||
} | ||
public String getColor() { | ||
return color; | ||
} | ||
} |
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 final ColorSupplier colorSupplier = new ColorSupplier(); | ||
private final Random random = new Random(); | ||
|
||
public Figure getRandomFigure() { | ||
int figureType = random.nextInt(5); | ||
|
||
switch (figureType) { | ||
case 0: | ||
return new Square(colorSupplier.getRandomColor(), random.nextDouble() * 10 + 1); | ||
case 1: | ||
return new Rectangle(colorSupplier.getRandomColor(), random.nextDouble() * 10 + 1, | ||
random.nextDouble() * 10 + 1); | ||
case 2: | ||
return new RightTriangle(colorSupplier.getRandomColor(), random.nextDouble() * 10 | ||
+ 1, random.nextDouble() * 10 + 1); | ||
case 3: | ||
return new Circle(colorSupplier.getRandomColor(), random.nextDouble() * 10 + 1); | ||
case 4: | ||
return new IsoscelesTrapezoid(colorSupplier.getRandomColor(), random.nextDouble() | ||
* 10 + 1, random.nextDouble() * 10 + 1, random.nextDouble() * 10 + 1); | ||
default: | ||
return getDefaultFigure(); | ||
} | ||
} | ||
|
||
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,27 @@ | ||
package core.basesyntax; | ||
|
||
public 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 calculateArea() { | ||
return 0.5 * (base1 + base2) * height; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Shape: isosceles trapezoid, area: " + calculateArea() | ||
+ " square units, first base: " + base1 + " units, second base: " + base2 | ||
+ " units, height: " + height + " units, color: " + 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,21 @@ | ||
package core.basesyntax; | ||
|
||
/** | ||
* Feel free to remove this class and create your own. | ||
*/ | ||
public class Main { | ||
public static void main(String[] args) { | ||
FigureSupplier figureSupplier = new FigureSupplier(); | ||
Figure[] figures = new Figure[6]; | ||
|
||
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 first loop is missing the logic to populate the first half of the |
||
figures[i] = figureSupplier.getRandomFigure(); | ||
} | ||
for (int i = figures.length / 2; i < figures.length; i++) { | ||
figures[i] = figureSupplier.getDefaultFigure(); | ||
} | ||
for (Figure figure : figures) { | ||
figure.draw(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Figure { | ||
private double width; | ||
private double height; | ||
|
||
public Rectangle(String color, double width, double height) { | ||
super(color); | ||
this.width = width; | ||
this.height = height; | ||
} | ||
|
||
@Override | ||
public double calculateArea() { | ||
return width * height; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Shape: rectangle, area: " + calculateArea() + "square units width: " | ||
+ width + "height units: " + height + "color units: " + getColor()); | ||
Comment on lines
+20
to
+21
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. There are missing spaces in the output string of the |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package core.basesyntax; | ||
|
||
public 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 calculateArea() { | ||
return 0.5 * firstLeg * secondLeg; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Shape: right triangle, area: " + calculateArea() | ||
+ " square units, first side: " + firstLeg + " units, second side: " | ||
+ secondLeg + " units, color: " + getColor()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package core.basesyntax; | ||
|
||
public class Square extends Figure { | ||
private double side; | ||
|
||
public Square(String color,double side) { | ||
super(color); | ||
this.side = side; | ||
} | ||
|
||
@Override | ||
public double calculateArea() { | ||
return side * side; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: square, area: " + calculateArea() + "square units, side: " | ||
+ side + " color, units" + getColor()); | ||
Comment on lines
+18
to
+19
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. There are missing spaces in the output string of the |
||
} | ||
} |
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.
Consider defining a constant for the number of figure types (5) to avoid using magic numbers. This will make your code more readable and easier to maintain. For example, you could define
private static final int FIGURE_TYPES_COUNT = 5;
and use it here.