设计一个Point类,类中包含点的坐标x、y,同时,包含一个通过另一个点求两点之间距离的distance(Point p)方法。利用该类实现点(3,4)到点(5,6)的距离。
坐等大神解答。。。。。。
public class Point {
private int xValue;
private int yValue;
public Point(int xValue, int yValue) {
this.setxValue(xValue);
this.setyValue(yValue);
}
public double distance(Point point) {
double distance = Math.sqrt(Math.pow((point.getxValue() - this.xValue),
2) + Math.pow((point.getyValue() - this.yValue), 2));
return distance;
}
/**
* @return the xValue
*/
public int getxValue() {
return xValue;
}
/**
* @param xValue
* the xValue to set
*/
public void setxValue(int xValue) {
this.xValue = xValue;
}
/**
* @return the yValue
*/
public int getyValue() {
return yValue;
}
/**
* @param yValue
* the yValue to set
*/
public void setyValue(int yValue) {
this.yValue = yValue;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "点(" + this.xValue + "," + this.yValue + ")";
}
public static void main(String[] args) {
Point pointA = new Point(3, 4);
Point pointB = new Point(5, 6);
double distance = pointA.distance(pointB);
System.out.println(pointA.toString() + "与" + pointB.toString()
+ "之间的距离为:" + distance);
}
}
class Point{
int x;
int y;
Point(int x,int y){
this.x=x;
this.y=y;
}
double distance(Point a,Point b){
return Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
}
public class Point {
private int _x;
private int _y;
public Point(int x, int y) {
this._x = x;
this._y = y;
}
public int get_x() {
return _x;
}
public void set_x(int _x) {
this._x = _x;
}
public int get_y() {
return _y;
}
public void set_y(int _y) {
this._y = _y;
}
public static double getDistance(Point p1, Point p2) {
int x = Math.abs(p2.get_x() - p1.get_x());
int y = Math.abs(p2.get_y() - p1.get_y());
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
}
}
public class Test {
public static void main(String[] args) {
Point p1 = new Point(3, 4);
Point p2 = new Point(5, 6);
double distance = Point.getDistance(p1, p2);
System.out.println(distance);//2.8284271247461903
}
}
class Point{
double x;
double y;
public Point(double x,double y){
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double distance(Point p){
double a = Math.abs(this.x - p.getX());
double b = Math.abs(this.y - p.getY());
return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
}
}
class Point{
double x;
double y;
public Point(double x,double y){
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double distance(Point p){
double a = Math.abs(this.x - p.getX());
double b = Math.abs(this.y - p.getY());
return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
}
}