-
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
Done #1775
base: master
Are you sure you want to change the base?
Done #1775
Changes from all commits
5f8db2f
c26debc
00af0f2
f453c23
5592940
1961220
b72ffb0
2a607bb
bb7e4f2
f57a1bf
022b7d6
e198d0b
9b0023b
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,22 @@ | ||||||
package core.basesyntax; | ||||||
|
||||||
public class Circle extends Figure { | ||||||
private final float radius; | ||||||
|
||||||
public Circle(Color color, float radius) { | ||||||
super(color, "Circle"); | ||||||
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.
Suggested change
|
||||||
this.radius = radius; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public double area() { | ||||||
return Math.PI * Math.pow(radius, 2); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public void draw() { | ||||||
System.out.println("Figure: " + getName() + ", area: " | ||||||
+ area() + " sq. units, radius: " | ||||||
+ radius + " units, color: " + getColor()); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package core.basesyntax; | ||
|
||
public enum Color { | ||
RED, | ||
BLUE, | ||
GREEN, | ||
WHITE, | ||
BLACK | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,11 @@ | ||||||
package core.basesyntax; | ||||||
|
||||||
import java.util.Random; | ||||||
|
||||||
public final 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.
Suggested change
|
||||||
private final Random picker = new Random(); | ||||||
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.
Suggested change
|
||||||
|
||||||
public Color getRandomColor() { | ||||||
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.
Suggested change
|
||||||
return Color.values()[picker.nextInt(Color.values().length)]; | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface Drawable { | ||
void draw(); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,29 @@ | ||||||
package core.basesyntax; | ||||||
|
||||||
public abstract class Figure implements Drawable { | ||||||
private Color color; | ||||||
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.
Suggested change
|
||||||
private String name; | ||||||
|
||||||
public Figure(Color color, String name) { | ||||||
this.color = color; | ||||||
this.name = name; | ||||||
} | ||||||
|
||||||
public Color getColor() { | ||||||
return this.color; | ||||||
} | ||||||
|
||||||
public void setColor(Color color) { | ||||||
this.color = color; | ||||||
} | ||||||
|
||||||
public String getName() { | ||||||
return this.name; | ||||||
} | ||||||
|
||||||
public void setName(String name) { | ||||||
this.name = name; | ||||||
} | ||||||
|
||||||
public abstract double area(); | ||||||
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. create interface
Suggested change
|
||||||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,36 @@ | ||||||||
package core.basesyntax; | ||||||||
|
||||||||
import java.util.Random; | ||||||||
|
||||||||
public final class FigureSupplier { | ||||||||
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.
Suggested change
|
||||||||
private final Random picker = new Random(); | ||||||||
private final ColorSupplier colorSupplier = new ColorSupplier(); | ||||||||
|
||||||||
public Figure getRandomFigure() { | ||||||||
final int figureCount = 5; | ||||||||
final int startNum = 1; | ||||||||
final int limitNum = 50; | ||||||||
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.
Suggested change
|
||||||||
|
||||||||
return switch (picker.nextInt(figureCount)) { | ||||||||
case 0 -> new Circle(colorSupplier.getRandomColor(), | ||||||||
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. same for other figures
Suggested change
|
||||||||
picker.nextInt(startNum, limitNum)); | ||||||||
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. create method separate method to get |
||||||||
case 1 -> new Rectangle(colorSupplier.getRandomColor(), | ||||||||
picker.nextInt(startNum, limitNum), | ||||||||
picker.nextInt(startNum, limitNum)); | ||||||||
case 2 -> new RightTriangle(colorSupplier.getRandomColor(), | ||||||||
picker.nextInt(startNum, limitNum), | ||||||||
picker.nextInt(startNum, limitNum)); | ||||||||
case 3 -> new Square(colorSupplier.getRandomColor(), | ||||||||
picker.nextInt(startNum, limitNum)); | ||||||||
case 4 -> new IsoscelesTrapezoid(colorSupplier.getRandomColor(), | ||||||||
picker.nextInt(startNum, limitNum), | ||||||||
picker.nextInt(startNum, limitNum), | ||||||||
picker.nextInt(startNum, limitNum)); | ||||||||
default -> new Circle(Color.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. 10 should be constant |
||||||||
}; | ||||||||
} | ||||||||
|
||||||||
public Figure getDefaultFigure() { | ||||||||
return new Circle(Color.WHITE, 10); | ||||||||
} | ||||||||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package core.basesyntax; | ||
|
||
public class IsoscelesTrapezoid extends Figure { | ||
private final float firstBase; | ||
private final float secondBase; | ||
private final float height; | ||
|
||
public IsoscelesTrapezoid(Color color, float firstBase, float secondBase, float height) { | ||
super(color, "IsoscelesTrapezoid"); | ||
this.firstBase = firstBase; | ||
this.secondBase = secondBase; | ||
this.height = height; | ||
} | ||
|
||
@Override | ||
public double area() { | ||
return ((firstBase + secondBase) / 2) * height; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: " + getName() | ||
+ ", area: " + area() + " sq. units, firstBase: " | ||
+ firstBase + " units, secondBase: " | ||
+ secondBase + " units, height: " | ||
+ height + " units, color: " + getColor()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package core.basesyntax; | ||
|
||
public class Main { | ||
private static final FigureSupplier figureSupplier = new FigureSupplier(); | ||
|
||
public static void main(String[] args) { | ||
printArray(); | ||
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. redundant separate method, create Figure[] here, 6 - should be constant, use one for loop for this task |
||
} | ||
|
||
private static Figure[] listOfRandomFigures() { | ||
Figure[] array = new Figure[6]; | ||
|
||
for (int i = 0; i < 3; i++) { | ||
array[i] = figureSupplier.getRandomFigure(); | ||
} | ||
|
||
for (int i = 3; i < 6; i++) { | ||
array[i] = figureSupplier.getDefaultFigure(); | ||
} | ||
|
||
return array; | ||
} | ||
|
||
private static void printArray() { | ||
final Figure[] listOfRandomFigures = listOfRandomFigures(); | ||
|
||
for (Figure i : listOfRandomFigures) { | ||
i.draw(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Figure { | ||
private final float firstSide; | ||
private final float secondSide; | ||
|
||
public Rectangle(Color color, float firstSide, float secondSide) { | ||
super(color, "Rectangle"); | ||
this.firstSide = firstSide; | ||
this.secondSide = secondSide; | ||
} | ||
|
||
@Override | ||
public double area() { | ||
return firstSide * secondSide; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: " + getName() + ", area: " | ||
+ area() + " sq. units, firstSide: " | ||
+ firstSide + " units, secondSide: " | ||
+ secondSide + " units, color: " + getColor()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package core.basesyntax; | ||
|
||
public class RightTriangle extends Figure { | ||
private final float firstLeg; | ||
private final float secondLeg; | ||
|
||
public RightTriangle(Color color, float firstLeg, float secondLeg) { | ||
super(color, "RightTriangle"); | ||
this.firstLeg = firstLeg; | ||
this.secondLeg = secondLeg; | ||
} | ||
|
||
@Override | ||
public double area() { | ||
return (firstLeg * secondLeg) / 2; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: " + getName() | ||
+ ", area: " + area() + " 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 float side; | ||
|
||
public Square(Color color, float side) { | ||
super(color, "Square"); | ||
this.side = side; | ||
} | ||
|
||
@Override | ||
public double area() { | ||
return side * side; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: " + getName() + ", area: " | ||
+ area() + " 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.