-
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
hw-1-jv-oop-advanced #1741
base: master
Are you sure you want to change the base?
hw-1-jv-oop-advanced #1741
Changes from all commits
640268c
fb52199
77f6840
21d4614
50ed5af
7869b7d
696a001
c9b1896
880f82b
f40894a
7b63e17
c09aec3
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,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface AreaCalcualtor { | ||
double getArea(); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,20 @@ | ||||||
package core.basesyntax; | ||||||
|
||||||
public class Circle extends Figure { | ||||||
private double radius; | ||||||
|
||||||
public Circle(Color color, double radius) { | ||||||
super(color.name()); | ||||||
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
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. not fixed |
||||||
this.radius = radius; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public double getArea() { | ||||||
return Math.PI * radius * radius; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public void draw() { | ||||||
System.out.println("radius: " + radius + " units"); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,5 @@ | ||||||||||||||||
package core.basesyntax; | ||||||||||||||||
|
||||||||||||||||
public enum Color { | ||||||||||||||||
RED, GREEN, BLUE, YELLOW, BLACK, WHITE; | ||||||||||||||||
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
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. not fixed |
||||||||||||||||
} |
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 Random RANDOM = new Random(); | ||
|
||
public String getRandomColor() { | ||
Color[] colors = Color.values(); | ||
return colors[RANDOM.nextInt(colors.length)].toString(); | ||
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. 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. not fixed |
||
} | ||
} |
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,15 @@ | ||
package core.basesyntax; | ||
|
||
public abstract class Figure implements Drawable, AreaCalcualtor { | ||
protected String color; | ||
|
||
public Figure(String color) { | ||
this.color = color; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: " + this.getClass().getSimpleName() | ||
+ ", area: " + getArea() + " sq. units, color: " + color); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
class FigureSupplier { | ||
private static final int FIGURE_TYPES_COUNT = 5; | ||
private static final int MAX_DIMENSION = 10; | ||
private static final int MIN_DIMENSION = 1; | ||
private static final int DEFAULT_RADIUS = 10; | ||
|
||
private Random random = new Random(); | ||
private ColorSupplier colorSupplier = new ColorSupplier(); | ||
|
||
public Figure getRandomFigure() { | ||
String color = colorSupplier.getRandomColor(); | ||
switch (random.nextInt(FIGURE_TYPES_COUNT)) { | ||
case 0: { | ||
int side = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; | ||
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 avoid duplication of code 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. not fixed |
||
return new Square(Color.valueOf(color), side); | ||
} | ||
case 1: { | ||
int firstLeg = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; | ||
int secondLeg = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; | ||
return new Rectangle(Color.valueOf(color), firstLeg, secondLeg); | ||
} | ||
case 2: { | ||
int firstLeg = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; | ||
int secondLeg = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; | ||
return new RightTriangle(Color.valueOf(color), firstLeg, secondLeg); | ||
} | ||
case 3: { | ||
int radius = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; | ||
return new Circle(Color.valueOf(color), radius); | ||
} | ||
default: { | ||
int topBase = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; | ||
int bottomBase = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; | ||
int secondLeg = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; | ||
return new IsoscelesTrapezoid(Color.valueOf(color), topBase, bottomBase, secondLeg); | ||
} | ||
} | ||
} | ||
|
||
public Figure getDefaultFigure() { | ||
return new Circle(Color.WHITE, DEFAULT_RADIUS); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package core.basesyntax; | ||
|
||
public class IsoscelesTrapezoid extends Figure { | ||
private double topBase; | ||
private double bottomBase; | ||
private double height; | ||
|
||
public IsoscelesTrapezoid(Color color, double topBase, double bottomBase, double height) { | ||
super(color.name()); | ||
this.topBase = topBase; | ||
this.bottomBase = bottomBase; | ||
this.height = height; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
super.draw(); | ||
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. Ensure that the |
||
System.out.println("topBase: " + topBase + " units, bottomBase: " | ||
+ bottomBase + " units, height: " + height + " units"); | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return 0.5 * (topBase + bottomBase) * height; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,22 @@ | ||||||||
package core.basesyntax; | ||||||||
|
||||||||
public class Main { | ||||||||
public static void main(String[] args) { | ||||||||
FigureSupplier figureSupplier = new FigureSupplier(); | ||||||||
final int arraySize = (int) (Math.random() * 10) + 1; | ||||||||
Figure[] figures = new Figure[arraySize]; | ||||||||
Comment on lines
+6
to
+7
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 constant SIZE_OF_ARRAY for example 6
Suggested change
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. not fixed |
||||||||
|
||||||||
for (int i = 0; i < figures.length; i++) { | ||||||||
if (i < figures.length / 2) { | ||||||||
figures[i] = figureSupplier.getRandomFigure(); | ||||||||
} else { | ||||||||
figures[i] = figureSupplier.getDefaultFigure(); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
for (Figure figure : figures) { | ||||||||
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 one for loop to implement main method 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. not fixed |
||||||||
figure.draw(); | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Figure { | ||
private double width; | ||
private double height; | ||
|
||
public Rectangle(Color color, double width, double height) { | ||
super(color.name()); | ||
this.width = width; | ||
this.height = height; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("width: " + width + " units, height: " + height + " units"); | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return width * height; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package core.basesyntax; | ||
|
||
public class RightTriangle extends Figure { | ||
private double firstLeg; | ||
private double secondLeg; | ||
|
||
public RightTriangle(Color color, double firstLeg, double secondLeg) { | ||
super(color.name()); | ||
this.firstLeg = firstLeg; | ||
this.secondLeg = secondLeg; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("firstLeg: " + firstLeg + " units, secondLeg: " + secondLeg + " units"); | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return 0.5 * firstLeg * secondLeg; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package core.basesyntax; | ||
|
||
public class Square extends Figure { | ||
private double side; | ||
|
||
public Square(Color color, double side) { | ||
super(color.name()); | ||
this.side = side; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
super.draw(); | ||
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. Ensure that the 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. Agree |
||
System.out.println("side: " + side + " units"); | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return side * side; | ||
} | ||
} |
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.
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.
For each figure
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.
not fixed