-->
当前位置:首页 > 题库 > 正文内容

函数题:MyPoint3

Luz2年前 (2022-11-10)题库488
* 设计一个名为MyPoint的不可变类,它代表一个坐标为x,y的点(x和y不能为负数)。类包含:
* 私有成员变量x和y代表坐标,类型为double。
* 一个不带参数的构造方法MyPoint(),x和y都设为0.0。
* 一个有参构造方法MyPoint(double x,double y),构造一个指定的坐标点,如果x和y小于0,则设为0。
* 为x和y添加getter方法,都为公共方法。
* public MyPoint move(double dx, double dy)方法, 生成一个横坐标移动dx,纵坐标移动dy后的新的MyPoint对象(如果移动后坐标小于0,则设为0)。
* public double distance(MyPoint p)方法, 返回该点到指定点p的距离。

已知两个点p1和p2的坐标,输出它们之间的距离。对p1和p2分别进行一次移动操作生成p3和p4,输出移动后p1和p2之间的距离,以及p3和p4之间的距离。
### 裁判测试程序样例:
c++
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
MyPoint p1 = new MyPoint(x1,y1);
MyPoint p2 = new MyPoint(x2,y2);
MyPoint p3;
MyPoint p4;
System.out.printf("distance between Point(%.2f,%.2f) and Point(%.2f,%.2f) is %.2f\n",
p1.getX(), p1.getY(), p2.getX(), p2.getY(), p1.distance(p2));
double dx1 = input.nextDouble();
double dy1 = input.nextDouble();
double dx2 = input.nextDouble();
double dy2 = input.nextDouble();
p3 = p1.move(dx1,dy1);
p4 = p2.move(dx2,dy2);
System.out.printf("distance between Point(%.2f,%.2f) and Point(%.2f,%.2f) is %.2f\n",
p1.getX(), p1.getY(), p2.getX(), p2.getY(), p1.distance(p2));
System.out.printf("distance between Point(%.2f,%.2f) and Point(%.2f,%.2f) is %.2f\n",
p3.getX(), p3.getY(), p4.getX(), p4.getY(), p3.distance(p4));
}
}

/* 请在这里填写答案 */


### 输入样例:

in
1 2 3 4
-2 -2 -2 -2


### 输出样例:

out
distance between Point(1.00,2.00) and Point(3.00,4.00) is 2.83
distance between Point(1.00,2.00) and Point(3.00,4.00) is 2.83
distance between Point(0.00,0.00) and Point(1.00,2.00) is 2.24







答案:若无答案欢迎评论

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。