java1.设计一个Point类表示平面坐标系中的点。该类包括私有的double型的x和y属性。两个构造方法,一个是无参的构造方法,该方法中x和y的值为0;另外一个构造方法包含两个参数,分别为x和y赋值。为x和y属性提供setter和getter方法,重写Object类的hashcode和equals方法。最后重写Object类的toString()方法。 编写一个类,包含如下功能:(1)定义一个数组,数组的长度是50,数组元素是Point的对象。Point对象的x和y值随机赋值。 (2)判断数组中是否有两个相等的点(点的x和y的值相等) (3)指定一个点,求该点到其他点的距离(两种理解) (4)求距离最近的两个点。
时间: 2023-05-24 09:07:17 浏览: 425
Point类代码:
public class Point {
private double x;
private double y;
public Point() {
this.x = 0;
this.y = 0;
}
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;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(x);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
Point other = (Point) obj;
if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x)) return false;
if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y)) return false;
return true;
}
@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
}
}
主方法代码:
import java.util.Random;
public class Test {
public static void main(String[] args) {
Point[] points = new Point[50];
Random random = new Random();
// 随机生成Point对象
for (int i = 0; i < 50; i++) {
double x = random.nextDouble() * 10 + 1;
double y = random.nextDouble() * 10 + 1;
points[i] = new Point(x, y);
}
// 判断是否有相等的点
for (int i = 0; i < 50; i++) {
for (int j = i + 1; j < 50; j++) {
if (points[i].equals(points[j])) {
System.out.println("存在相等的点 " + points[i].toString() + " " + points[j].toString());
}
}
}
// 重点:求距离最近的两个点
Point p1 = points[0];
Point p2 = points[1];
double minDistance = getDistance(p1, p2);
for (int i = 0; i < 50; i++) {
for (int j = i + 1; j < 50; j++) {
double distance = getDistance(points[i], points[j]);
if (distance < minDistance) {
p1 = points[i];
p2 = points[j];
minDistance = distance;
}
}
}
System.out.println("距离最近的两个点分别是:" + p1.toString() + " " + p2.toString() + ",距离为:" + minDistance);
// 指定一个点,求该点到其他点的距离
Point targetPoint = new Point(5, 5);
for (int i = 0; i < 50; i++) {
double distance = getDistance(targetPoint, points[i]);
System.out.println("点 " + points[i].toString() + " 到点 " + targetPoint.toString() + " 的距离为:" + distance);
}
}
/**
* 计算两个点之间的距离
* 有两种理解:
* 1. 返回欧几里得距离
* 2. 返回两个点在x轴和y轴上的距离之和(曼哈顿距离)
*/
public static double getDistance(Point p1, Point p2) {
// 欧几里得距离公式
// double distance = Math.sqrt((p1.getX() - p2.getX()) * (p1.getX() - p2.getX()) + (p1.getY() - p2.getY()) * (p1.getY() - p2.getY()));
// 曼哈顿距离公式
double distance = Math.abs(p1.getX() - p2.getX()) + Math.abs(p1.getY() - p2.getY());
return distance;
}
}
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)