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/AreaCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

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

public class Circle extends Figure {
private final int radius;

Choose a reason for hiding this comment

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

this field is marked as 'final' which means that it can't be changed after it's initialized. This is okay if you will never need to change this value, but if you want your objects to be mutable, you should remove the 'final' keyword.


public Circle(Color color,int radius) {

Choose a reason for hiding this comment

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

There should be a space after the comma in the parameters list of the constructor.

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

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

@Override
public void draw() {
System.out.println("Circle{" + "radius=" + radius
+ ", color='" + getColor() + '\'' + ", area=" + getArea() + '}');
}
}
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 {
WHITE,
BLACK,
BLUE,
RED
}
11 changes: 11 additions & 0 deletions src/main/java/core/basesyntax/ColorSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package core.basesyntax;

import java.util.Random;

public class ColorSupplier {
private final Random rand = new Random();

public Color getRandomColor() {
return Color.values()[rand.nextInt(Color.values().length)];
}
}
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 AreaCalculator, FigureDrawer {
private final Color color;

Choose a reason for hiding this comment

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

this field is marked as 'final' which means that it can't be changed after it's initialized. This is okay if you will never need to change this value, but if you want your objects to be mutable, you should remove the 'final' keyword.


public Figure(Color color) {
this.color = color;
}

public Color getColor() {
return color;
}
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/FigureDrawer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

public interface FigureDrawer {
void draw();
}
41 changes: 41 additions & 0 deletions src/main/java/core/basesyntax/FigureSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package core.basesyntax;

import java.util.Random;

public class FigureSupplier {
public static final int MAX_NUMBER = 100;

Choose a reason for hiding this comment

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

The MAX_NUMBER constant could be private as it's not used outside of this class.

private final ColorSupplier colorSupplier = new ColorSupplier();

Choose a reason for hiding this comment

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

The colorSupplier object could be static because its method is stateless, and it would be more efficient to have only one instance of ColorSupplier for all FigureSupplier objects.

private final Random random = new Random();

public Figure getRandomFigure() {
FigureType randomFigure = FigureType.values()[random.nextInt(FigureType.values().length)];
switch (randomFigure) {
case CIRCLE:
return new Circle(colorSupplier.getRandomColor(),
random.nextInt(MAX_NUMBER));
case SQUARE:
return new Square(colorSupplier.getRandomColor(),
random.nextInt(MAX_NUMBER));
case RECTANGLE:
return new Rectangle(colorSupplier.getRandomColor(),
random.nextInt(MAX_NUMBER),
random.nextInt(MAX_NUMBER));
case RIGHT_RECTANGLE:
return new RightTriangle(colorSupplier.getRandomColor(),
random.nextInt(MAX_NUMBER),
random.nextInt(MAX_NUMBER));
case ISOSCELES_TRAPEZOID:
return new IsoscelesTrapezoid(colorSupplier.getRandomColor(),
random.nextInt(MAX_NUMBER),
random.nextInt(MAX_NUMBER),
random.nextInt(MAX_NUMBER));
default:
return getDefaultFigure();
}
}

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

Choose a reason for hiding this comment

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

image

}

Choose a reason for hiding this comment

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

redundant indent line

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

public enum FigureType {
CIRCLE,
SQUARE,
RECTANGLE,
RIGHT_RECTANGLE,
ISOSCELES_TRAPEZOID
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

public class IsoscelesTrapezoid extends Figure {
private final double upSide;

Choose a reason for hiding this comment

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

this fields is marked as 'final' which means that it can't be changed after it's initialized. This is okay if you will never need to change this value, but if you want your objects to be mutable, you should remove the 'final' keyword.

private final double downSide;
private final double height;

public IsoscelesTrapezoid(Color color, double upSide, double downSide, double height) {
super(color);
this.upSide = upSide;
this.downSide = downSide;
this.height = height;
}

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

@Override
public void draw() {
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).

}
}
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(Color.WHITE, 10);
figures[3] = new Square(Color.BLACK, 5);
for (Figure figure : figures) {
figure.draw();
}
}
}
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax;

public class Rectangle extends Figure {
private final double firstLeg;

Choose a reason for hiding this comment

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

this fields is marked as 'final' which means that it can't be changed after it's initialized. This is okay if you will never need to change this value, but if you want your objects to be mutable, you should remove the 'final' keyword.

private final double secondLeg;

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

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

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

public class RightTriangle extends Figure {
private final double firstLeg;

Choose a reason for hiding this comment

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

this fields is marked as 'final' which means that it can't be changed after it's initialized. This is okay if you will never need to change this value, but if you want your objects to be mutable, you should remove the 'final' keyword.

private final double secondLeg;

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

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

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

public class Square extends Figure {
private final double side;

Choose a reason for hiding this comment

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

this fields is marked as 'final' which means that it can't be changed after it's initialized. This is okay if you will never need to change this value, but if you want your objects to be mutable, you should remove the 'final' keyword.

Copy link
Author

Choose a reason for hiding this comment

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

This object is designed to be immutable, which does not change after it is created. In fact, I should have made the shape classes themselves final (Circle, Square and others).


public Square(Color color, double side) {
super(color);
this.side = side;
}

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

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