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

函数题:MyPoint 1

Luz2年前 (2022-11-10)题库506
设计一个名为MyPoint的类,它代表一个坐标为x,y的点。类包含:
* 私有变量x和y代表坐标,类型为double。
* 一个不带参数的构造函数MyPoint(),x和y都设为0.0。
* 一个有参构造函数MyPoint(double x,double y),构造一个指定的坐标点。
* 为x和y添加getter和setter方法,都为公共方法。
* public void move(double dx, double dy)方法, 横坐标移动dx,纵坐标移动dy。
* public double distance(MyPoint p)方法, 返回该点到指定点p的距离。

已知两个点p1和p2的坐标,输出它们之间的距离。
对p1和p2分别进行一次移动操作,输出移动后它们之间的距离。

### 裁判测试程序样例:
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);
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));

x1 = input.nextDouble();
y1 = input.nextDouble();
x2 = input.nextDouble();
y2 = input.nextDouble();
p1.setX(x1);
p1.setY(y1);
p2.setX(x2);
p2.setY(y2);
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();
p1.move(dx1,dy1);
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));
}
}
/* 请在这里填写答案 */


### 输入样例:

in
0 1 3 5
1 1 2 2
1 1 -1 0


### 输出样例:

out
distance between Point(0.00,1.00) and Point(3.00,5.00) is 5.00
distance between Point(1.00,1.00) and Point(2.00,2.00) is 1.41
distance between Point(2.00,2.00) and Point(1.00,2.00) is 1.00







答案:若无答案欢迎评论

发表评论

访客

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