-
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
jv-oop-advanced homework #1297
base: master
Are you sure you want to change the base?
jv-oop-advanced homework #1297
Changes from 2 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,24 @@ | ||
package core.basesyntax; | ||
|
||
public class Circle extends Figure { | ||
private Color color; | ||
private double radius; | ||
|
||
public Circle(Color color, double radius) { | ||
this.color = color; | ||
this.radius = radius; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return Math.PI * radius * radius; | ||
} | ||
|
||
@Override | ||
public void print() { | ||
System.out.println("Figure: Circle " | ||
+ " area " + getArea() + " sq.units," | ||
+ " side " + radius + " units" | ||
+ " color " + color); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package core.basesyntax; | ||
|
||
public enum Color { | ||
RED, | ||
BLUE, | ||
GREEN, | ||
YELLOW, | ||
WHITE, | ||
BLACK, | ||
VELVET; | ||
} |
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 Random random = new Random(); | ||
|
||
public Color getRandomColor() { | ||
int index = random.nextInt(Color.values().length); | ||
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. Minor: you already have the field which holds colors, you can reuse it here |
||
return Color.values()[index]; | ||
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. I think you can make |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public abstract class Figure implements GetArea, Print { | ||
private Color color; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class FigureSupplier { | ||
static final int FIGURE_AMOUNT = 5; | ||
static final int FIGURE_PARAMETERS_MAX = 20; | ||
static final Color DEFAULT_COLOR = Color.WHITE; | ||
static final int DEFAULT_RADIUS = 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. Don't forget to make these fields private |
||
private Random random = new Random(); | ||
private ColorSupplier colorSupplier = new ColorSupplier(); | ||
|
||
public Figure getRandomFigure() { | ||
int randomFigureNumber = random.nextInt(FIGURE_AMOUNT + 1); | ||
int randomFigureParameter = random.nextInt(FIGURE_PARAMETERS_MAX + 1); | ||
switch (randomFigureNumber) { | ||
case 0: { | ||
return new Circle(colorSupplier.getRandomColor(), randomFigureParameter); | ||
} | ||
case 1: { | ||
return new Square(colorSupplier.getRandomColor(), randomFigureParameter); | ||
} | ||
case 2: { | ||
return new Rectangle(colorSupplier.getRandomColor(), | ||
randomFigureParameter, randomFigureParameter); | ||
} | ||
case 3: { | ||
return new RightTriangle(colorSupplier.getRandomColor(), | ||
randomFigureParameter, randomFigureParameter); | ||
} | ||
default: { | ||
return new IsoscelesTrapezoid(colorSupplier.getRandomColor(), | ||
randomFigureParameter, randomFigureParameter); | ||
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. since each figure needs a color you can extract colorSupplier.getRandomColor() to a separate variable and reuse 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. Your method is not fully random. For example, your Rectangle will always be a square since you pass the same randomFigureParameter there. So sizes of rectangle will always be equal. Please refactor this part |
||
} | ||
} | ||
} | ||
|
||
public Figure getDefaultFigure() { | ||
return new Circle(DEFAULT_COLOR, DEFAULT_RADIUS); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface GetArea { | ||
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. Think about this. |
||
double getArea(); | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package core.basesyntax; | ||
|
||
public class IsoscelesTrapezoid extends Figure { | ||
private Color color; | ||
private int side; | ||
private int height; | ||
|
||
public IsoscelesTrapezoid(Color color, int side, int height) { | ||
this.color = color; | ||
this.side = side; | ||
this.height = height; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
double sideD = side; | ||
double heightD = height; | ||
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. Remove it. |
||
return ((sideD + sideD) / 2d) * heightD; | ||
} | ||
|
||
@Override | ||
public void print() { | ||
System.out.println("Figure: Square " | ||
+ " area " + getArea() + " sq.units," | ||
+ " side " + side + " units," | ||
+ " height " + height + " units," | ||
+ " color " + color); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package core.basesyntax; | ||
|
||
public class MainApp { | ||
public static final int FIGURES_AMOUNT = 6; | ||
|
||
public static void main(String[] args) { | ||
FigureSupplier figureSupplier = new FigureSupplier(); | ||
Figure[] figures = new Figure[FIGURES_AMOUNT]; | ||
|
||
for (int i = 0; i < figures.length; i++) { | ||
if (i < FIGURES_AMOUNT / 2) { | ||
figures[i] = figureSupplier.getRandomFigure(); | ||
} else { | ||
figures[i] = figureSupplier.getDefaultFigure(); | ||
} | ||
} | ||
for (Figure figure : figures) { | ||
figure.print(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface Print { | ||
void print(); | ||
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. I think figure should |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Figure { | ||
private Color color; | ||
private int height; | ||
private int width; | ||
|
||
public Rectangle(Color color, int height, int width) { | ||
this.color = color; | ||
this.height = height; | ||
this.width = width; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return height * width; | ||
} | ||
|
||
@Override | ||
public void print() { | ||
System.out.println("Figure: Square " | ||
+ " area " + getArea() + " sq.units," | ||
+ " heigth " + height + " units" | ||
+ " width " + width + " units" | ||
+ " color " + color); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package core.basesyntax; | ||
|
||
public class RightTriangle extends Figure { | ||
private Color color; | ||
private int side; | ||
private int height; | ||
|
||
public RightTriangle(Color color, int side, int height) { | ||
this.color = color; | ||
this.side = side; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
double sideD = side; | ||
double heightD = height; | ||
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. Remove it. |
||
return (sideD * heightD) / 2d; | ||
} | ||
|
||
@Override | ||
public void print() { | ||
System.out.println("Figure: Circle " | ||
+ " area " + getArea() + " sq.units," | ||
+ " side " + side + " units" | ||
+ " height " + height + " units" | ||
+ " color " + color); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package core.basesyntax; | ||
|
||
public class Square extends Figure { | ||
private int side; | ||
private Color color; | ||
|
||
public Square(Color color, int side) { | ||
this.color = color; | ||
this.side = side; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return side * side; | ||
} | ||
|
||
@Override | ||
public void print() { | ||
System.out.println("Figure: Square " | ||
+ " area " + getArea() + " sq.units," | ||
+ " side " + side + " units" | ||
+ " color " + color); | ||
} | ||
} |
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.
You already have
Color color
field inFigure
class.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.
It has no sence, cuz it has to be inharited from Figure. Changed that everywhere