From 297d65762811f63e3fa5e892ce9e4af35ff55002 Mon Sep 17 00:00:00 2001 From: dengxitong <2452879460@qq.com> Date: Sun, 31 May 2026 13:43:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20'w4/ShapeAreaCalculator.ja?= =?UTF-8?q?va'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w4/ShapeAreaCalculator.java | 72 +++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 w4/ShapeAreaCalculator.java diff --git a/w4/ShapeAreaCalculator.java b/w4/ShapeAreaCalculator.java new file mode 100644 index 0000000..6fd8dd3 --- /dev/null +++ b/w4/ShapeAreaCalculator.java @@ -0,0 +1,72 @@ +package src.main.w5; +abstract class Shape { + public abstract double getArea(); +} + +class Circle extends Shape { + private double radius; + + public Circle(double radius) { + this.radius = radius; + } + + @Override + public double getArea() { + return Math.PI * radius * radius; + } +} +class Rectangle extends Shape { + private double width; + private double height; + + public Rectangle(double width, double height) { + this.width = width; + this.height = height; + } + + @Override + public double getArea() { + return width * height; + } +} + +class Triangle extends Shape { + private double base; + private double height; + + public Triangle(double base, double height) { + this.base = base; + this.height = height; + } + + @Override + public double getArea() { + return 0.5 * base * height; + } +} + +class ShapeUtil { + public static void printArea(Shape shape) { + System.out.printf("该图形的面积为:%.2f%n", shape.getArea()); + } +} + +public class ShapeAreaCalculator { + public static void main(String[] args) { + Shape circle = new Circle(2.0); + Shape rectangle = new Rectangle(3.0, 4.0); + Shape triangle = new Triangle(3.0, 4.0); + + System.out.println("圆形:"); + ShapeUtil.printArea(circle); + + System.out.println("矩形:"); + ShapeUtil.printArea(rectangle); + + System.out.println("三角形:"); + ShapeUtil.printArea(triangle); + } +} + + +