-
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
add base classes, add base loop #1734
base: master
Are you sure you want to change the base?
Changes from 6 commits
3e14e28
294ce09
da3d91c
0f3dfa8
5842718
2809d8e
47051ff
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,23 @@ | ||
package core.basesyntax; | ||
|
||
/** | ||
* Feel free to remove this class and create your own. | ||
*/ | ||
public class App { | ||
public static final int FIGURES_LENGTH = 6; | ||
|
||
public static void main(String[] args) { | ||
Figure[] figures = new Figure[FIGURES_LENGTH]; | ||
FigureSupplier figureSupplier = new FigureSupplier(); | ||
for (int i = 0; i < figures.length; i++) { | ||
boolean isFirstPart = i < (FIGURES_LENGTH / 2); | ||
if (isFirstPart) { | ||
figures[i] = figureSupplier.getRandomFigure(); | ||
} else { | ||
figures[i] = figureSupplier.getDefaultFigure(); | ||
} | ||
|
||
figures[i].drawDetails(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package core.basesyntax; | ||
|
||
public class Circle extends Figure { | ||
private int radius; | ||
|
||
public Circle(int radius, Color color) { | ||
setFigure(FigureType.CIRCLE); | ||
setRadius(radius); | ||
setColor(color); | ||
double area = Math.round(getCalculatedArea()); | ||
setArea(area); | ||
} | ||
|
||
public Circle() { | ||
setFigure(FigureType.CIRCLE); | ||
setRadius(2); | ||
setColor(Color.BLUE); | ||
double area = Math.round(getCalculatedArea()); | ||
setArea(area); | ||
} | ||
|
||
@Override | ||
public double getCalculatedArea() { | ||
return Math.PI * radius * 2; | ||
} | ||
|
||
@Override | ||
public void drawDetails() { | ||
System.out.println( | ||
String.format("Figure: %s, area: %s sq.units, radius: %s units, color: %s", | ||
getFigure().toLowerCase(), | ||
getArea(), | ||
getRadius(), | ||
getColor().toString().toLowerCase()) | ||
); | ||
} | ||
|
||
public void setRadius(int radius) { | ||
this.radius = radius; | ||
} | ||
|
||
public int getRadius() { | ||
return radius; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public enum Color { | ||
RED, BLUE, WHITE, GREEN, PURPLE, BLACK, YELLOW, ORANGE, OCEAN, PINK, ZINC | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class ColorSupplier { | ||
private final List<Color> availableColors = Arrays.asList(Color.values()); | ||
private final Random random = new Random(); | ||
|
||
public Color getRandomColor() { | ||
int colorsMaxIndex = availableColors.size(); | ||
int randomIndex = random.nextInt(colorsMaxIndex); | ||
return availableColors.get(randomIndex); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface Draw { | ||
public void drawDetails(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package core.basesyntax; | ||
|
||
public abstract class Figure implements Draw { | ||
private FigureType figure; | ||
private Color color; | ||
private double area; | ||
|
||
public void setFigure(FigureType figure) { | ||
this.figure = figure; | ||
} | ||
|
||
public void setColor(Color color) { | ||
this.color = color; | ||
} | ||
|
||
public void setArea(double area) { | ||
this.area = area; | ||
} | ||
|
||
public String getFigure() { | ||
return figure.name().toLowerCase(); | ||
} | ||
|
||
public Color getColor() { | ||
return color; | ||
} | ||
|
||
public double getArea() { | ||
return area; | ||
} | ||
|
||
public abstract double getCalculatedArea(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class FigureSupplier { | ||
private static final int DEFAULT_CIRCLE_RADIUS = 10; | ||
private static final int MAX_RANDOM_INTEGER = 10; | ||
private final List<FigureType> availableFigures = Arrays.asList(FigureType.values()); | ||
private final Random random = new Random(); | ||
private final ColorSupplier color = new ColorSupplier(); | ||
|
||
public Figure getDefaultFigure() { | ||
return new Circle(DEFAULT_CIRCLE_RADIUS, Color.WHITE); | ||
} | ||
|
||
public Figure getRandomFigure() { | ||
int figuresMaxIndex = availableFigures.size(); | ||
int randomIndex = random.nextInt(figuresMaxIndex); | ||
|
||
FigureType randomFigure = availableFigures.get(randomIndex); | ||
Color randomColor = color.getRandomColor(); | ||
|
||
switch (randomFigure) { | ||
case CIRCLE: return new Circle( | ||
random.nextInt(MAX_RANDOM_INTEGER), | ||
randomColor | ||
); | ||
case SQUARE: return new Square( | ||
random.nextInt(MAX_RANDOM_INTEGER), | ||
randomColor | ||
); | ||
case RECTANGLE: return new Rectangle( | ||
random.nextInt(MAX_RANDOM_INTEGER), | ||
random.nextInt(MAX_RANDOM_INTEGER), | ||
randomColor | ||
); | ||
case RIGHT_TRIANGLE: return new RightTriangle( | ||
random.nextInt(MAX_RANDOM_INTEGER), | ||
random.nextInt(MAX_RANDOM_INTEGER), | ||
randomColor | ||
); | ||
case ISOSCELES_TRAPEZOID: return new IsoscelesTrapezoid( | ||
random.nextInt(MAX_RANDOM_INTEGER), | ||
random.nextInt(MAX_RANDOM_INTEGER), | ||
random.nextInt(MAX_RANDOM_INTEGER), | ||
randomColor | ||
); | ||
default: return getDefaultFigure(); | ||
} | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package core.basesyntax; | ||
|
||
public enum FigureType { | ||
SQUARE, | ||
RECTANGLE, | ||
RIGHT_TRIANGLE, | ||
CIRCLE, | ||
ISOSCELES_TRAPEZOID | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package core.basesyntax; | ||
|
||
public class IsoscelesTrapezoid extends Figure { | ||
private static final String DEFAULT_DRAW_MESSAGE = | ||
"Figure: %s, area: %s sq.units, top: %s units, " | ||
+ "bottom: %s units, height: %s units, color: %s"; | ||
private int topLength; | ||
private int bottomLength; | ||
private int height; | ||
|
||
public IsoscelesTrapezoid(int topLength, int bottomLength, int height, Color color) { | ||
setFigure(FigureType.ISOSCELES_TRAPEZOID); | ||
setTopLength(topLength); | ||
setBottomLength(bottomLength); | ||
setHeight(height); | ||
setColor(color); | ||
} | ||
|
||
public IsoscelesTrapezoid() { | ||
setFigure(FigureType.ISOSCELES_TRAPEZOID); | ||
setTopLength(2); | ||
setBottomLength(2); | ||
setHeight(2); | ||
setColor(Color.BLUE); | ||
} | ||
|
||
@Override | ||
public double getCalculatedArea() { | ||
return ((bottomLength * topLength) * height) / 2.0; | ||
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. The formula for calculating the area of an isosceles trapezoid is incorrect. It should be |
||
} | ||
|
||
@Override | ||
public void drawDetails() { | ||
System.out.println( | ||
String.format( | ||
DEFAULT_DRAW_MESSAGE, | ||
getFigure().toLowerCase(), | ||
getArea(), | ||
getTopLength(), | ||
getBottomLength(), | ||
getHeight(), | ||
getColor().toString().toLowerCase()) | ||
); | ||
} | ||
|
||
public void setBottomLength(int bottomLength) { | ||
this.bottomLength = bottomLength; | ||
} | ||
|
||
public void setTopLength(int topLength) { | ||
this.topLength = topLength; | ||
} | ||
|
||
public void setHeight(int height) { | ||
this.height = height; | ||
} | ||
|
||
public int getBottomLength() { | ||
return bottomLength; | ||
} | ||
|
||
public int getTopLength() { | ||
return topLength; | ||
} | ||
|
||
public int getHeight() { | ||
return height; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Figure { | ||
private static final String DEFAULT_DRAW_MESSAGE = | ||
"Figure: %s, area: %s sq.units, width: %s units, length: %s units, color: %s"; | ||
private int width; | ||
private int length; | ||
|
||
public Rectangle(int width, int length, Color color) { | ||
setFigure(FigureType.RECTANGLE); | ||
setWidth(width); | ||
setLength(length); | ||
double area = getCalculatedArea(); | ||
setArea(area); | ||
setColor(color); | ||
} | ||
|
||
public Rectangle() { | ||
setFigure(FigureType.RECTANGLE); | ||
setWidth(2); | ||
setLength(2); | ||
double area = getCalculatedArea(); | ||
setArea(area); | ||
setColor(Color.BLUE); | ||
} | ||
|
||
@Override | ||
public double getCalculatedArea() { | ||
return width * length; | ||
} | ||
|
||
@Override | ||
public void drawDetails() { | ||
System.out.println( | ||
String.format( | ||
DEFAULT_DRAW_MESSAGE, | ||
getFigure().toLowerCase(), | ||
getArea(), | ||
getWidth(), | ||
getLength(), | ||
getColor().toString().toLowerCase()) | ||
); | ||
} | ||
|
||
public void setWidth(int width) { | ||
this.width = width; | ||
} | ||
|
||
public void setLength(int length) { | ||
this.length = length; | ||
} | ||
|
||
public int getWidth() { | ||
return width; | ||
} | ||
|
||
public int getLength() { | ||
return length; | ||
} | ||
} |
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.
The formula for calculating the area of a circle is incorrect. It should be
Math.PI * radius * radius
instead ofMath.PI * radius * 2
. Please correct this to ensure the area is calculated accurately.