编写一个Java Application程序,该程序有Point类、Circle类、Cylinder类以及测试类。
时间: 2024-05-22 12:14:48 浏览: 115
以下是一个可能的解决方案:
Point类:
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 double getY() {
return y;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public String toString() {
return "(" + x + ", " + y + ")";
}
}
Circle类:
public class Circle {
private Point center;
private double radius;
public Circle(Point center, double radius) {
this.center = center;
this.radius = radius;
}
public Point getCenter() {
return center;
}
public double getRadius() {
return radius;
}
public void setCenter(Point center) {
this.center = center;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public String toString() {
return "Circle with center " + center + " and radius " + radius;
}
}
Cylinder类:
public class Cylinder extends Circle {
private double height;
public Cylinder(Point center, double radius, double height) {
super(center, radius);
this.height = height;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getVolume() {
return getArea() * height;
}
public String toString() {
return "Cylinder with center " + getCenter() + ", radius " + getRadius() + " and height " + height;
}
}
测试类:
public class Test {
public static void main(String[] args) {
Point p = new Point(1, 2);
Circle c = new Circle(p, 3);
Cylinder cyl = new Cylinder(p, 3, 4);
System.out.println(p);
System.out.println(c);
System.out.println(c.getArea());
System.out.println(cyl);
System.out.println(cyl.getVolume());
}
}
运行测试类将输出以下内容:
(1.0, 2.0)
Circle with center (1.0, 2.0) and radius 3.0
28.274333882308138
Cylinder with center (1.0, 2.0), radius 3.0 and height 4.0
339.29200658769764
阅读全文