一、习题介绍
第十章
Programming Exercise:10.4,10.11
二、习题及答案
10.4 (The MyPoint class) Design a class named MyPoint to represent a point with
x- and y-coordinates. The class contains:
■ The data fields x and y that represent the coordinates with getter methods.
■ A no-arg constructor that creates a point (0, 0).
■ A constructor that constructs a point with specified coordinates.
■ A method named distance that returns the distance from this point to a specified point of the MyPoint type.
■ A method named distance that returns the distance from this point to another point with specified x- and y-coordinates.
Draw the UML diagram for the class and then implement the class.
Write a test program that creates the two points (0, 0) and (10, 30.5) and displays the
distance between them.
10.4 (MyPoint类)设计一个名为MyPoint的类来表示一个点X和y坐标。这个类包含:
数据字段x和y用getter表示坐标方法。
创建一个点(0,0)的无参数构造函数。
构造具有指定坐标的点的构造函数。
一个名为distance的方法,返回从该点到A的距离MyPoint类型的指定点。
一个名为distance的方法,返回从该点到另一个具有指定x和y坐标的点。
为类绘制UML图,然后实现类。写一个创建两个点(0,0)和(10,30.5)并显示它们之间的距离。
UML类图
+--------+
| MyPoint|
+--------+
| -x: double |
| -y: double |
+--------+
| +MyPoint() |
| +MyPoint(x: double, y: double) |
| +getX(): double |
| +getY(): double |
| +distance(p: MyPoint): double |
| +distance(x: double, y: double): double |
+--------+
public class MyPoint {
private double x;
private double y;
// 无参数构造函数
public MyPoint() {
this(0, 0);
}
// 带参数的构造函数
public MyPoint(double x, double y) {
this.x = x;
this.y = y;
}
// 获取x坐标
public double getX() {
return x;
}
// 获取y坐标
public double getY() {
return y;
}
// 计算与另一个点的距离
public double distance(MyPoint other) {
return Math.sqrt(Math.pow(this.x - other.x, 2) + Math.pow(this.y - other.y, 2));
}
}
*10.11 (Geometry: the Circle2D class) Define the Circle2D class that contains:
■ Two double data fields named x and y that specify the center of the circle with getter methods.
■ A data field radius with a getter method.
■ A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1 for radius.
■ A constructor that creates a circle with the specified x, y, and radius.
■ A method getArea() that returns the area of the circle.
■ A method getPerimeter() that returns the perimeter of the circle.
■ A method contains(double x, double y) that returns true if the specified point (x, y) is inside this circle (see Figure 10.21a).
■ A method contains(Circle2D circle) that returns true if the specified circle is inside this circle (see Figure 10.21b).
■ A method overlaps(Circle2D circle) that returns true if the specified circle overlaps with this circle (see Figure 10.21c).
FIGURE 10.21
(a) A point is inside the circle. (b) A circle is inside another circle. (c) A circle overlaps another circle.
Draw the UML diagram for the class and then implement the class. Write a test program that creates a Circle2D object c1 (new Circle2D(2, 2, 5.5)), displays its area and perimeter, and displays the result of c1.contains(3, 3), c1.contains(new Circle2D(4, 5, 10.5)), and c1.overlaps(new Circle2D(3, 5, 2.3)).
*10.11(几何:Circle2D类)定义Circle2D类包含:
两个名为x和y的双数据字段指定圆心用getter方法。
带有getter方法的数据域半径。
一个无参数构造函数,用于以(0,0)为默认(x, y)和以1为半径创建一个圆。
一个构造函数,创建一个具有指定x, y和半径的圆。
getArea()方法返回圆的面积。
getPerimeter()方法返回圆的周长。
一个方法包含(双精度x,双精度y),如果
指定点(x, y)在圆内(见图10.21a)。
一个方法包含(Circle2D circle),当指定时返回true
在这个圆的内部(见图10.21b)。
一个方法overlaps(Circle2D circle),当指定时返回true另一个圆与这个圆重叠(见图10.21c)。
(a)一个点在圆内。(b)圆圈在另一个圆圈内。(c)圆圈与另一个圆重叠。
为类绘制UML图,然后实现类。
编写测试程序创建一个Circle2D对象c1 (new Circle2D(2,2,5.5)),显示其面积和周长,并显示c1.contains(3,3), c1
UML类图
+--------+
| MyPoint|
+--------+
| -x: double |
| -y: double |
+--------+
| +MyPoint() |
| +MyPoint(x: double, y: double) |
| +getX(): double |
| +getY(): double |
| +distance(p: MyPoint): double |
| +distance(x: double, y: double): double |
+--------+
public class Circle2D {
private double x;
private double y;
private double radius;
// 无参数构造函数
public Circle2D() {
this(0, 0, 1);
}
// 带参数的构造函数
public Circle2D(double x, double y, double radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
// 获取圆心x坐标
public double getX() {
return x;
}
// 获取圆心y坐标
public double getY() {
return y;
}
// 获取半径
public double getRadius() {
return radius;
}
// 计算圆的面积
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}
// 计算圆的周长
public double getPerimeter() {
return 2 * Math.PI * radius;
}
// 判断点是否在圆内
public boolean contains(double x, double y) {
return (x - this.x) * (x - this.x) + (y - this.y) * (y - this.y) <= radius * radius;
}
// 判断一个圆是否在这个圆内
public boolean contains(Circle2D circle) {
return (circle.x - this.x) * (circle.x - this.x) + (circle.y - this.y) * (circle.y - this.y) <= (circle.radius + this.radius) * (circle.radius + this.radius);
}
// 判断两个圆是否重叠
public boolean overlaps(Circle2D circle) {
return (circle.x - this.x) * (circle.x - this.x) + (circle.y - this.y) * (circle.y - this.y) <= (circle.radius + this.radius) * (circle.radius + this.radius);
}
}
public class Test {
public static void main(String[] args) {
// 创建两个点
MyPoint point1 = new MyPoint();
MyPoint point2 = new MyPoint(10, 30.5);
// 显示它们之间的距离
System.out.println("Distance between points: " + point1.distance(point2));
// 创建一个圆
Circle2D c1 = new Circle2D(2, 2, 5.5);
// 显示圆的面积和周长
System.out.println("Area of circle c1: " + c1.getArea());
System.out.println("Perimeter of circle c1: " + c1.getPerimeter());
// 显示点是否在圆内
System.out.println("c1 contains (3, 3): " + c1.contains(3, 3));
}
}
执行结果 :
结语
不为失败找理由
只为成功找方法
!!!