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

make task #1311

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/Area.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

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

public class Circle extends Figure {
private int radius;

public Circle(int radius,String color) {
this.radius = radius;
setColor(color);
getArea();
Copy link

Choose a reason for hiding this comment

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

Suggested change
this.radius = radius;
setColor(color);
getArea();
super(color);
this.radius = radius;

}

@Override
public double getArea() {
return Math.PI * radius * radius;
}

@Override
public String toString() {
System.out.println("Circle{" + "radius=" + radius
+ ", color='" + getColor() + '\'' + ", area=" + getArea() + '}');
return "";

Choose a reason for hiding this comment

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

Suggested change
System.out.println("Circle{" + "radius=" + radius
+ ", color='" + getColor() + '\'' + ", area=" + getArea() + '}');
return "";
return "Circle{" + "radius=" + radius
+ ", color='" + getColor() + '\'' + ", area=" + getArea() + '}';

Choose a reason for hiding this comment

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

behaviour - we can obtain the area of any figure and are able to draw it. To 'draw' means to print out all information about a figure using System.out.println() (you shouldn't override toString() method for this).

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

public enum Color {
RED,
WHITE,
BLACK,
BLUE
}
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() {
Random rand = new Random();
Copy link

Choose a reason for hiding this comment

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

image

return Color.values()[rand.nextInt(Color.values().length)].toString();
Paradoxallist marked this conversation as resolved.
Show resolved Hide resolved
}
}
13 changes: 13 additions & 0 deletions src/main/java/core/basesyntax/Figure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package core.basesyntax;

public abstract class Figure implements Area {
private String color;

public String getColor() {
return color;
}

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

public enum FigureList {
Circle,
IsoscelesTrapezoid,
Rectangle,
RightTriangle,
Square
}
39 changes: 39 additions & 0 deletions src/main/java/core/basesyntax/FigureSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package core.basesyntax;

import java.util.Random;

public class FigureSupplier {

public Figure getRandomFigure() {
Random random = new Random();
ColorSupplier colorSupplier = new ColorSupplier();
FigureList randomFigure = FigureList.values()[random.nextInt(FigureList.values().length)];
int randomRange = 100;

Choose a reason for hiding this comment

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

should be class level constant.

randomRange is misleading name

switch (randomFigure) {
case Circle:
return new Circle(random.nextInt(randomRange),colorSupplier.getRandomColor());
case Square:
return new Square(random.nextInt(randomRange),colorSupplier.getRandomColor());
case Rectangle:
return new Rectangle(random.nextInt(randomRange),
random.nextInt(randomRange),
colorSupplier.getRandomColor());
case RightTriangle:
return new RightTriangle(random.nextInt(randomRange),
random.nextInt(randomRange),
colorSupplier.getRandomColor());
case IsoscelesTrapezoid:
return new IsoscelesTrapezoid(random.nextInt(randomRange),
random.nextInt(randomRange),
random.nextInt(randomRange),
colorSupplier.getRandomColor());
default:
return new Circle(10,Color.WHITE.toString());
}
}

public Figure getDefaultFigure() {
return new Circle(10,Color.WHITE.toString());
}

Choose a reason for hiding this comment

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

redundant indent line

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

This file was deleted.

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

public class IsoscelesTrapezoid extends Figure {
private double upSide;
private double downSide;
private double height;

public IsoscelesTrapezoid(double upSide, double downSide, double height, String color) {
this.upSide = upSide;
this.downSide = downSide;
this.height = height;
setColor(color);
getArea();
}

@Override
public double getArea() {
return (upSide + downSide) * height / 2;
}

@Override
public String toString() {
System.out.println("IsoscelesTrapezoid{" + "upSide=" + upSide
+ ", downSide=" + downSide
+ ", height=" + height + ", color='"
+ getColor() + '\'' + ", area=" + getArea() + '}');
Comment on lines +22 to +25

Choose a reason for hiding this comment

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

behaviour - we can obtain the area of any figure and are able to draw it. To 'draw' means to print out all information about a figure using System.out.println() (you shouldn't override toString() method for this).

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

public class Main {
public static void main(String[] args) {
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.

The number of figures is hardcoded to 4, but the task description says to create an array of figures where the first half are generated with random parameters and the second half have default parameters. the key point is "the size of the array can be 3 or 6, it doesn't matter". Consider making the size of the array dynamic and clarify the number of figures to be generated to the constant. use loop for dynamic objects processing

FigureSupplier figureSupplier = new FigureSupplier();
figures[0] = figureSupplier.getRandomFigure();
figures[1] = figureSupplier.getRandomFigure();
figures[2] = new Circle(10,Color.WHITE.toString());
figures[3] = new Square(5,Color.BLACK.toString());
for (Figure figure : figures) {
figure.toString();

Choose a reason for hiding this comment

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

consider adding interface instead of overriding toString

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

public class Rectangle extends Figure {
private double firstLeg;
private double secondLeg;

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

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

@Override
public String toString() {
System.out.println("Rectangle{" + "firstLeg=" + firstLeg
+ ", secondLeg=" + secondLeg + ", color='" + getColor() + '\''
+ ", area=" + getArea() + '}');
return "";
}
}
26 changes: 26 additions & 0 deletions src/main/java/core/basesyntax/RightTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package core.basesyntax;

public class RightTriangle extends Figure {
private double firstLeg;
private double secondLeg;

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

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

@Override
public String toString() {
System.out.println("RightTriangle{" + "firstLeg=" + firstLeg
+ ", secondLeg=" + secondLeg + ", color='" + getColor() + '\''
+ ", area=" + getArea() + '}');
return "";
}
}
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax;

public class Square extends Figure {
private double side;

public Square(double side,String color) {
this.side = side;
setColor(color);
getArea();
}

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

@Override
public String toString() {
System.out.println("Square{" + "side=" + side
+ ", color='" + getColor() + '\''
+ ", area=" + getArea() + '}');
return "";
}
}
Loading