已知有如下类的定义:public class Point { private double x,y; public Point(double a,double b) { x=a;y=b; }public double getX(){return x;}public double getY(){return y;}} 编写一个测试类 TestPoint,在此类中定义一个方法,此方法
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/20 09:24:06
已知有如下类的定义:public class Point { private double x,y; public Point(double a,double b) { x=a;y=b; }public double getX(){return x;}public double getY(){return y;}} 编写一个测试类 TestPoint,在此类中定义一个方法,此方法
已知有如下类的定义:public class Point { private double x,y; public Point(double a,double b) { x=a;
y=b;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
}
编写一个测试类 TestPoint,在此类中定义一个方法,此方法的方法头如下:
public double distance(Point p1,Point p2):此方法的求两点p1和p2的距离.
在main方法中,自己设定两个点(x1,y1)和(x2,y2),并求这两个点的距离.
急,本人感激不尽
已知有如下类的定义:public class Point { private double x,y; public Point(double a,double b) { x=a;y=b; }public double getX(){return x;}public double getY(){return y;}} 编写一个测试类 TestPoint,在此类中定义一个方法,此方法
import java.util.*;
class TestPoint
{
public double distance(Point p1,Point p2)
{
return Math.sqrt(Math.pow(p1.getX()-p2.getX(),2)+Math.pow(p1.getY()-p2.getY(),2));
}
public static void main(String[] args)
{
Point p1 = new Point(10.5,12.3);
Point p2 = new Point(-10.5,-2.0);
TestPoint t = new TestPoint();
double dis = t.distance(p1,p2);
System.out.println(dis);
}
}