commit 2db76ce98e017b9445cddf32333f020ce358c445 Author: Thea Wang Date: Mon Apr 13 11:34:55 2026 +0800 完成W5作业 diff --git a/Circle.class b/Circle.class new file mode 100644 index 0000000..14f10cc Binary files /dev/null and b/Circle.class differ diff --git a/Circle.java b/Circle.java new file mode 100644 index 0000000..f47b982 --- /dev/null +++ b/Circle.java @@ -0,0 +1,17 @@ +public class Circle extends Shape { + private double radius; + + public Circle(double radius) { + this.radius = radius; + } + + @Override + public double getArea() { + return Math.PI * radius * radius; + } + + @Override + public void draw() { + System.out.println("Drawing a circle with radius " + radius); + } +} diff --git a/Main.class b/Main.class new file mode 100644 index 0000000..e4658e2 Binary files /dev/null and b/Main.class differ diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..ee47404 --- /dev/null +++ b/Main.java @@ -0,0 +1,19 @@ +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, 8); + + System.out.println("Circle:"); + ShapeUtil.printArea(circle); + ShapeUtil.drawShape(circle); + + System.out.println("Rectangle:"); + ShapeUtil.printArea(rectangle); + ShapeUtil.drawShape(rectangle); + + System.out.println("Triangle:"); + ShapeUtil.printArea(triangle); + ShapeUtil.drawShape(triangle); + } +} diff --git a/Rectangle.class b/Rectangle.class new file mode 100644 index 0000000..0a39ed7 Binary files /dev/null and b/Rectangle.class differ diff --git a/Rectangle.java b/Rectangle.java new file mode 100644 index 0000000..31137e6 --- /dev/null +++ b/Rectangle.java @@ -0,0 +1,19 @@ +public 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; + } + + @Override + public void draw() { + System.out.println("Drawing a rectangle with width " + width + " and height " + height); + } +} diff --git a/Shape.class b/Shape.class new file mode 100644 index 0000000..ea00333 Binary files /dev/null and b/Shape.class differ diff --git a/Shape.java b/Shape.java new file mode 100644 index 0000000..5cbc8c0 --- /dev/null +++ b/Shape.java @@ -0,0 +1,4 @@ +public abstract class Shape { + public abstract double getArea(); + public abstract void draw(); +} diff --git a/ShapeUtil.class b/ShapeUtil.class new file mode 100644 index 0000000..ad6ae4f Binary files /dev/null and b/ShapeUtil.class differ diff --git a/ShapeUtil.java b/ShapeUtil.java new file mode 100644 index 0000000..5c68741 --- /dev/null +++ b/ShapeUtil.java @@ -0,0 +1,9 @@ +public class ShapeUtil { + public static void printArea(Shape shape) { + System.out.println("Area: " + shape.getArea()); + } + + public static void drawShape(Shape s) { + s.draw(); + } +} diff --git a/Triangle.class b/Triangle.class new file mode 100644 index 0000000..3546078 Binary files /dev/null and b/Triangle.class differ diff --git a/Triangle.java b/Triangle.java new file mode 100644 index 0000000..a17e1ab --- /dev/null +++ b/Triangle.java @@ -0,0 +1,19 @@ +public 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; + } + + @Override + public void draw() { + System.out.println("Drawing a triangle with base " + base + " and height " + height); + } +}