diff --git a/w4/Circle.java b/w4/Circle.java new file mode 100644 index 0000000..b3a64bc --- /dev/null +++ b/w4/Circle.java @@ -0,0 +1,12 @@ +public class Circle extends Shape { + private double radius; + + public Circle(double radius) { + this.radius = radius; + } + + @Override + public double getArea() { + return Math.PI * radius * radius; + } +} diff --git a/w4/Main.java b/w4/Main.java new file mode 100644 index 0000000..65461fa --- /dev/null +++ b/w4/Main.java @@ -0,0 +1,11 @@ +public class Main { + public static void main(String[] args) { + Shape circle = new Circle(5); + Shape rectangle = new Rectangle(4, 6); + Shape triangle = new Triangle(3, 4, 5); + + ShapeUtil.printArea(circle); + ShapeUtil.printArea(rectangle); + ShapeUtil.printArea(triangle); + } +} diff --git a/w4/Rectangle.java b/w4/Rectangle.java new file mode 100644 index 0000000..6cd9205 --- /dev/null +++ b/w4/Rectangle.java @@ -0,0 +1,13 @@ +public class Rectangle extends Shape { + private double width, height; + + public Rectangle(double width, double height) { + this.width = width; + this.height = height; + } + + @Override + public double getArea() { + return width * height; + } +} diff --git a/w4/Shape.java b/w4/Shape.java new file mode 100644 index 0000000..ac3b8fb --- /dev/null +++ b/w4/Shape.java @@ -0,0 +1,3 @@ +public abstract class Shape { + public abstract double getArea(); +} diff --git a/w4/ShapeUtil.java b/w4/ShapeUtil.java new file mode 100644 index 0000000..a5e9fac --- /dev/null +++ b/w4/ShapeUtil.java @@ -0,0 +1,5 @@ +public class ShapeUtil { + public static void printArea(Shape shape) { + System.out.println("面积为: " + shape.getArea()); + } +} \ No newline at end of file diff --git a/w4/Triangle.java b/w4/Triangle.java new file mode 100644 index 0000000..c1311c4 --- /dev/null +++ b/w4/Triangle.java @@ -0,0 +1,15 @@ +public class Triangle extends Shape { + private double a, b, c; + + public Triangle(double a, double b, double c) { + this.a = a; + this.b = b; + this.c = c; + } + + @Override + public double getArea() { + double s = (a + b + c) / 2; + return Math.sqrt(s * (s - a) * (s - b) * (s - c)); + } +}