(2)定义Point类,每个点包含(x,y)坐标,利用随机函数产生10个点的(x,y)值,将创建的 Point实例存入一个数组中,按与原点(0,0)的距离由小到大的顺序输出所有的点及到原点的距离。
时间: 2024-05-12 12:20:57 浏览: 21
```
import math
import random
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def distance_to_origin(self):
return math.sqrt(self.x ** 2 + self.y ** 2)
# 生成10个随机点
points = []
for i in range(10):
x = random.uniform(-10, 10)
y = random.uniform(-10, 10)
points.append(Point(x, y))
# 按到原点的距离排序
points.sort(key=lambda point: point.distance_to_origin())
# 输出所有点及到原点的距离
for point in points:
print("({:.2f}, {:.2f}) - {:.2f}".format(point.x, point.y, point.distance_to_origin()))
```
相关问题
用Java实现定义Point类,每个点包含(x,y)坐标,利用随机函数产生10个点的(x,y)值,将创建的 Point实例存入一个数组中,按与原点(0,0)的距离由小到大的顺序输出所有的点及到原点的距离。
好的,我来给您提供Java代码实现:
```java
import java.util.Arrays;
import java.util.Random;
public class Point implements Comparable<Point> {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public double distance() {
return Math.sqrt(x * x + y * y);
}
@Override
public int compareTo(Point o) {
return Double.compare(this.distance(), o.distance());
}
@Override
public String toString() {
return "(" + x + "," + y + ")";
}
public static void main(String[] args) {
Point[] points = new Point[10];
Random random = new Random();
for (int i = 0; i < 10; i++) {
int x = random.nextInt(100);
int y = random.nextInt(100);
points[i] = new Point(x, y);
}
Arrays.sort(points);
for (Point p : points) {
System.out.println(p + " distance: " + p.distance());
}
}
}
```
这里定义了一个Point类,包含x和y两个坐标,以及计算与原点之间距离的distance()方法。为了方便排序,实现了Comparable接口,按照与原点距离由小到大的顺序进行排序。在main方法中,利用随机函数生成10个点的坐标,存入Point数组中,然后对数组进行排序,最后按照顺序输出每个点及其到原点的距离。
java语言编写定义Point类,每个点包含(x,y)坐标,利用随机函数产生10个点的(x,y)值,将创建的 Point实例存入一个数组中,按与原点(0,0)的距离由小到大的顺序输出所有的点及到原点的距离。
下面是Java语言编写的Point类以及实现上述要求的代码:
```
import java.util.Arrays;
import java.util.Random;
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public double distanceFromOrigin() {
return Math.sqrt(x * x + y * y);
}
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
public static void main(String[] args) {
Point[] points = new Point[10];
Random rand = new Random();
for (int i = 0; i < points.length; i++) {
int x = rand.nextInt(101) - 50; // 产生-50到50之间的随机整数
int y = rand.nextInt(101) - 50;
points[i] = new Point(x, y);
}
Arrays.sort(points, (a, b) -> Double.compare(a.distanceFromOrigin(), b.distanceFromOrigin()));
for (Point p : points) {
System.out.println(p + " distance from origin: " + p.distanceFromOrigin());
}
}
}
```
代码中,定义了一个Point类,每个Point对象包含x和y坐标以及计算到原点距离的方法distanceFromOrigin()和toString()方法。在main()方法中,首先定义了一个长度为10的Point数组,然后使用Random类产生-50到50之间的随机整数作为每个点的x和y坐标,并将产生的Point对象存入数组中。接着,使用Arrays类的sort()方法将数组按到原点距离从小到大排序,其中使用lambda表达式实现Comparator接口的compare()方法。最后,遍历数组并输出每个点及其到原点的距离。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)