阅读以下说明和 Java 程序,填充程序中的空缺,将解答填入答题纸的对应栏内。
【说明】
下面的程序用来计算并寻找平面坐标系中给定点中最近的点对(若存在多对,则输出其中的一对即可)。程序运行时,先输入点的个数和一组互异的点的坐标,通过计算每对点之间的距离,从而确定出距离最近的点对。例如,在图6-1所示的8个点中,点(1,1)与(2,0.5)是间距最近的点对。
【Java代码】
import java.util.Scanner;
class GPoint
{
private double x ,y;
public void setX(double x) { this.x = x; }
public void setY(double y) { this.y = y; }
public double getX( ) { return this.x; }
public double getY() { return this.y; }
}
class FindNearestPoints {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(" 输入点的个数: ");
int numberOfPoints = input.nextInt( );
(1)points = new GPoint[numberOfPoints); //创建保存点坐标
的数组
System.out.print("请输入" + numberOfPoints + "个点的坐标: ");
for (int i = 0; i < points.length; i++) {
points [i] = (2);
points[i].setX(input.nextDouble( ));
points[i].setY(input.nextDouble( ));
}
FindNearestPoints fnp = new FindNearestPoints( );
int p1 = 0 ,p2 = 1; / / p1 和 p2 用于表示距离最近的点对在数组中的下标
double shortestDistance fnp.getDistance (points [p1],points[p2]);
//计算每一对点之间的距离
for (int i = 0; i < points.length; i++)
{
for (int j = i + 1; j < (3); j++)
{
double tmpDistance = fnp.(4)
//计算两点间的距离
if ( (5))
{
p1= i;
p2 = j;
shortestDistance = tmpDistance;
}
}
}
System.out.println(" 距离最近的点对是(" +
points[p1].getX( ) + "," + points[p1].gety( ) + ")和(" +
points[p2] .getX( ) + ", " + points[p2] .gety( ) + ")");
}
public double getDistance(GPoint pt1,GPoint pt2)
{
return Math. sqrt ((pt2.getX ( ) - pt1.getX ( )) * (pt2.getX ( ) -pt1.getX ( ))
+ (pt2.gety( ) - pt1.getY( )) * (pt2.getY( ) - pt1.getY( )));
}
}