① 定义一个Point类,它包含两个属性:横坐标x和纵坐标y,和一个构造函数Point(double x, double y); ② 定义一个接口Shape,它包含两个抽象方法:计算面积的方法double computeArea()和计算周长的方法double computeDiameter();定义一个矩形Rectangle,它实现了接口Shape,并包含四个Point类型的私有属性A、B、C、D。分别为这四个属性定义getter和setter方法。定义Rectangle的一个构造函数Rectangle(Point A, Point B, Point C, Point D), 并验证这四个点是否能构成矩形(AB=CD, AD=BC, AB垂直于BC),如果不能构成矩形,为用户输出提示信息。另外,Rectangle中包含一个公有实例方法draw(),它负责在控制台打印信息“这是一个矩形”;定义一个公有类型ShapeTest,包含main方法。在main方法中,初始化一个矩、,并输出它的面积和周长。
时间: 2024-03-09 16:47:24 浏览: 145
默认构造函数-C++面向对象程序设计教程 第2章 类和对象
好的,我理解了你的问题。以下是对应的Java代码:
```
public class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
public interface Shape {
public abstract double computeArea();
public abstract double computeDiameter();
}
public class Rectangle implements Shape {
private Point A;
private Point B;
private Point C;
private Point D;
public Rectangle(Point A, Point B, Point C, Point D) {
if (checkRectangle(A, B, C, D)) {
this.A = A;
this.B = B;
this.C = C;
this.D = D;
} else {
System.out.println("这四个点不能构成矩形!");
}
}
private boolean checkRectangle(Point A, Point B, Point C, Point D) {
double AB = Math.sqrt(Math.pow(A.getX() - B.getX(), 2) + Math.pow(A.getY() - B.getY(), 2));
double CD = Math.sqrt(Math.pow(C.getX() - D.getX(), 2) + Math.pow(C.getY() - D.getY(), 2));
double AD = Math.sqrt(Math.pow(A.getX() - D.getX(), 2) + Math.pow(A.getY() - D.getY(), 2));
double BC = Math.sqrt(Math.pow(B.getX() - C.getX(), 2) + Math.pow(B.getY() - C.getY(), 2));
double AB_CD = Math.abs(AB - CD);
double AD_BC = Math.abs(AD - BC);
double cos = (A.getX() - B.getX()) * (C.getX() - B.getX()) + (A.getY() - B.getY()) * (C.getY() - B.getY());
if (AB_CD < 0.0001 && AD_BC < 0.0001 && cos < 0.0001) {
return true;
} else {
return false;
}
}
public Point getA() {
return A;
}
public void setA(Point A) {
this.A = A;
}
public Point getB() {
return B;
}
public void setB(Point B) {
this.B = B;
}
public Point getC() {
return C;
}
public void setC(Point C) {
this.C = C;
}
public Point getD() {
return D;
}
public void setD(Point D) {
this.D = D;
}
@Override
public double computeArea() {
double AB = Math.sqrt(Math.pow(A.getX() - B.getX(), 2) + Math.pow(A.getY() - B.getY(), 2));
double BC = Math.sqrt(Math.pow(B.getX() - C.getX(), 2) + Math.pow(B.getY() - C.getY(), 2));
return AB * BC;
}
@Override
public double computeDiameter() {
double AB = Math.sqrt(Math.pow(A.getX() - B.getX(), 2) + Math.pow(A.getY() - B.getY(), 2));
double BC = Math.sqrt(Math.pow(B.getX() - C.getX(), 2) + Math.pow(B.getY() - C.getY(), 2));
double CD = Math.sqrt(Math.pow(C.getX() - D.getX(), 2) + Math.pow(C.getY() - D.getY(), 2));
double AD = Math.sqrt(Math.pow(A.getX() - D.getX(), 2) + Math.pow(A.getY() - D.getY(), 2));
return AB + BC + CD + AD;
}
public void draw() {
System.out.println("这是一个矩形");
}
}
public class ShapeTest {
public static void main(String[] args) {
Point A = new Point(0, 0);
Point B = new Point(0, 2);
Point C = new Point(3, 2);
Point D = new Point(3, 0);
Rectangle rectangle = new Rectangle(A, B, C, D);
if (rectangle.getA() != null) {
System.out.println("矩形的面积为:" + rectangle.computeArea());
System.out.println("矩形的周长为:" + rectangle.computeDiameter());
rectangle.draw();
}
}
}
```
希望这个代码能够解决你的问题!
阅读全文