如下给出抽象图形类(Graph)和坐标点类Point。 1)设计其抽象子类二维图形(TwoDGraph),添加计算面积calArea()和周长calPerimeter()两个抽象方法; 2)从TwoDGraph类派生出圆形Circle,增加圆心centerPoint和半径radius属性,增加构造方法; 3)在Circle中重写calArea、calPerimeter和toString()方法 4)从TwoDGraph类派生出长方形Rectangle,增加左上角坐标leftPoint、长length和宽w
时间: 2024-03-26 18:34:57 浏览: 50
idth属性,增加构造方法; 5)在Rectangle中重写calArea、calPerimeter和toString()方法。
代码如下:
```
abstract class Graph {
// 抽象方法
public abstract void draw();
}
class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
// 省略getter、setter方法
}
abstract class TwoDGraph extends Graph {
// 增加计算面积和周长的抽象方法
public abstract double calArea();
public abstract double calPerimeter();
}
class Circle extends TwoDGraph {
private Point centerPoint;
private double radius;
public Circle(Point centerPoint, double radius) {
this.centerPoint = centerPoint;
this.radius = radius;
}
@Override
public double calArea() {
return Math.PI * radius * radius;
}
@Override
public double calPerimeter() {
return 2 * Math.PI * radius;
}
@Override
public void draw() {
// 绘制圆形
}
@Override
public String toString() {
return "Circle [centerPoint=" + centerPoint + ", radius=" + radius + "]";
}
}
class Rectangle extends TwoDGraph {
private Point leftPoint;
private double length;
private double width;
public Rectangle(Point leftPoint, double length, double width) {
this.leftPoint = leftPoint;
this.length = length;
this.width = width;
}
@Override
public double calArea() {
return length * width;
}
@Override
public double calPerimeter() {
return 2 * (length + width);
}
@Override
public void draw() {
// 绘制长方形
}
@Override
public String toString() {
return "Rectangle [leftPoint=" + leftPoint + ", length=" + length + ", width=" + width + "]";
}
}
```
阅读全文