1.设计一个Point类表示平面坐标系中的点。该类包括私有的double型的x和y属性。两个构造方法,一个是无参的构造方法,该方法中×和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-30 21:06:03 浏览: 129
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 + "]";
}
}
```
基于Point类,实现包含如下功能的类:
```
import java.util.Random;
public class PointArray {
private Point[] points;
public PointArray() {
this.points = new Point[50];
Random random = new Random();
for (int i = 0; i < 50; i++) {
double x = random.nextDouble() * 100;
double y = random.nextDouble() * 100;
points[i] = new Point(x, y);
}
}
public boolean hasEqualPoints() {
for (int i = 0; i < points.length - 1; i++) {
for (int j = i + 1; j < points.length; j++) {
if (points[i].equals(points[j])) {
return true;
}
}
}
return false;
}
public double[] distancesTo(Point p) {
double[] distances = new double[points.length];
for (int i = 0; i < points.length; i++) {
distances[i] = Math.sqrt(Math.pow(points[i].getX() - p.getX(), 2) + Math.pow(points[i].getY() - p.getY(), 2));
}
return distances;
}
public Point[] closestPoints() {
Point[] closest = new Point[2];
double minDistance = Double.MAX_VALUE;
for (int i = 0; i < points.length - 1; i++) {
for (int j = i + 1; j < points.length; j++) {
double distance = Math.sqrt(Math.pow(points[i].getX() - points[j].getX(), 2) + Math.pow(points[i].getY() - points[j].getY(), 2));
if (distance < minDistance) {
minDistance = distance;
closest[0] = points[i];
closest[1] = points[j];
}
}
}
return closest;
}
}
```
使用示例:
```
public class Main {
public static void main(String[] args) {
PointArray pa = new PointArray();
System.out.println(pa.hasEqualPoints());
Point p = new Point(50, 50);
double[] distances = pa.distancesTo(p);
for (int i = 0; i < distances.length; i++) {
System.out.println("distance to point " + i + ": " + distances[i]);
}
Point[] closest = pa.closestPoints();
System.out.println("closest points: " + closest[0] + ", " + closest[1]);
}
}
```
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.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)