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

函数题:MyPoint2

Luz2年前 (2022-11-10)题库543
设计一个名为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 void setLocation(double x, double y)方法, 用参数重新设置点的坐标,如果小于0,则设为0。
* public void move(double dx, double dy)方法, 横坐标移动dx,纵坐标移动dy,如果移动后坐标小于0,则设为0。
* 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.setLocation(x1, y1);
p2.setLocation(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));

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 2 3 4
-2 -1 -1 -1


### 输出样例:

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







答案:若无答案欢迎评论

发表评论

访客

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