From 8d0c8f3a53c24b899708c782ac68f8cfbac205fd Mon Sep 17 00:00:00 2001 From: Andrii Linkov Date: Fri, 8 Nov 2024 19:54:04 +0200 Subject: [PATCH] Deleted interface AreaCalculator, transfered method getArea() to class Figure --- src/main/java/core/basesyntax/AreaCalculator.java | 5 ----- src/main/java/core/basesyntax/Circle.java | 12 ++++++------ src/main/java/core/basesyntax/Figure.java | 4 +++- .../java/core/basesyntax/IsoscelesTrapezoid.java | 14 +++++++------- src/main/java/core/basesyntax/Rectangle.java | 12 ++++++------ src/main/java/core/basesyntax/RightTriangle.java | 12 ++++++------ src/main/java/core/basesyntax/Square.java | 12 ++++++------ 7 files changed, 34 insertions(+), 37 deletions(-) delete mode 100644 src/main/java/core/basesyntax/AreaCalculator.java diff --git a/src/main/java/core/basesyntax/AreaCalculator.java b/src/main/java/core/basesyntax/AreaCalculator.java deleted file mode 100644 index 673262c690..0000000000 --- a/src/main/java/core/basesyntax/AreaCalculator.java +++ /dev/null @@ -1,5 +0,0 @@ -package core.basesyntax; - -public interface AreaCalculator { - double getArea(); -} diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java index c9736296d5..9589232a9e 100644 --- a/src/main/java/core/basesyntax/Circle.java +++ b/src/main/java/core/basesyntax/Circle.java @@ -8,18 +8,18 @@ public Circle(Color color, double radius) { this.radius = radius; } - @Override - public double getArea() { - return Math.PI * radius * radius; - } - @Override public void draw() { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("Figure: circle, ") .append("area: ").append(getArea()).append(" sq.units, ") .append("radius: ").append(radius).append(" units, ") - .append("color: ").append(color); + .append("color: ").append(color.name()); System.out.println(stringBuilder.toString()); } + + @Override + protected double getArea() { + return Math.PI * radius * radius; + } } diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java index 7f37317ba4..de8ae77d34 100644 --- a/src/main/java/core/basesyntax/Figure.java +++ b/src/main/java/core/basesyntax/Figure.java @@ -1,9 +1,11 @@ package core.basesyntax; -public abstract class Figure implements AreaCalculator, Drawable { +public abstract class Figure implements Drawable { protected Color color; public Figure(Color color) { this.color = color; } + + protected abstract double getArea(); } diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index a76fdbfcdd..9d1107b85c 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -12,12 +12,6 @@ public IsoscelesTrapezoid(Color color, double leg, double firstBase, double seco this.secondBase = secondBase; } - @Override - public double getArea() { - double h = Math.sqrt(Math.pow(leg, 2.0) - Math.pow((firstBase - secondBase), 2.0) / 4.0); - return (firstBase + secondBase) * h / 2.0; - } - @Override public void draw() { StringBuilder stringBuilder = new StringBuilder(); @@ -26,8 +20,14 @@ public void draw() { .append("leg: ").append(leg).append(" units, ") .append("firstBase: ").append(firstBase).append(" units, ") .append("secondBase: ").append(secondBase).append(" units, ") - .append("color: ").append(color); + .append("color: ").append(color.name()); System.out.println(stringBuilder.toString()); } + + @Override + protected double getArea() { + double h = Math.sqrt(Math.pow(leg, 2.0) - Math.pow((firstBase - secondBase) / 2.0, 2.0)); + return (firstBase + secondBase) * h / 2.0; + } } diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index 4cc8c91a6a..2f3804bf94 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -10,11 +10,6 @@ public Rectangle(Color color, double length, double width) { this.width = width; } - @Override - public double getArea() { - return length * width; - } - @Override public void draw() { StringBuilder stringBuilder = new StringBuilder(); @@ -22,7 +17,12 @@ public void draw() { .append("area: ").append(getArea()).append(" sq.units, ") .append("length: ").append(length).append(" units, ") .append("width: ").append(width).append(" units, ") - .append("color: ").append(color); + .append("color: ").append(color.name()); System.out.println(stringBuilder.toString()); } + + @Override + protected double getArea() { + return length * width; + } } diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index 1c7c3aa019..c6a9bce8a5 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -10,11 +10,6 @@ public RightTriangle(Color color, double firstLeg, double secondLeg) { this.secondLeg = secondLeg; } - @Override - public double getArea() { - return firstLeg * secondLeg / 2.0; - } - @Override public void draw() { StringBuilder stringBuilder = new StringBuilder(); @@ -22,7 +17,12 @@ public void draw() { .append("area: ").append(getArea()).append(" sq.units, ") .append("firstLeg: ").append(firstLeg).append(" units, ") .append("secondLeg: ").append(secondLeg).append(" units, ") - .append("color: ").append(color); + .append("color: ").append(color.name()); System.out.println(stringBuilder.toString()); } + + @Override + protected double getArea() { + return firstLeg * secondLeg / 2.0; + } } diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java index c4fd163882..bd20947e83 100644 --- a/src/main/java/core/basesyntax/Square.java +++ b/src/main/java/core/basesyntax/Square.java @@ -8,18 +8,18 @@ public Square(Color color, double side) { this.side = side; } - @Override - public double getArea() { - return side * side; - } - @Override public void draw() { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("Figure: square, ") .append("area: ").append(getArea()).append(" sq.units, ") .append("side: ").append(side).append(" units, ") - .append("color: ").append(color); + .append("color: ").append(color.name()); System.out.println(stringBuilder.toString()); } + + @Override + protected double getArea() { + return side * side; + } }