From 57a692112f583cb2ed68e3a23216d3ce9e2209b4 Mon Sep 17 00:00:00 2001 From: zhangsiyuan <3837703520@qq.com> Date: Sun, 29 Mar 2026 19:01:37 +0800 Subject: [PATCH] =?UTF-8?q?w4-=E5=BC=A0=E6=80=9D=E6=B8=8A-202401070104?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w4/ShapeUtil.java | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 w4/ShapeUtil.java diff --git a/w4/ShapeUtil.java b/w4/ShapeUtil.java new file mode 100644 index 0000000..06720cf --- /dev/null +++ b/w4/ShapeUtil.java @@ -0,0 +1,54 @@ +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 length; + private double height; + public Rectangle(double length,double height){ + this.length=length; + this.height=height; + } + @Override + public double getArea(){ + return length*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 base*height/2; + } +} + +public class ShapeUtil{ + public static void printArea(Shape shape){ + System.out.printf("أو»‎تا%.2f%n",shape.getArea()); + } + public static void main(String[] args){ + Shape circle = new Circle(5.0); + Shape rectangle = new Rectangle(10.0,20.0); + Shape triangle = new Triangle(3.0,4.0); + printArea(circle); + printArea(rectangle); + printArea(triangle); + } +} \ No newline at end of file