java编程:编Point类,有两属性x,y,一个方法distance(Point p1,Point p2),算两点距离,我用内部类做不

最好是用手动输入坐标点的

import java.util.Scanner;

public class Point {

private double x;
private double y;

public Point() {
this(0,0);
}

public Point(double x, double y){
this.x = x;
this.y = y;
}

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
System.out.print("Please input x for point 1:");
double x1 = scanner.nextDouble();
System.out.print("Please input y for point 1:");
double y1 = scanner.nextDouble();

System.out.print("Please input x for point 1:");
double x2 = scanner.nextDouble();
System.out.print("Please input y for point 1:");
double y2 = scanner.nextDouble();

double distance = Point.distance(new Point(x1, y1), new Point(x2, y2));
System.out.println("The distance is: " + distance);
}

public static double distance(Point p1, Point p2){
return Math.sqrt(Math.pow(p1.getX() - p2.getX(), 2) + Math.pow(p1.getY()-p2.getY(), 2));
}

public double getX() {
return x;
}

public double getY() {
return y;
}

}
---------------------
Please input x for point 1:0
Please input y for point 1:3
Please input x for point 1:4
Please input y for point 1:0
The distance is: 5.0
温馨提示:内容为网友见解,仅供参考
无其他回答

...两个属性xy,一个方法,distance(point p1,point p2.)计算两者的距离...
Java程序:class Point {protected double x;protected double y;public Point() {x = 0;y = 0;}public Point(double x, double y) {this.x = x;this.y = y;}public static double distance(Point p1, Point p2) {return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1....

java编写一个表示坐标点的类(Point),其中包含x和y两个坐标点的值,并且...
this.y = y; } public displayPoint (){ \/\/输出坐标 System.out.println("x:"+x+",y:"+y); } public static distancePoint(Point p1, Point p2){ \/\/计算坐标距离 int a = p1.getX()-p2.getX(); int b = p1.getY()-p2.getY(); return Math...

java编程题,写一个point类,计算两个point实例间的距离
double x = p.getX() - this.x;double y = p.getY() - this.y;return Math.sqrt(x*x + y*y);} } 测试类Test.java public class Test { public static void main(String[] args){ Point p1 = new Point(0,0);Point p2 = new Point(100, 0);System.out.print(p1.getDista...

求高手用JAVA帮我设计一个完整的Point类,可以计算两点之间距离和中间...
Point(double x1,double y1,double x2,double y2){\/\/构造方法 this.x1 = x1;this.x2 = x2;this.y1 = y1;this.y2 = y2;} void TestPoint(){\/\/求距离和中点坐标 x3 = (x1 + x2)\/2;y3 = (y1 + y2)\/2;d = Math.sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 ...

java point类如何使用
先实例化2个Point类对象,然后对每个对象的横纵坐标值进行设置,可以用Point类的distance方法求出2个点的距离。

java 如何计算三维空间中两点的距离
public Distance(){ } public static double getDistance(Point p1,Point p2){ return Math.sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y)+(p2.z-p1.z)*(p2.z-p1.z)); } public static void main (String[] args){\/\/测试方法 Point p1=new Point(...

java设计题:设计一个Point类,该类包含两个Int型成员变量:x,y,一个Co...
import java.awt.Color;public class Point { private int x;\/\/x坐标 private int y;\/\/y坐标 private Color color;\/\/颜色 public Point() {\/\/不带参数的构造方法 } public Point(int x,int y) {\/\/带两个参数的构造方法 this.x = x;this.y = y;} public Point(int x,int y,Color ...

JAVA题目,求解答,求代码!!!
public class Point {private double x, y;public Point(double x, double y){this.x = x;this.y = y;}public double distance(Point p){return Math.sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y));}public String toString(){return "(" + x +", "+ y ...

用java怎样编写一个二维坐标平移程序?
Point3D(int x,int y,int z){ this.x=x;this.y=y;this.z=z;} Point3D(Point2D p,int z){ x=p.x;y=p.y;this.z=z;} void offset(int a, int b,int c){ x=x+a;b=x+b;c=x+c;} void distance(Point3D a,Point3D b){ float n;n=(float)Math.sqrt((a.x-b.x)*...

Java编程题 求高人
this.y = y;} public float distance(Point p1,Point p2){ return (float) Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));} public static void main(String[] args){ Point p = new Point();Point p1= new Point();Point p2 = new Point();p1...

相似回答