-
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 11 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(String.valueOf(color)); | ||
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 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. in task description, this method should return String |
||
Color[] colors = Color.values(); | ||
return colors[RANDOM.nextInt(colors.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,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() { | ||
Color 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, side); | ||
} | ||
case 1: { | ||
int width = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; | ||
int height = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; | ||
return new Rectangle(color, width, height); | ||
} | ||
case 2: { | ||
int base = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; | ||
int height = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; | ||
return new RightTriangle(color, base, height); | ||
} | ||
case 3: { | ||
int radius = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; | ||
return new Circle(color, radius); | ||
} | ||
default: { | ||
int base1 = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; | ||
int base2 = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; | ||
int height = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; | ||
return new IsoscelesTrapezoid(color, base1, base2, height); | ||
} | ||
} | ||
} | ||
|
||
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 base1; | ||
private double base2; | ||
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. numbers in naming are usually bad, let's name them topBase bottomBase |
||
private double height; | ||
|
||
public IsoscelesTrapezoid(Color color, double base1, double base2, double height) { | ||
super(String.valueOf(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. we should pass String to constructor. |
||
this.base1 = base1; | ||
this.base2 = base2; | ||
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("base1: " + base1 + " units, base2: " | ||
+ base2 + " units, height: " + height + " units"); | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return 0.5 * (base1 + base2) * 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(String.valueOf(color)); | ||
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(String.valueOf(color)); | ||
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(String.valueOf(color)); | ||
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