12 changed files with 59 additions and 0 deletions
Binary file not shown.
@ -0,0 +1,12 @@ |
|||
class Circle extends Shape { |
|||
private double radius; |
|||
|
|||
Circle(double radius) { |
|||
this.radius = radius; |
|||
} |
|||
|
|||
@Override |
|||
public double getArea() { |
|||
return Math.PI * radius * radius; |
|||
} |
|||
} |
|||
Binary file not shown.
@ -0,0 +1,11 @@ |
|||
public class Main { |
|||
public static void main(String[] args) { |
|||
Shape circle = new Circle(5.0); |
|||
Shape rectangle = new Rectangle(4.0, 6.0); |
|||
Shape triangle = new Triangle(3.0, 8.0); |
|||
|
|||
ShapeUtil.printArea(circle); |
|||
ShapeUtil.printArea(rectangle); |
|||
ShapeUtil.printArea(triangle); |
|||
} |
|||
} |
|||
Binary file not shown.
@ -0,0 +1,14 @@ |
|||
class Rectangle extends Shape { |
|||
private double width; |
|||
private double height; |
|||
|
|||
Rectangle(double width, double height) { |
|||
this.width = width; |
|||
this.height = height; |
|||
} |
|||
|
|||
@Override |
|||
public double getArea() { |
|||
return width * height; |
|||
} |
|||
} |
|||
Binary file not shown.
@ -0,0 +1,3 @@ |
|||
abstract class Shape { |
|||
public abstract double getArea(); |
|||
} |
|||
Binary file not shown.
@ -0,0 +1,5 @@ |
|||
class ShapeUtil { |
|||
public static void printArea(Shape shape) { |
|||
System.out.printf("%.2f%n", shape.getArea()); |
|||
} |
|||
} |
|||
Binary file not shown.
@ -0,0 +1,14 @@ |
|||
class Triangle extends Shape { |
|||
private double base; |
|||
private double height; |
|||
|
|||
Triangle(double base, double height) { |
|||
this.base = base; |
|||
this.height = height; |
|||
} |
|||
|
|||
@Override |
|||
public double getArea() { |
|||
return 0.5 * base * height; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue