-
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
created figures and interfaces with calculation and drawing methods #1326
base: master
Are you sure you want to change the base?
Changes from 3 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,6 @@ | ||
package core.basesyntax; | ||
|
||
interface AreaCalculator { | ||
|
||
double calculateArea(); | ||
} |
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()); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
class ColorSupplier { | ||
|
||
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. |
||
private static final String[] colors = {"red", "green", "blue", "yellow", "purple", | ||
"orange", "pink"}; | ||
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. Let's remove this array and create |
||
private final Random random = new Random(); | ||
|
||
public String getRandomColor() { | ||
int index = random.nextInt(colors.length); | ||
return colors[index]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
interface Drawable { | ||
void draw(); | ||
} |
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; | ||
} | ||
|
||
} |
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); | ||
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. |
||
|
||
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
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. 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); | ||
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. Use constant for parameters |
||
} | ||
} |
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 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()); | ||
} | ||
|
||
} |
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(); | ||
|
||
} | ||
|
||
} | ||
} | ||
|
||
|
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()); | ||
} | ||
|
||
} |
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()); | ||
} | ||
|
||
} |
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()); | ||
} | ||
|
||
} |
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.