如下给出抽象图形类(Graph)和坐标点类Point。 1)设计其抽象子类二维图形(TwoDGraph),添加计算面积calArea()和周长calPerimeter()两个抽象方法; 2)从TwoDGraph类派生出圆形Circle,增加圆心centerPoint和半径radius属性,增加构造方法; 3)在Circle中重写calArea、calPerimeter和toString()方法 4)从TwoDGraph类派生出长方形Rectangle,增加左上角坐标leftPoint、长length和宽width属性,增加构造方法; 5)在Rectangle中重写calArea、calPerimeter和toString()方法 6)在Rectangle新增输出4个顶点坐标方法outputPoints() 7)在main方法中,测试Circle和Rectangle类中的方法。 public abstract class Graph { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return ""; } } public class Point { private double x; private double y; public Point() { this(0,0); } 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 String toString() { return String.format("[%.2f, %.2f]", x,y); } }
时间: 2024-01-21 17:05:06 浏览: 32
好的,我已经理解了你的问题。以下是我的回答:
1. 二维图形类(TwoDGraph)的设计如下:
```java
public abstract class TwoDGraph extends Graph {
public abstract double calArea();
public abstract double calPerimeter();
}
```
2. 圆形类(Circle)的设计如下:
```java
public class Circle extends TwoDGraph {
private Point centerPoint;
private double radius;
public Circle(String name, Point centerPoint, double radius) {
setName(name);
this.centerPoint = centerPoint;
this.radius = radius;
}
public Point getCenterPoint() {
return centerPoint;
}
public void setCenterPoint(Point centerPoint) {
this.centerPoint = centerPoint;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double calArea() {
return Math.PI * radius * radius;
}
@Override
public double calPerimeter() {
return 2 * Math.PI * radius;
}
@Override
public String toString() {
return "Circle{" +
"name='" + getName() + '\'' +
", centerPoint=" + centerPoint +
", radius=" + radius +
'}';
}
}
```
3. 长方形类(Rectangle)的设计如下:
```java
public class Rectangle extends TwoDGraph {
private Point leftPoint;
private double length;
private double width;
public Rectangle(String name, Point leftPoint, double length, double width) {
setName(name);
this.leftPoint = leftPoint;
this.length = length;
this.width = width;
}
public Point getLeftPoint() {
return leftPoint;
}
public void setLeftPoint(Point leftPoint) {
this.leftPoint = leftPoint;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public double calArea() {
return length * width;
}
@Override
public double calPerimeter() {
return 2 * (length + width);
}
@Override
public String toString() {
return "Rectangle{" +
"name='" + getName() + '\'' +
", leftPoint=" + leftPoint +
", length=" + length +
", width=" + width +
'}';
}
public void outputPoints() {
Point rightPoint = new Point(leftPoint.getX() + length, leftPoint.getY());
Point bottomPoint = new Point(leftPoint.getX(), leftPoint.getY() - width);
Point rightBottomPoint = new Point(rightPoint.getX(), bottomPoint.getY());
System.out.println("The four points of the rectangle are:");
System.out.println(leftPoint + ", " + rightPoint + ", " + rightBottomPoint + ", " + bottomPoint);
}
}
```
4. 在main方法中,我们可以进行如下测试:
```java
public static void main(String[] args) {
Point centerPoint = new Point(0, 0);
Circle circle = new Circle("Circle1", centerPoint, 5);
System.out.println(circle);
System.out.println("Area: " + circle.calArea());
System.out.println("Perimeter: " + circle.calPerimeter());
Point leftPoint = new Point(0, 0);
Rectangle rectangle = new Rectangle("Rectangle1", leftPoint, 6, 4);
System.out.println(rectangle);
System.out.println("Area: " + rectangle.calArea());
System.out.println("Perimeter: " + rectangle.calPerimeter());
rectangle.outputPoints();
}
```
输出结果如下:
```
Circle{name='Circle1', centerPoint=[0.00, 0.00], radius=5.0}
Area: 78.53981633974483
Perimeter: 31.41592653589793
Rectangle{name='Rectangle1', leftPoint=[0.00, 0.00], length=6.0, width=4.0}
Area: 24.0
Perimeter: 20.0
The four points of the rectangle are:
[0.00, 0.00], [6.00, 0.00], [6.00, -4.00], [0.00, -4.00]
```