-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(DEVELOP): create Figure structure #1951
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package core.basesyntax; | ||
|
||
import core.basesyntax.model.Figure; | ||
import core.basesyntax.service.FigureSupplier; | ||
|
||
public class Main { | ||
private static final int COUNT_FIGURES = 6; | ||
private static final int HALF_OF_LIST = COUNT_FIGURES / 2; | ||
|
||
public static void main(String[] args) { | ||
Figure[] figures = new Figure[COUNT_FIGURES]; | ||
FigureSupplier figureSupplier = new FigureSupplier(); | ||
|
||
for (int i = 0; i < figures.length; i++) { | ||
if (i < HALF_OF_LIST) { | ||
figures[i] = figureSupplier.getRandomFigure(); | ||
} else { | ||
figures[i] = figureSupplier.getDefaultFigure(); | ||
} | ||
figures[i].draw(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package core.basesyntax.model; | ||
|
||
public class Circle extends Figure { | ||
private int radius; | ||
|
||
public Circle(int radius, String color) { | ||
super(color); | ||
this.radius = radius; | ||
} | ||
|
||
public int getRadius() { | ||
return radius; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return Math.PI * radius * radius; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Circle: " | ||
+ "color: " + getColor() | ||
+ ", radius: " + getRadius() + " units" | ||
+ ", area: " + getArea() + " square units"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package core.basesyntax.model; | ||
|
||
public enum Color { | ||
RED, | ||
BLUE, | ||
GREEN, | ||
YELLOW, | ||
BLACK, | ||
WHITE, | ||
ORANGE | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax.model; | ||
|
||
public interface Drawable { | ||
void draw(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package core.basesyntax.model; | ||
|
||
public abstract class Figure implements Drawable { | ||
private String color; | ||
|
||
public Figure(String color) { | ||
this.color = color; | ||
} | ||
|
||
abstract double getArea(); | ||
|
||
public String getColor() { | ||
return color; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package core.basesyntax.model; | ||
|
||
public class IsoscelesTrapezoid extends Figure { | ||
private int base; | ||
private int height; | ||
private int side; | ||
|
||
public IsoscelesTrapezoid(int base, int height, int side, String color) { | ||
super(color); | ||
this.base = base; | ||
this.height = height; | ||
this.side = side; | ||
} | ||
|
||
public int getBase() { | ||
return base; | ||
} | ||
|
||
public int getHeight() { | ||
return height; | ||
} | ||
|
||
public int getSide() { | ||
return side; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return 0.5 * (base + side) * height; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("IsoscelesTrapezoid: " | ||
+ "color: " + getColor() | ||
+ ", base: " + getBase() + " units" | ||
+ ", height: " + getHeight() + " units" | ||
+ ", side: " + getSide() + " units" | ||
+ ", area: " + getArea() + " sq. units"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package core.basesyntax.model; | ||
|
||
public class Rectangle extends Figure { | ||
private int width; | ||
private int height; | ||
|
||
public Rectangle(int width, int height, String color) { | ||
super(color); | ||
this.width = width; | ||
this.height = height; | ||
} | ||
|
||
public int getWidth() { | ||
return width; | ||
} | ||
|
||
public int getHeight() { | ||
return height; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return width * height; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Rectangle: " | ||
+ "color: " + getColor() | ||
+ ", width: " + getWidth() + " units" | ||
+ ", height: " + getHeight() + " units" | ||
+ ", area: " + getArea() + " square units"); | ||
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 area output in draw() should use "sq. units" instead of "square units" to match the required format: 'area: sq. units'. 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. This violates the requirement for exact output format: area must be printed as "area: ... sq. units" (not "square units"). See task description and previous review: output must be exactly "sq. units" to pass tests. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package core.basesyntax.model; | ||
|
||
public class RightTriangle extends Figure { | ||
private int firstLeg; | ||
private int secondLeg; | ||
|
||
public RightTriangle(int firstLeg, int secondLeg, String color) { | ||
super(color); | ||
this.firstLeg = firstLeg; | ||
this.secondLeg = secondLeg; | ||
} | ||
|
||
public int getFirstLeg() { | ||
return firstLeg; | ||
} | ||
|
||
public int getSecondLeg() { | ||
return secondLeg; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return 0.5 * firstLeg * secondLeg; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("RightTriangle: " | ||
+ "color: " + getColor() | ||
+ ", base: " + getFirstLeg() + " units" | ||
+ ", height: " + getSecondLeg() + " units" | ||
+ ", area: " + getArea() + " sq. units"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package core.basesyntax.model; | ||
|
||
public class Square extends Figure { | ||
private int side; | ||
|
||
public Square(int side, String color) { | ||
super(color); | ||
this.side = side; | ||
} | ||
|
||
public int getSide() { | ||
return side; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return side * side; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Square: " | ||
+ "color: " + getColor() | ||
+ ", side: " + getSide() + " units" | ||
+ ", area: " + getArea() + " sq. units"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.Color; | ||
import java.util.Random; | ||
|
||
public class ColorSupplier { | ||
private Random random = new Random(); | ||
private Color[] colors = Color.values(); | ||
|
||
public String getRandomColor() { | ||
int randomIndex = random.nextInt(colors.length); | ||
|
||
return colors[randomIndex].name(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.Circle; | ||
import core.basesyntax.model.Figure; | ||
import core.basesyntax.model.IsoscelesTrapezoid; | ||
import core.basesyntax.model.Rectangle; | ||
import core.basesyntax.model.RightTriangle; | ||
import core.basesyntax.model.Square; | ||
import java.util.Random; | ||
|
||
public class FigureSupplier { | ||
private static final int FIGURE_TYPES_COUNT = 5; | ||
private static final int MAX_SIZE = 10; | ||
|
||
private final Random random = new Random(); | ||
private final ColorSupplier colorSupplier = new ColorSupplier(); | ||
|
||
public Figure getRandomFigure() { | ||
int randomIndex = random.nextInt(FIGURE_TYPES_COUNT) + 1; | ||
String color = colorSupplier.getRandomColor(); | ||
|
||
int side; | ||
int radius; | ||
int width; | ||
int height; | ||
int base; | ||
int firstLeg; | ||
int secondLeg; | ||
|
||
switch (randomIndex) { | ||
case 1: | ||
side = random.nextInt(MAX_SIZE) + 1; | ||
return new Square(side, color); | ||
case 2: | ||
radius = random.nextInt(MAX_SIZE) + 1; | ||
return new Circle(radius, color); | ||
case 3: | ||
width = random.nextInt(MAX_SIZE) + 1; | ||
height = random.nextInt(MAX_SIZE) + 1; | ||
return new Rectangle(width, height, color); | ||
case 4: | ||
firstLeg = random.nextInt(MAX_SIZE) + 1; | ||
secondLeg = random.nextInt(MAX_SIZE) + 1; | ||
return new RightTriangle(firstLeg, secondLeg, color); | ||
case 5: | ||
base = random.nextInt(MAX_SIZE) + 1; | ||
height = random.nextInt(MAX_SIZE) + 1; | ||
side = random.nextInt(MAX_SIZE) + 1; | ||
return new IsoscelesTrapezoid(base, height, side, color); | ||
default: | ||
return getDefaultFigure(); | ||
} | ||
} | ||
|
||
public Figure getDefaultFigure() { | ||
return new Circle(10, "white"); | ||
} | ||
} |
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.
This violates the requirement for exact output format: area must be printed as "area: ... sq. units" (not "square units").
See task description and previous review: output must be exactly "sq. units" to pass tests.