diff --git a/.idea/compiler.xml b/.idea/compiler.xml
new file mode 100644
index 0000000000000000000000000000000000000000..d1120094d9ec720a6c591261358a49402f999e0b
--- /dev/null
+++ b/.idea/compiler.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml
new file mode 100644
index 0000000000000000000000000000000000000000..712ab9d985c20018a0c97b93d2148ac1ffe588a5
--- /dev/null
+++ b/.idea/jarRepositories.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000000000000000000000000000000000000..789324dc2079f4bbc966288738bb0948de6486e2
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 0c6b7f84dfde2bd64b22d1c90b432e8ba70231de..7e7a4c319ebf05d3583b335a3aa582c576447211 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -1,5 +1,8 @@
+
+
+
@@ -11,7 +14,14 @@
-
+
+
+
+
+
+
+
+
@@ -31,25 +41,35 @@
-
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
@@ -67,10 +87,19 @@
-
+
+
+
+
+
+
+
+
+
+
@@ -101,7 +130,7 @@
-
+
@@ -119,26 +148,4 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/src/main/java/cn/zust/edu/cn/App.java b/src/main/java/cn/zust/edu/cn/App.java
index bf9c122443f372c99495cef0daf5d07e13abdff6..4ef7c4cd245e1e5c68c61803f12cbf9060e510a3 100644
--- a/src/main/java/cn/zust/edu/cn/App.java
+++ b/src/main/java/cn/zust/edu/cn/App.java
@@ -1,4 +1,9 @@
package cn.zust.edu.cn;
+
+import cn.zust.edu.cn.shape.Circle;
+import cn.zust.edu.cn.shape.Rectangle;
+import cn.zust.edu.cn.shape.Triangle;
+
/**
*
* @author czg
@@ -6,7 +11,19 @@ package cn.zust.edu.cn;
*/
public class App {
public static void main(String[] args) {
- System.out.println("hello world!");
+ ShapeManger shapeManager=new ShapeManger();
+ Rectangle rect=new Rectangle(new Point(10,10),100,200);
+ Triangle tri=new Triangle(new Point(0,10),new Point(50,50),new Point(150,0));
+ Circle cir=new Circle(100,new Point(100,100));
+ System.out.println("进行第一次绘制:");
+ shapeManager.addShape(rect);
+ shapeManager.addShape(tri);
+ shapeManager.addShape(cir);
+ shapeManager.drawShapes();
+ System.out.println("-----------------------------------------------------------------------------------------------");
+ System.out.println("进行第二次绘制:");
+ shapeManager.delShape(rect);
+ shapeManager.drawShapes();
}
}
diff --git a/src/main/java/cn/zust/edu/cn/IShape.java b/src/main/java/cn/zust/edu/cn/IShape.java
new file mode 100644
index 0000000000000000000000000000000000000000..84ba88b38ffbbac7d17e81970f678d4d32a2de16
--- /dev/null
+++ b/src/main/java/cn/zust/edu/cn/IShape.java
@@ -0,0 +1,7 @@
+package cn.zust.edu.cn;
+
+public interface IShape {
+ double perimeter();//周长
+ double area();//面积
+ void draw();//绘制
+}
diff --git a/src/main/java/cn/zust/edu/cn/Point.java b/src/main/java/cn/zust/edu/cn/Point.java
index 5661ad0dc525c7f45779a6b9a2d09b9be0a440b6..62049f6d702d47441daa576d6c981b19e1f48902 100644
--- a/src/main/java/cn/zust/edu/cn/Point.java
+++ b/src/main/java/cn/zust/edu/cn/Point.java
@@ -5,5 +5,22 @@ package cn.zust.edu.cn;
* @since 2023/3/7 10:50
*/
public class Point {
+ public int x;
+ public int y;
+ //无参构造函数
+ public Point() {
+ }
+
+ //有参构造函数
+ public Point(int x, int y) {
+ this.x = x;
+ this.y = y;
+ }
+
+ // 拷贝构造函数
+ public Point(Point p) {
+ this.x = p.x;
+ this.y = p.y;
+ }
}
diff --git a/src/main/java/cn/zust/edu/cn/ShapeManger.java b/src/main/java/cn/zust/edu/cn/ShapeManger.java
new file mode 100644
index 0000000000000000000000000000000000000000..24fa9b1147c8cdd9dd75b9907325c3db1348c16a
--- /dev/null
+++ b/src/main/java/cn/zust/edu/cn/ShapeManger.java
@@ -0,0 +1,22 @@
+package cn.zust.edu.cn;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class ShapeManger {
+ private final List shapes ;
+ public ShapeManger(){
+ shapes = new ArrayList<>();
+ }
+ public void addShape(IShape shape){
+ shapes.add(shape);
+ }
+ public void delShape(IShape shape){
+ shapes.remove(shape);
+ }
+ public void drawShapes(){
+ for (IShape shape:shapes){
+ shape.draw();
+ }
+ }
+}
diff --git a/src/main/java/cn/zust/edu/cn/shape/Circle.java b/src/main/java/cn/zust/edu/cn/shape/Circle.java
new file mode 100644
index 0000000000000000000000000000000000000000..878c14066be5a0701f6024ed051cbc79ba7f5bf4
--- /dev/null
+++ b/src/main/java/cn/zust/edu/cn/shape/Circle.java
@@ -0,0 +1,29 @@
+package cn.zust.edu.cn.shape;
+
+import cn.zust.edu.cn.IShape;
+import cn.zust.edu.cn.Point;
+
+public class Circle implements IShape {
+ private Point ptCenter; // 圆心位置
+ private int radius;// 半径
+
+ public Circle(int r, Point Center) {
+ this.radius = r;
+ this.ptCenter = Center;
+ }
+
+ @Override
+ public double perimeter() {
+ return 2 * Math.PI * radius;
+ }
+
+ @Override
+ public double area() {
+ return Math.PI * radius * radius;
+ }
+
+ @Override
+ public void draw() {
+ System.out.println("将把圆形的圆心绘制在坐标(" + ptCenter.x + ", " + ptCenter.y + "),并且它的半径长为 " + radius);
+ }
+}
diff --git a/src/main/java/cn/zust/edu/cn/shape/Rectangle.java b/src/main/java/cn/zust/edu/cn/shape/Rectangle.java
new file mode 100644
index 0000000000000000000000000000000000000000..a7fc64d0200f3a796fb10395de2d9b814b099fbf
--- /dev/null
+++ b/src/main/java/cn/zust/edu/cn/shape/Rectangle.java
@@ -0,0 +1,38 @@
+package cn.zust.edu.cn.shape;
+
+import cn.zust.edu.cn.IShape;
+import cn.zust.edu.cn.Point;
+
+/**
+ *
+ * @author czg
+ * @since 2023/3/7 10:50
+ */
+public class Rectangle implements IShape {
+ Point ptTopLeft;
+ Point ptBottomRight;
+ int width;
+ int height;
+
+ public Rectangle() {
+ }
+
+ public Rectangle(Point ptTopLeft, int width, int height) {
+ this.ptTopLeft = ptTopLeft;
+ this.width = width;
+ this.height = height;
+ }
+
+ public double perimeter(){
+ return 2*(width+height);
+ }
+ public double area(){
+ return width*height;
+ }
+ public void draw(){
+ int rightbottom1=ptTopLeft.x+width;
+ int rightbottom2=ptTopLeft.y-height;
+ System.out.println("将把矩形的左上角绘制在坐标 (" + ptTopLeft.x + ", " + ptTopLeft.y + "),其右下角绘制在坐标 ("+rightbottom1+", "+rightbottom2+"),其宽度为 " + width + ",高度为 " + height);
+ }
+
+}
diff --git a/src/main/java/cn/zust/edu/cn/shape/Triangle.java b/src/main/java/cn/zust/edu/cn/shape/Triangle.java
new file mode 100644
index 0000000000000000000000000000000000000000..cb7b8ee8cd83b611c6a8ab5a3438246e23bb7ce2
--- /dev/null
+++ b/src/main/java/cn/zust/edu/cn/shape/Triangle.java
@@ -0,0 +1,54 @@
+package cn.zust.edu.cn.shape;
+import cn.zust.edu.cn.IShape;
+import cn.zust.edu.cn.Point;
+
+public class Triangle implements IShape {
+ //三角形坐标
+ private Point ptA;
+ private Point ptB;
+ private Point ptC;
+
+ public Triangle(Point ptA, Point ptB, Point ptC) {
+ this.ptA = ptA;
+ this.ptB = ptB;
+ this.ptC = ptC;
+ }
+
+ // 只读属性:边长
+ public double getEdgeA() {
+ return distance(ptB, ptC);
+ }
+
+ public double getEdgeB() {
+ return distance(ptA, ptC);
+ }
+
+ public double getEdgeC() {
+ return distance(ptA, ptB);
+ }
+
+ // 计算两点间的距离
+ private double distance(Point p1, Point p2) {
+ return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
+ }
+ @Override
+ public double perimeter() {
+ return getEdgeA() + getEdgeB() + getEdgeC();
+
+ }
+
+ @Override
+ public double area() {
+ //利用海伦公式
+ double s=perimeter()/2;
+ return Math.sqrt(s * (s - getEdgeA()) * (s - getEdgeB()) * (s - getEdgeC()));
+ }
+
+ @Override
+ public void draw() {
+ System.out.println("将把三角形的顶点分别绘制在坐标 ("
+ + ptA.x + ", " + ptA.y + ")、("
+ + ptB.x + ", " + ptB.y + ") 和 ("
+ + ptC.x + ", " + ptC.y + ")");
+ }
+}
diff --git a/target/classes/cn/zust/edu/cn/App.class b/target/classes/cn/zust/edu/cn/App.class
new file mode 100644
index 0000000000000000000000000000000000000000..b409be245463150863a68bcd68be3ecb1972c368
Binary files /dev/null and b/target/classes/cn/zust/edu/cn/App.class differ
diff --git a/target/classes/cn/zust/edu/cn/IShape.class b/target/classes/cn/zust/edu/cn/IShape.class
new file mode 100644
index 0000000000000000000000000000000000000000..2eacc522c098bb3ecf3b6e4098db6d3e398adcb2
Binary files /dev/null and b/target/classes/cn/zust/edu/cn/IShape.class differ
diff --git a/target/classes/cn/zust/edu/cn/Point.class b/target/classes/cn/zust/edu/cn/Point.class
new file mode 100644
index 0000000000000000000000000000000000000000..00802000ccbf9204afa4dee16e59952bf18db815
Binary files /dev/null and b/target/classes/cn/zust/edu/cn/Point.class differ
diff --git a/target/classes/cn/zust/edu/cn/Rectangle.class b/target/classes/cn/zust/edu/cn/Rectangle.class
new file mode 100644
index 0000000000000000000000000000000000000000..5835193bbd8eaee2eacda9a0de7f8e79bc0ebc33
Binary files /dev/null and b/target/classes/cn/zust/edu/cn/Rectangle.class differ
diff --git a/target/classes/cn/zust/edu/cn/ShapeManger.class b/target/classes/cn/zust/edu/cn/ShapeManger.class
new file mode 100644
index 0000000000000000000000000000000000000000..ff58b03fb71341470a020335aef6eef2cc87c7cd
Binary files /dev/null and b/target/classes/cn/zust/edu/cn/ShapeManger.class differ
diff --git a/target/classes/cn/zust/edu/cn/shape/Circle.class b/target/classes/cn/zust/edu/cn/shape/Circle.class
new file mode 100644
index 0000000000000000000000000000000000000000..cee23fd2808294b37967b708f596422581f6ba2c
Binary files /dev/null and b/target/classes/cn/zust/edu/cn/shape/Circle.class differ
diff --git a/target/classes/cn/zust/edu/cn/shape/Rectangle.class b/target/classes/cn/zust/edu/cn/shape/Rectangle.class
new file mode 100644
index 0000000000000000000000000000000000000000..186c501b7117d74d0b0af72257f15065eea0890a
Binary files /dev/null and b/target/classes/cn/zust/edu/cn/shape/Rectangle.class differ
diff --git a/target/classes/cn/zust/edu/cn/shape/Triangle.class b/target/classes/cn/zust/edu/cn/shape/Triangle.class
new file mode 100644
index 0000000000000000000000000000000000000000..94d18548ceb7d3c13520588acfaf5b5ba4dbb0b5
Binary files /dev/null and b/target/classes/cn/zust/edu/cn/shape/Triangle.class differ