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

Homework #1316

Open
wants to merge 8 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 {
Copy link

Choose a reason for hiding this comment

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

Suggested change
public interface Area {
public interface AreaCalculator {

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

public class Circle extends Figure {
private final double radius;

public Circle(String color, double radius) {
this.color = 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: "
+
radius
+
" units, color: "
+
color);
}
}
12 changes: 12 additions & 0 deletions src/main/java/core/basesyntax/ColorSupplier.java
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", "green", "blue", "yellow", "purple", "orange"};
Copy link

Choose a reason for hiding this comment

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

Create public enum Color

private final Random random = new Random();

public String getRandomColor() {
return COLORS[random.nextInt(COLORS.length)];
}
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/Drawer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

public interface Drawer {
Copy link

Choose a reason for hiding this comment

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

Suggested change
public interface Drawer {
public interface Drawable {

void draw();
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/Figure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

public abstract class Figure implements Area, Drawer {
protected String color;
}
35 changes: 35 additions & 0 deletions src/main/java/core/basesyntax/FigureSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package core.basesyntax;

import java.util.Random;

public class FigureSupplier {
private static final Random random = new Random();
Copy link

Choose a reason for hiding this comment

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

Suggested change
private static final Random random = new Random();
private final Random random = new Random();

should be same as color supplier


public Figure getRandomFigure() {
int choice = random.nextInt(5);
Copy link

Choose a reason for hiding this comment

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

image

String color = new ColorSupplier().getRandomColor();

switch (choice) {
case 0:
return new Square(color, random.nextDouble() * 10 + 1);
case 1:
return new Rectangle(color, random.nextDouble() * 10 + 1,
random.nextDouble() * 10 + 1);
case 2:
return new RightTriangle(color, random.nextDouble() * 10 + 1,
random.nextDouble() * 10 + 1);
Copy link

Choose a reason for hiding this comment

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

extract random.nextDouble() * 10 + 1 to a private method, make 10 a constant

case 3:
return new Circle(color, random.nextDouble() * 10 + 1);
case 4:
return new IsoscelesTrapezoid(color, random.nextDouble() * 10 + 1,
random.nextDouble() * 10 + 1,
random.nextDouble() * 10 + 1);
default:
return getDefaultFigure();
}
}

public Figure getDefaultFigure() {
return new Circle("white", 10);
}
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

public class IsoscelesTrapezoid extends Figure {
private final double upperBase;
private final double lowerBase;
private final double height;

public IsoscelesTrapezoid(String color, double upperBase, double lowerBase, double height) {
this.color = color;
this.upperBase = upperBase;
this.lowerBase = lowerBase;
this.height = height;
}

@Override
public double getArea() {
return 0.5 * (upperBase + lowerBase) * height;
}

@Override
public void draw() {
System.out.println("Figure: isosceles trapezoid, area: "
+ getArea()
+ " sq.units, upperBase: "
+ upperBase
+ " units, lowerBase: "
+ lowerBase + " units, height: "
+ height
+ " units, color: "
+ color);
}
}
21 changes: 21 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.basesyntax;

public class Main {
public static void main(String[] args) {
final int N = 6;
Copy link

Choose a reason for hiding this comment

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

make it a class constant with a more meaningful name :)

FigureSupplier figureSupplier = new FigureSupplier();
Figure[] figures = new Figure[N];

for (int i = 0; i < 3; i++) {
figures[i] = figureSupplier.getRandomFigure();
}

for (int i = 3; i < 6; i++) {
figures[i] = figureSupplier.getDefaultFigure();
}

for (Figure figure : figures) {
figure.draw();
}
Copy link

Choose a reason for hiding this comment

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

can be done in one loop. use length/2

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

public class Rectangle extends Figure {
private final double length;
private final double width;

public Rectangle(String color, double length, double width) {
this.color = 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: "
+ length
+ " units, width: "
+ width + " units, color: "
+ color);
}
}
29 changes: 29 additions & 0 deletions src/main/java/core/basesyntax/RightTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package core.basesyntax;

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

public RightTriangle(String color, double firstLeg, double secondLeg) {
this.color = 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: "
+ firstLeg
+ " units, secondLeg: "
+ secondLeg + " units, color: "
+ color);
}

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

class Square extends Figure {
private final double side;

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

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

@Override
public void draw() {
System.out.println("Figure: square, area: "
+ getArea()
+ " sq.units, side: "
+ side
+ " units, color: "
+ color);
}
}
Loading