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

created figures and interfaces with calculation and drawing methods #1326

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -42,7 +48,6 @@
</executions>
<configuration>
<configLocation>${maven.checkstyle.plugin.configLocation}</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
Expand All @@ -61,8 +66,13 @@
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>

</plugins>

</pluginManagement>

</build>

</project>


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

interface AreaCalculator {

double calculateArea();

Choose a reason for hiding this comment

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

image

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

public class Circle extends Figure {
private int radius;

public Circle(String color, int 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());
}

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

import java.util.Random;

class ColorSupplier {

Choose a reason for hiding this comment

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

image

private static final String[] colors = {"red", "green", "blue", "yellow", "purple",
"orange", "pink"};

Choose a reason for hiding this comment

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

Let's remove this array and create enum Color (as a separate class)

private final Random random = new Random();

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

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

public abstract class Figure implements AreaCalculator, Drawable {
private String color;

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

public String getColor() {
return color;
}

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

import java.util.Random;

public class FigureSupplier {
private final Random random = new Random();
private final ColorSupplier colorSupplier = new ColorSupplier();

public Figure getRandomFigure() {

final int figureType = random.nextInt(5);

Choose a reason for hiding this comment

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

image


String color = colorSupplier.getRandomColor();
int sideOrRadius = random.nextInt() * 10;

switch (figureType) {
case 0:
return new Square(color, sideOrRadius);
case 1:
return new Circle(color, sideOrRadius);
case 2:
double width = random.nextDouble() * 10;
double height = random.nextDouble() * 10;
return new Rectangle(color, (int) width, (int) height);
case 3:
double firstLeg = random.nextDouble() * 10;
double secondLeg = random.nextDouble() * 10;
return new RightTriangle(color, (int) secondLeg, (int) firstLeg);
case 4:
int topBase = random.nextInt() * 10;
int bottomBase = random.nextInt() * 10;
int heightTrapezoid = random.nextInt() * 10;
return new IsoscelesTrapezoid(color, topBase, bottomBase, heightTrapezoid);
Comment on lines +15 to +31

Choose a reason for hiding this comment

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

For every case you can create own method and you have a lot of duplicate code with your random values


default:
return getDefaultFigure();
}

}

public Figure getDefaultFigure() {
return new Circle("white", 10);

Choose a reason for hiding this comment

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

Use constant for parameters

}
}
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 int base1;
private int base2;
private int height;

public IsoscelesTrapezoid(String color, int base1, int base2, int height) {
super(color);
this.base1 = base1;
this.base2 = base2;
this.height = height;
}

@Override
public double calculateArea() {
return ((double) (base1 + base2) / 2) * height;
}

@Override
public void draw() {
System.out.println("Figure: isoscelesTrapezoid, area: " + calculateArea()
+ " sq. units, base1: "
+ base1 + "base2" + base2 + " units, color: " + getColor());
}

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

public class Main {
public static void main(String[] args) {
FigureSupplier figureSupplier = new FigureSupplier();
ColorSupplier colorSupplier = new ColorSupplier();

Figure[] figures = new Figure[6];

for (int i = 0; i < 6; i++) {
figures[i] = figureSupplier.getRandomFigure();
String color = colorSupplier.getRandomColor();
figures[i].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 int width;
private int height;

public Rectangle(String color, int width, int height) {
super(color);
this.width = width;
this.height = height;
}

@Override
public double calculateArea() {
return width * height;
}

@Override
public void draw() {
System.out.println("Figure: rectangle, area: " + calculateArea() + " sq. units, width: "
+ width + " units, height: " + height + " units, color: " + getColor());
}

}
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 int firstLeg;
private int secondLeg;

public RightTriangle(String color, int firstLeg, int secondLeg) {
super(color);
this.firstLeg = firstLeg;
this.secondLeg = secondLeg;
}

@Override
public double calculateArea() {
return 0;
}

@Override
public void draw() {
System.out.println("Figure: rectangle, area: " + calculateArea() + " sq. units, firstLeg: "
+ firstLeg + " units, secondLeg: " + secondLeg + " units, color: " + getColor());
}

}
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;

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() + " sq. units, side: " + side
+ " units, color: " + getColor());
}

}
Loading