You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
859 B
25 lines
859 B
public class CircleToSquareConverter implements ShapeConverter<Circle, Square> {
|
|
@Override
|
|
public Square convert(Circle source) {
|
|
if (!canConvert(source)) {
|
|
throw new IllegalArgumentException("Cannot convert null circle");
|
|
}
|
|
|
|
// Calculate side length of square with equal area to the circle
|
|
double circleArea = source.calculateArea();
|
|
double squareSide = Math.sqrt(circleArea);
|
|
|
|
// Create square with the same color as the circle
|
|
return new Square(squareSide, source.getColor());
|
|
}
|
|
|
|
@Override
|
|
public boolean canConvert(Circle source) {
|
|
return source != null && source.getRadius() > 0;
|
|
}
|
|
|
|
@Override
|
|
public String getConversionDescription() {
|
|
return "Converts a circle to a square with equal area and same color";
|
|
}
|
|
} |