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 22:02:44 浏览: 127
好的,这是一个关于Java的面向对象编程的问题。以下是代码实现:
```java
public abstract class TwoDGraph extends Graph {
public abstract double calArea();
public abstract double calPerimeter();
}
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 double calArea() {
return Math.PI * radius * radius;
}
public double calPerimeter() {
return 2 * Math.PI * radius;
}
public String toString() {
return "Circle " + getName() + ": center = " + centerPoint.toString() + ", radius = " + radius;
}
}
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 double calArea() {
return length * width;
}
public double calPerimeter() {
return 2 * (length + width);
}
public String toString() {
return "Rectangle " + getName() + ": left point = " + leftPoint.toString() + ", 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 diagonalPoint = new Point(leftPoint.getX() + length, leftPoint.getY() - width);
System.out.println("The four points of " + getName() + " are:");
System.out.println(leftPoint.toString() + ", " + rightPoint.toString() + ", " + bottomPoint.toString() + ", " + diagonalPoint.toString());
}
}
public class Test {
public static void main(String[] args) {
Circle circle = new Circle("c1", new Point(0,0), 5);
System.out.println(circle.toString());
System.out.println("Area: " + circle.calArea());
System.out.println("Perimeter: " + circle.calPerimeter());
System.out.println();
Rectangle rectangle = new Rectangle("r1", new Point(0,0), 5, 3);
System.out.println(rectangle.toString());
System.out.println("Area: " + rectangle.calArea());
System.out.println("Perimeter: " + rectangle.calPerimeter());
rectangle.outputPoints();
}
}
```
在这个代码实现中,我们首先创建了一个抽象的二维图形类TwoDGraph,它包含了计算面积和周长的抽象方法。然后我们从TwoDGraph中派生出了圆形类Circle和长方形类Rectangle。这两个类都实现了抽象方法,同时也重写了toString方法以便于输出信息。在Rectangle类中,我们还新增了输出四个顶点坐标的方法outputPoints。最后,在main方法中,我们对Circle和Rectangle类中的方法进行了测试。
阅读全文