题目要求
编写一个程序,设计一个点类Point,实例化两个点之后,能调用其方法求两点之间的距离。
Point
package com.newedu.javabase.homework.exrecises.topic6;
/**
* 点--类
* @author giantPanda
*
*/
public class Point {
private double x ;
private double y ;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
/**
* 计算两点之间的距离
* 公式:根号下(x1-x2)^2+(y1-y2)^2
* @param p1
* @param p2
* @return
*/
public static void distance(Point p1,Point p2) {
double sum = Math.sqrt(Math.pow(p1.x-p2.x, 2)+Math.pow(p1.y-p2.y, 2));
System.out.println(sum);
}
}
PointDemo
package com.newedu.javabase.homework.exrecises.topic6;
public class PoingDemo {
public static void main(String[] args) {
Point.distance(new Point(0,0), new Point(3,4));
}
}
运行结果:5.0
ヾ(◍°∇°◍)ノ゙