定义一个点Point类,它包含横坐标x和纵坐标y两个私有属性,包含一个无参构造方法和一个有参构造方法。 //2)定义一个圆Circle类,它包含圆心center(类型为Point类)和半径radius两个私有属性, //包含一个有参构造方法,完成圆的参数设置;一个area()方法返回圆的面积;一个show()方法完成圆心、半径及面积的输出。 //3)定义CircleTest测试类,要求通过键盘输入圆心和半径。
时间: 2023-06-16 20:02:54 浏览: 173
下面是代码实现:
```
import java.util.Scanner;
class Point {
private double x;
private double y;
public Point() {
}
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;
}
}
class Circle {
private Point center;
private double radius;
public Circle(Point center, double radius) {
this.center = center;
this.radius = radius;
}
public double area() {
return Math.PI * radius * radius;
}
public void show() {
System.out.println("圆心坐标为(" + center.getX() + "," + center.getY() + ")");
System.out.println("半径为" + radius);
System.out.println("面积为" + area());
}
}
public class CircleTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 输入圆心坐标
System.out.print("请输入圆心横坐标:");
double x = scanner.nextDouble();
System.out.print("请输入圆心纵坐标:");
double y = scanner.nextDouble();
Point center = new Point(x, y);
// 输入半径
System.out.print("请输入圆的半径:");
double radius = scanner.nextDouble();
Circle circle = new Circle(center, radius);
circle.show();
}
}
```
运行结果示例:
```
请输入圆心横坐标:3
请输入圆心纵坐标:4
请输入圆的半径:5
圆心坐标为(3.0,4.0)
半径为5.0
面积为78.53981633974483
```
阅读全文